DEV Community

Cover image for Storing and Publishing your Docker Images
Kostas Kalafatis
Kostas Kalafatis

Posted on

Storing and Publishing your Docker Images

From the get-go, Docker's popularity stemmed from having a central hub where users could grab images, tweak them, and share their creations with others. Docker Hub has become a go-to resource for developers looking for pre-built images to kickstart their projects. Despite some security hiccups along the way, it's still the default starting point for many.

As a public repository, Docker Hub is a treasure trove of open-source images, perfect for streamlining your next project. It's also a great platform for companies and developers to share their work with the community.

But hold on, there's more to the image-sharing game than just Docker Hub. When it comes to production environments, especially for larger teams, relying on a public repository might not be the most secure or efficient option.

Nowadays, you've got cloud-based options like Amazon ECR or Google Container Registry, which offer more control and security for your images. And if you're feeling adventurous, I'll even show you how to set up your own local registry later on.

In this post, we'll get hands-on with transferring Docker images between different machines. We'll start by exploring how to share your creations on Docker Hub, making them publicly available to the world. Then, for a more private setup, we'll dive into setting up your own local Docker registry right on your development machine. This gives you more control and security over your images, perfect for team collaboration or sensitive projects.

Moving Docker Images Manually

In certain situations, like dealing with firewalls or network restrictions, you might need to directly transfer a Docker image between machines without using a registry. Luckily, Docker has you covered! This example will walk you through how to move an image from one system to another without relying on a registry.

We are going to use the image created in the previous post, and the docker save command. We'll save it as a .tar file with a specific filename and directory. To do that use the following command. Make sure that you replace the user, image name, and tag of the image with what you used.



docker save -o /tmp/base-image.tar base-image:1.1.0


Enter fullscreen mode Exit fullscreen mode

You should now find the image you just saved sitting in your /tmp directory, packaged up as a TAR file (base-image.tar). This is because the docker save command always creates a TAR archive. But here's a fun fact: the file extension itself doesn't matter all that much. You could rename it to .tar.gz or even .myimage if you wanted to! The content inside is what's important, and Docker will recognize it as an image file regardless of the extension.


Use the du command to verify that the base-image.tar file has data in it



du -sh /tmp/base-image.tar
22M     base-image.tar


Enter fullscreen mode Exit fullscreen mode

Now that you've saved your Docker image as a TAR file, you're free to move it around however you like. You can use tools like rsync, scp, or even a simple cp command. If you want to save a bit of space during the transfer, you can even compress the TAR file into a ZIP archive.

For this example, let's just remove the image from your current system to clean things up. You'll need the image ID for this



docker images


Enter fullscreen mode Exit fullscreen mode

And then remove the image



docker rmi -f b5e88b0b4601                                                         

Untagged: base-image:1.1.0
Deleted: sha256:b5e88b0b4601183bb9425186cd95fee52e22be5f1ec8c58b95a50f42e78c2004


Enter fullscreen mode Exit fullscreen mode

Imagine that you've successfully transferred your image to a new machine, let's load it back into Docker so you can use it again. You'll need the docker load command for this:



docker load -i /tmp/base-image.tar


Enter fullscreen mode Exit fullscreen mode

You should get output like the following



Loaded image: base-image:1.1.0


Enter fullscreen mode Exit fullscreen mode

Use the docker image command to bring up the image you just loaded into your local environment



docker images


Enter fullscreen mode Exit fullscreen mode

You should get output similar to the following



REPOSITORY                                      TAG       IMAGE ID       CREATED             SIZE
base-image                                      1.1.0     b5e88b0b4601   About an hour ago   22MB


Enter fullscreen mode Exit fullscreen mode

This example was a straightforward demonstration, but it aimed to highlight that even without access to a registry, you can still transport your Docker images across systems.

Now, let's shift our focus to the more common methods of storing, sharing, and distributing your Docker images. We'll delve into using Docker Hub and other registries to streamline your workflow and collaborate effectively.

Storing and Deleting Images from Docker Hub

While Docker Hub offers a free tier for working with Docker images, it's important to note that you only get one private repository for free. If you need more than that, you'll have to opt for a paid monthly plan.

For most teams, a single private repository is rarely enough. So, if you're considering Docker Hub, be prepared to factor in the cost of a subscription if you need multiple private repositories.

However, if your needs are limited and you're okay with your images being public, then a free account gives you an unlimited number of public repositories to work with.

Storing Docker Images in Docker Hub

Alright, let's get your base-image image up on Docker Hub. We'll walk through creating a new repository, pushing your image to it, and then deleting the repository when we're done. This will give you a taste of how to manage your Docker images in the cloud.

To follow along with these Docker shenanigans, you'll need a Docker Hub account. Don't worry, I'm not made of money either, so we'll stick to the free stuff (unless you want to sponsor me, of course). If you haven't signed up yet, head over to Docker Signup and create your free account. Then come back here and let's get this Docker party started!

First things first, head over to your Docker Hub account. Once you're logged in, look for the "Repositories" section. You should see a blue button on the right side of the screen that says "Create Repository". Click that button to start setting up a new home for your base-image image.

Image description


When creating a new repository, you'll be presented with a page like the one that follows. Fill in the Name of the repository, which is usually the name of the image or service you are storing (in this case, basic-app). You also have the option to set the repository as Public or Private, and in this instance, select Public:

