Table of Contents
Installing Docker Images from private repositories in Docker Swarm
When the Docker images are pulled from public repository like DockerHub they can be easily deployed using a simple command such as
docker service create nginx
and if you happen to use compose file you can use
docker stack deploy -c "name of the compose file".
However in the case of private repositories you need to provide Docker credentials and Docker repository details too.
In this tutorial, I will list out the steps needed for deploying Docker images from a DockerHub private repository.
Login to DockerHub
Considering that you have a substantial number of images and using a Docker-compose file where you have listed the Docker images needed from private repositories.The first step would be is to log on to DockerHub from all nodes(manager and worker)
Use this command docker login and input your username and password.
The reason why this step is important (in my experience) is that we might want to use the deploy constraint available in docker compose(see below) to run Docker containers only on worker nodes. I believe in this cases the Docker pull is ran from worker machines only and that is why it is essential to log in on worker nodes. Not logging from worker nodes in this scenario failed to pull images for me in Worker nodes.
constraints: [node.role == worker]
Deploying images on Swarm
When deploying on Swarm you need to run the following command, considering that you have created a compose file
docker login -u #DockerHub Username# -p #DockerHub Password# registry.hub.Docker.com/#Organization-Or-DockerHubUserName# && Docker stack deploy -c Docker-swarm.yml #STACK-NAME# --with-registry-auth
Breaking above command for clarification
- -u #DockerHub Username# : The DockerHub Username
- -p #DockerHub Password#: The DockerHub Password
- #Organization-Or-DockerHubUserName#: In case you have created a team/organisation on DockerHub and you have pushing images like organisation/Docker-Image. This is a scenario where you can have multiple team members working on same Docker image and pushing the image using something like TEAM/DockerImage.In case you are a single user and don’t have any Team defined in DockerHub you can use username
- #STACK-NAME#: The stack name, could be just a “test” or something more meaningful name suited to requirement
- –with-registry-auth: This option tells that you are pulling images from a docker hub registry with authorization.
a simple example of above command would be like
docker login -u username -p password registry.hub.Docker.com/myproject && Docker stack deploy -c Docker-swarm.yml test --with-registry-auth