Docker basics: Docker Registry
By default when using docker you pull the images from the Dockerhub docker registry. Most probably you have your own docker images for you application and you want to distribute them and do so in a secure way. One way to do so is to go with the already set options such as a paid plan from Dockerhub or the registries provided by cloud providers like amazon, azure etc.
The other option is setting up your own docker registry. In any case since you use docker you need to have a registry to distribute your images so that they can make it into production.
There are many benefits on managing your own registry but be aware that it requires effort on your side on provisioning and maintaining it.
Therefore we will create our docker registry
docker run -d -p 5000:5000 --restart=always --name registry registry:2
So we have a docker registry running on port 5000 and the registry will always restart.
Now let’s test our registry and push an image. First I will build a simple image with no specific purpose.
FROM ubuntu ENTRYPOINT top
It is just a dummy image printing top.
so we are gonna build it
docker build --tag top-ubuntu:1.0 .
The key is to tag your image based on the domain under which your registry runs.
Currently our registry runs on the localhost therefore by tagging we also specify the location of the registry.
docker tag top-ubuntu:1.0 localhost:5000/top-ubuntu:1.0
And no we push our image
docker push localhost:5000/top-ubuntu:1.0
Now let’s remove our images and see if our image will be downloaded from our running registry
docker rmi top-ubuntu:1.0 docker rmi localhost:5000/top-ubuntu:1.0
And let’s pull
docker pull localhost:5000/top-ubuntu:1.0
As you can see our image has been downloaded from our local registry and is ready to be used.
So far so good. The next step is securing our registry with a username and password.
Let’s start by setting the username and password
First let’s create a directory which shall contain our credentials
mkdir auth
The we shall creae
docker run --entrypoint htpasswd registry:2 -Bbn {your-user} {your-password} > auth/password-file
The file shall contain your username and password information. The password shall be hashed.
Now let’s run our secured registry
docker run -d -p 5000:5000 --restart=always --name registry -v `pwd`/auth:/auth -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/password-file registry:2
As you can see we mounted the credentials file to the docker container and we specified the location of the password-file.
Let’s try to push our image
docker push localhost:5000/top-ubuntu:1.0 . . . 059ad60bcacf: Preparing 8db5f072feec: Preparing 67885e448177: Preparing ec75999a0cb1: Preparing 65bdd50ee76a: Preparing no basic auth credentials
It’s time to login to our registry
docker login localhost:5000
Once your have provided your credentials you will be able to push the image to your local repository.
docker push localhost:5000/top-ubuntu:1.0
Be aware that our registry is not secure. Having your registry secured with credentials does not make it secure since you need to have ssl encryption.
On the next tutorial we will secure a docker registry with ssl.
Published on Web Code Geeks with permission by Emmanouil Gkatziouras, partner at our WCG program. See the original article here: Docker basics: Docker Registry Opinions expressed by Web Code Geeks contributors are their own. |