Image description


Once your new repository is created, it will provide details on how to start pushing your images to your new repository. Tag your image with /:tag to let Docker know where it will be pushing the image and which repository Docker will be pushing it to:



docker tag base-image cyberduckling/base-image:v1


Enter fullscreen mode Exit fullscreen mode

Great, Docker is now aware of where to send your image. Before you can push the image, you'll need to log in to your Docker Hub account through the Docker CLI. Use the docker login command and enter the same username and password you used when you logged into Docker Hub earlier. Then, let's push it to Docker Hub using the following command:



docker push cyberduckling/base-image:v1


Enter fullscreen mode Exit fullscreen mode

Alright! Now, if you head back to your Docker Hub account and look at your newly created base-image repository, you should see your image version (v1) listed there. It's officially up in the cloud and ready to be shared or used in your projects

Image description


If you look at the previous screenshot, you'll see a button on the right side of the screen labeled "Public View." This lets you preview exactly what the public will see when they search for your image on Docker Hub. Go ahead and click it! You should see a screen similar to this

Image description

This exercise demonstrates how Docker Hub can be a great way to share your Docker images with the world, making it easy for others to collaborate on your projects or build upon your work. While a public repository might not always be the perfect fit for a large company, it offers a similar platform for collaboration and code sharing as GitHub does for software development. By publishing your images on Docker Hub, you're contributing to a thriving community of developers and making your own projects more accessible and impactful.

The Docker Registry

Docker Registry is like a personal storage space for your Docker images. In most cases, these registries are kept private, only accessible by your team. There are many great options out there, and one of them is a ready-made registry image maintained by Docker itself.

There are many reasons why you might want to run your own Docker registry. Maybe you have concerns about security, want to keep your work under wraps until it's ready, or simply prefer the convenience of having your images right there on your own machine.

In this section, we'll guide you through setting up a Docker registry on your development environment and start storing your valuable images there.

Creating a Local Docker Registry

In this hands-on example, we'll create a private Docker registry that runs directly on your machine. Don't worry, this won't be accessible to your team or the public – it's just for you to experiment and decide if a local registry is the right fit for your needs. We'll even set up a custom domain name to make it feel like a real registry

To give your local registry a custom domain name, you'll need to edit your system's hosts file. Here's how you can find it:

  • Windows: C:\Windows\System32\drivers\etc\hosts
  • Linux/macOS: /etc/hosts

Open the hosts file in a text editor (you might need administrator privileges on Windows) and add the following line:



127.0.0.1    my-registry.local


Enter fullscreen mode Exit fullscreen mode

This line tells your computer to resolve the domain name my-registry.local to the local IP address 127.0.0.1, which is where your registry will be running.


Pull the latest registry image down from Docker Hub



docker pull registry


Enter fullscreen mode Exit fullscreen mode

Great, you've set up the domain! Now, let's fire up your local Docker registry using the following command:



docker run -d -p 8954:5000 --restart=always --name my-registry registry:2


Enter fullscreen mode Exit fullscreen mode

I am using port 8954 because I have other things running on 5000, but feel free to use any port you like.


Run the docker ps command to see the registry container running on your system



docker ps


Enter fullscreen mode Exit fullscreen mode

You should see an output similar to the following



CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                    NAMES
ba187cd38a11   registry:2   "/entrypoint.sh /etc…"   42 seconds ago   Up 41 seconds   0.0.0.0:8954->5000/tcp   my-registry


Enter fullscreen mode Exit fullscreen mode

Now run the docker tag command to tag existing images with the registry hostname and port my-registry:8954



docker tag cyberduckling/base-image:v1 my-registry:8954/cyberduckling/base-image:v1


Enter fullscreen mode Exit fullscreen mode

Delete the original images using the docker image remove command



docker image remove my-registry:8954/cyberduckling/base-image:v1


Enter fullscreen mode Exit fullscreen mode

Finally, pull down the image from your local registry



ocker pull my-registry:8954/cyberduckling/base-image:v1


Enter fullscreen mode Exit fullscreen mode

This wraps up our exploration of setting up a local Docker registry on your system. While this simple registry isn't officially supported, it provides a hands-on way to understand how registries work and how they can benefit your team. It's a great stepping stone for evaluating whether a private registry is the right solution for your needs.

If you're looking for a more comprehensive, supported option, Docker also offers Docker Trusted Registry, a commercial product designed to meet enterprise-level requirements for image management and security. This could be a good next step to consider if you need a more robust registry solution.

Summary

This post dives into the world of Docker image sharing, starting with a brief overview of Docker Hub's history and benefits. It then guides you through hands-on examples of how to move Docker images between machines, both manually (using a TAR file) and by leveraging Docker Hub's repository system. You'll learn how to create a repository on Docker Hub, push your images to it, and even view your published images from a public perspective.

But the fun doesn't stop there! The post also explores setting up a local Docker registry on your development machine, giving you a taste of how to create a private repository and manage your images in a controlled environment. This is a great option for teams or individuals who want more control over their image storage and security.

Whether you're a beginner looking to share your Docker creations or a seasoned pro exploring private registry options, this post offers valuable insights and practical examples to help you master Docker image management.

Top comments (0)