CentOS 7
Sponsored Link

Docker : Use Registry
2015/12/17
 
Install Docker-Registry to build Private Registry for Docker images.
[1] Pull the Registry image and run it.
Container Images are located under [/var/lib/regstry] on Registry v2 Container,
so map to mount [/var/lib/docker/registry] on parent Host for Registry Container to use as Persistent Storage.
[root@dlp ~]#
docker pull registry:2

[root@dlp ~]#
mkdir /var/lib/docker/registry

[root@dlp ~]#
docker run -d -p 5000:5000 \
-v /var/lib/docker/registry:/var/lib/registry \
registry:2
996e918ad31f0ebcbc27187c18b5f70876e840f5c447ad370844b984bdcf0f9d

[root@dlp ~]#
docker ps

CONTAINER ID   IMAGE        COMMAND                  CREATED         STATUS         PORTS                                       NAMES
996e918ad31f   registry:2   "/entrypoint.sh /etc…"   8 seconds ago   Up 7 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   peaceful_solomon

# if Firewalld is running, allow ports

[root@dlp ~]#
firewall-cmd --add-port=5000/tcp --permanent

[root@dlp ~]#
firewall-cmd --reload
# to use the Registry from other Docker Client Hosts, set like follows

[root@client ~]#
vi /etc/docker/daemon.json
# create new or add

# add Hosts you allow HTTP connection (default is HTTPS)

{
  "insecure-registries":
    [
      "docker.internal:5000",
      "dlp.srv.world:5000"
    ]
}

[root@client ~]#
systemctl restart docker
[root@client ~]#
docker tag nginx dlp.srv.world:5000/nginx:my-registry

[root@client ~]#
docker push dlp.srv.world:5000/nginx:my-registry

[root@client ~]#
docker images

REPOSITORY                 TAG           IMAGE ID       CREATED       SIZE
nginx                      latest        f0b8a9a54136   11 days ago   133MB
dlp.srv.world:5000/nginx   my-registry   f0b8a9a54136   11 days ago   133MB
[2] This is for the case you set self-signed certificate and enable HTTPS connection.
This example is based on that certificate were created under the [/etc/pki/tls/certs] directory.
[root@dlp ~]#
docker run -d -p 5000:5000 \
-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/server.crt \
-e REGISTRY_HTTP_TLS_KEY=/certs/server.key \
-v /etc/pki/tls/certs:/certs \
-v /var/lib/docker/registry:/var/lib/registry \
registry:2
2d8b71a51f374154f5d8d331ca8b9d0a773598defcbf264834d71a2cec466b8b

# to use the Registry from other Docker Client Hosts, set like follows

# it's not need to add [insecure-registries] but

# need to locate server's certificate on the client side like follows

[root@client ~]#
mkdir -p /etc/docker/certs.d/dlp.srv.world:5000

[root@client ~]#
scp dlp.srv.world:/etc/pki/tls/certs/server.crt /etc/docker/certs.d/dlp.srv.world:5000/ca.crt
[root@client ~]#
docker tag centos dlp.srv.world:5000/centos:my-registry

[root@client ~]#
docker push dlp.srv.world:5000/centos:my-registry

[root@client ~]#
docker images

REPOSITORY                  TAG           IMAGE ID       CREATED        SIZE
dlp.srv.world:5000/centos   my-registry   300e315adb2f   5 months ago   209MB
centos                      latest        300e315adb2f   5 months ago   209MB
[3] This is for the case you set valid certificate like Let's Encrypt and enable HTTPS connection.
This example is based on that certificate were created under the [/etc/letsencrypt] directory.
[root@dlp ~]#
docker run -d -p 5000:5000 \
-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/fullchain.pem \
-e REGISTRY_HTTP_TLS_KEY=/certs/privkey.pem \
-v /etc/letsencrypt/live/dlp.srv.world:/certs \
-v /var/lib/docker/registry:/var/lib/registry \
registry:2
28512a4b96d93172b7af66f6b7263fdbff8d729a76659c8b955c60800b557f4f

# to use the Registry from other Docker Client Hosts, set like follows

# it's not need to change any specific settings, it can use with default

[root@client ~]#
docker tag nginx dlp.srv.world:5000/my-nginx:my-registry

[root@client ~]#
docker push dlp.srv.world:5000/my-nginx:my-registry

[root@client ~]#
docker images

dlp.srv.world:5000/my-nginx    my-registry   7ce4f91ef623   6 days ago     133MB
nginx                          latest        7ce4f91ef623   6 days ago     133MB
[4] To enable Basic authentication, Configure like follows.
[root@dlp ~]#
yum install httpd-tools
# add users

# add [-c] at initial file creation

[root@dlp ~]#
htpasswd -Bc /etc/docker/.htpasswd admin

New password:
Re-type new password:
Adding password for user admin

[root@dlp ~]# docker run -d -p 5000:5000 \
-v /var/lib/docker/registry:/var/lib/docker/registry \
-v /etc/docker:/auth \
-e REGISTRY_AUTH=htpasswd \
-e REGISTRY_AUTH_HTPASSWD_PATH=/auth/.htpasswd \
-e REGISTRY_AUTH_HTPASSWD_REALM="Registry Realm" \
registry:2 
77c4b2db35af4cf06b09474062767ec9c6e5b19465d6686843d847a35f464a2f

[root@dlp ~]#
docker ps

CONTAINER ID   IMAGE                    COMMAND                  CREATED              STATUS              PORTS                                       NAMES
77c4b2db35af   registry:2               "/entrypoint.sh /etc…"   17 seconds ago       Up 16 seconds       0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   great_curran

# verify possible to access

# authenticate by a user added with [htpasswd]

[root@client ~]#
docker login dlp.srv.world:5000

Username: admin
Password:
Login Succeeded

[root@client ~]#
docker pull dlp.srv.world:5000/nginx:my-registry

[root@client ~]#
docker images

REPOSITORY                 TAG           IMAGE ID       CREATED       SIZE
dlp.srv.world:5000/nginx   my-registry   62d49f9bab67   4 weeks ago   133MB
Matched Content