DEV Community

SOVANNARO
SOVANNARO

Posted on • Edited on

๐Ÿšข Using Docker Registry with Swarm: Ship Your Images Like a Pro

Hey there, Docker captain! ๐Ÿณ
Ready to take your container game to the next level? Today, weโ€™re going to set sail into the world of Docker Swarm and Docker Registryโ€”two amazing tools that work together like peanut butter and jelly ๐Ÿž.

By the end of this article, youโ€™ll learn how to:

  • ๐Ÿง  Understand what Docker Registry and Swarm do together
  • ๐Ÿ›  Set up a private Docker Registry
  • ๐Ÿ•ธ Use Docker Swarm to deploy services with your own images
  • ๐Ÿ˜ Feel confident and happy shipping your containers like a real DevOps pro

Letโ€™s dive in!


๐Ÿ“ฆ What is Docker Registry (Quick Recap)

Think of Docker Registry like a warehouse. Itโ€™s where all your Docker images live so they can be pulled (downloaded) later.
The most famous public registry is Docker Hub, but you can also create your own private registryโ€”super helpful when working on internal or secret projects.

๐Ÿ’ก Example: Instead of pulling nginx from Docker Hub, you could pull myregistry.local:5000/nginx.


๐Ÿ•น What is Docker Swarm?

Docker Swarm is like a ship captain. It lets you manage a fleet of containers across multiple servers (called nodes) like a true master of the seas ๐ŸŒŠ. You can deploy, scale, and update your services with just one command.

It's Dockerโ€™s built-in orchestration system. Simpler than Kubernetes, perfect for small to mid-size projects.


๐ŸŽฏ Why Use Docker Registry with Docker Swarm?

Great question!

When you use Swarm to deploy containers, it needs to pull the image you specify. If that image lives in your own registry, every Swarm node must be able to access it.

๐Ÿ’ฅ So combining the two means:

  • You control where your images are stored
  • You can deploy your own custom images in seconds
  • It works even in air-gapped networks (no internet)

๐Ÿ›  Step-by-Step: Set Up Docker Registry + Swarm

1. ๐Ÿ”ง Initialize Docker Swarm

First, pick one machine to be the manager node.

docker swarm init
Enter fullscreen mode Exit fullscreen mode

Boom! Now you have a swarm.

If you have other machines to add:

docker swarm join-token worker
Enter fullscreen mode Exit fullscreen mode

Run the given command on the other machines (your future worker nodes).


2. ๐Ÿ“ฆ Run a Private Docker Registry

Letโ€™s run the registry container on port 5000:

docker service create \
  --name registry \
  --publish published=5000,target=5000 \
  --mount type=volume,source=registry-data,target=/var/lib/registry \
  registry:2
Enter fullscreen mode Exit fullscreen mode

This creates a Swarm service called registry, which will be available at http://<manager-ip>:5000.


3. ๐Ÿ— Build and Push a Custom Image

Letโ€™s build a sample image and push it to your new registry.

docker build -t myregistry.local:5000/hello:v1 .
docker push myregistry.local:5000/hello:v1
Enter fullscreen mode Exit fullscreen mode

โš ๏ธ If your registry is not using HTTPS, you need to allow insecure registries in Dockerโ€™s config:

On each Swarm node, edit /etc/docker/daemon.json:

{
  "insecure-registries": ["myregistry.local:5000"]
}
Enter fullscreen mode Exit fullscreen mode

Then restart Docker:

sudo systemctl restart docker
Enter fullscreen mode Exit fullscreen mode

4. ๐Ÿš€ Deploy Your Service in Swarm

Now letโ€™s deploy the image we just pushed:

docker service create \
  --name hello-service \
  --replicas 3 \
  --publish published=8080,target=80 \
  myregistry.local:5000/hello:v1
Enter fullscreen mode Exit fullscreen mode

๐ŸŽ‰ Woohoo! Now youโ€™ve deployed your own image from your own registry on your own Swarm!


๐Ÿ‘€ Check Everything

See all services:

docker service ls
Enter fullscreen mode Exit fullscreen mode

Check where containers are running:

docker service ps hello-service
Enter fullscreen mode Exit fullscreen mode

Scale it up:

docker service scale hello-service=5
Enter fullscreen mode Exit fullscreen mode

Remove a service:

docker service rm hello-service
Enter fullscreen mode Exit fullscreen mode

๐Ÿ Final Thoughts

Using Docker Registry with Swarm gives you full control over how your containers are built, stored, and deployed. Itโ€™s like having your own private shipyard and fleet!

Now you:

  • Know how to create a private Docker Registry
  • Know how to deploy Swarm services using custom images
  • Look awesome doing it ๐Ÿ˜Ž

So go aheadโ€”ship your code with confidence, Captain!


โค๏ธ Bonus Tips

  • Use a domain name and HTTPS with your registry in production
  • Set up authentication for private access
  • Clean up unused images with registry garbage-collect
  • Backup your registry data (it's just a volume!)

If you enjoyed this article, let your DevOps friends know, and go build something awesome today! ๐Ÿ’ฅ

Want another guide or have a question? Just ask! ๐Ÿ˜Š

Top comments (0)