Docker - Private Registries

Introduction to Docker Registries

Hello there, future Docker masters! Today, we're going to dive into the fascinating world of Docker registries. Don't worry if you're new to this; I'll guide you through it step by step, just like I've done for countless students over my years of teaching. Let's start our journey!

Docker - Registries

A Docker registry is like a library for your Docker images. It's a place where you can store, manage, and distribute these images. Think of it as a bookshelf where you keep all your favorite books (or in this case, Docker images) neatly organized and easily accessible.

Types of Docker Registries

There are two main types of Docker registries:

  1. Public registries (like Docker Hub)
  2. Private registries

Today, we'll focus on private registries. But why would you want a private registry? Well, imagine you're writing a secret recipe. You wouldn't want to post it on a public bulletin board, would you? That's where private registries come in handy!

Setting Up a Private Registry

Let's roll up our sleeves and set up our own private registry. It's easier than you might think!

Step 1: Pull the Registry Image

First, we need to pull the official registry image from Docker Hub. Open your terminal and type:

docker pull registry:2

This command is like asking the librarian to bring you the "registry" book from the "Docker Hub" library.

Step 2: Run the Registry Container

Now, let's start our registry:

docker run -d -p 5000:5000 --name my-registry registry:2

Let's break this down:

  • -d: Run the container in detached mode (in the background)
  • -p 5000:5000: Map port 5000 of the container to port 5000 on your host
  • --name my-registry: Give our container a friendly name
  • registry:2: The image we're using to create this container

Congratulations! You now have a private registry running on your machine.

Pushing Images to Your Private Registry

Now that we have our registry, let's put some images in it!

Step 1: Tag an Image

First, we need to tag an image to associate it with our registry:

docker tag my-image:latest localhost:5000/my-image:latest

This is like putting a special sticker on your book to show it belongs in your private library.

Step 2: Push the Image

Now, let's push this image to our registry:

docker push localhost:5000/my-image:latest

You've just added your first book to your private library!

Pulling Images from Your Private Registry

To use an image from your private registry, you can pull it like this:

docker pull localhost:5000/my-image:latest

It's that simple! You're now retrieving books from your private library.

Securing Your Private Registry

Now, we wouldn't want just anyone accessing our private library, would we? Let's add some security.

Using Basic Authentication

Here's how to set up basic authentication:

  1. Create a password file:
docker run --entrypoint htpasswd registry:2 -Bbn myuser mypassword > auth/htpasswd
  1. Run the registry with authentication:
docker run -d \
  -p 5000:5000 \
  --name secure-registry \
  -v "$(pwd)"/auth:/auth \
  -e "REGISTRY_AUTH=htpasswd" \
  -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
  -e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" \
  registry:2

Now your registry is protected with a username and password!

Useful Docker Registry Commands

Here's a handy table of commands you might find useful:

Command Description
docker pull registry:2 Pull the registry image
docker run -d -p 5000:5000 --name my-registry registry:2 Run a registry container
docker tag image:tag localhost:5000/image:tag Tag an image for the local registry
docker push localhost:5000/image:tag Push an image to the local registry
docker pull localhost:5000/image:tag Pull an image from the local registry
docker search localhost:5000/ Search the local registry

Conclusion

And there you have it, folks! You've just set up your very own private Docker registry. Remember, practice makes perfect. Don't be afraid to experiment and try different things. Who knows? You might discover something new and exciting!

In my years of teaching, I've seen students go from complete beginners to Docker wizards. With patience and persistence, you'll get there too. Keep pushing (pun intended) and happy Dockering!

Credits: Image by storyset