Last active
August 29, 2015 14:13
-
-
Save William-Yeh/c6b67e0fcf5e9f65eeba to your computer and use it in GitHub Desktop.
Simple Docker CLI wrapper to "pull" public registry images from private registry.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# | |
# Simple Docker CLI wrapper: | |
# - let "docker pull" to pull public registry images | |
# from private registry. | |
# - let other "docker" commands to act as before. | |
# | |
# Will use environment variable PRIVATE_DOCKER_REGISTRY | |
# to pull images. | |
# | |
# | |
# USAGE: | |
# | |
# $ PRIVATE_DOCKER_REGISTRY="my-registry.com" \ | |
# ./DOCKER.pl pull williamyeh/wrk | |
# | |
# | |
# @author William Yeh <[email protected]> | |
# @date 2015-01-08 | |
# | |
# @LICENSE - public domain. | |
# | |
use strict; | |
use warnings; | |
# constants | |
my $PRIVATE_DOCKER_REGISTRY_NAME = "PRIVATE_DOCKER_REGISTRY"; | |
# variables | |
my $Public_Image; | |
my $Private_Image; | |
if (should_bypass()) { | |
call_original_docker(); | |
} | |
else { | |
handle_docker_pull(); | |
call_original_docker(); | |
convert_tag(); | |
remove_private_tag(); | |
} | |
sub should_bypass { | |
return (scalar @ARGV < 2) | |
|| ($ARGV[0] ne "pull") | |
|| ! defined $ENV{$PRIVATE_DOCKER_REGISTRY_NAME} | |
; | |
} | |
sub call_original_docker { | |
my @cmd_line = ("docker"); | |
push(@cmd_line, @ARGV); | |
exec_docker_cmd(@cmd_line); | |
} | |
sub handle_docker_pull { | |
$Public_Image = pop @ARGV; | |
$Private_Image = $ENV{$PRIVATE_DOCKER_REGISTRY_NAME} | |
. "/" . $Public_Image; | |
push(@ARGV, $Private_Image); | |
} | |
sub convert_tag { | |
my @cmd = ("docker", "tag", "-f", $Private_Image, $Public_Image); | |
exec_docker_cmd(@cmd); | |
} | |
sub remove_private_tag { | |
my @cmd = ("docker", "rmi", "-f", $Private_Image); | |
exec_docker_cmd(@cmd); | |
} | |
sub exec_docker_cmd { | |
my @cmd = @_; | |
print "--> @cmd", "\n"; | |
system(@cmd) == 0 | |
|| die "Failed: $?"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment