Pushing Your Docker Images to Docker Hub: A Step-by-Step Guide

Pushing Your Docker Images to Docker Hub: A Step-by-Step Guide

·

3 min read

You can find the Chinese version at Docker 實戰系列:在 Docker Hub 上分享自己的 image.

After reading the previous article on Dockerize Your App: An Introduction to Docker, You have learned how to write a Dockerfile and build your own image. But how to share the image with others for their use? That's exactly what this article is about.

Docker Registry

Docker Hub is the official Docker Registry. A Docker Registry is a place where you can pull(download) or push(upload) images. Once you upload an image to Docker Registry, others can directly pull and use it, eliminating the need for them to build the image from a Dockerfile.

Official vs. unofficial images

Images on Docker Hub are categorized as official or unofficial. Official images, such as Ubuntu and MySQL, are more reliable in terms of security and quality. To use them, you can simply execute the command docker pull <image>.

On the other hand, images like larry850806/nodejs-workspace are the ones I built myself. They will have my username as the prefix. To pull such an image, you would use the command docker pull larry850806/nodejs-workspace.

Building and Pushing to Docker Hub

In this article, I will tell you how to share your image on Docker Hub. If you don't have a Docker Hub account yet, you can sign up first. We will use "simple-express-server" as an example, but the steps can be applied to any other project that can be built into an image.

Building from Dockerfile

Since we want to upload the image to Docker Hub, we need to include our username as prefix during the build process. For example, I would use the command docker build -t larry850806/simple-express-server .. After the build, there will be an image named "larry850806/simple-express-server" with a total size of 677 MB.

Login to Docker Hub

Before pushing, we need to log in using docker login. If you have already signed in before, you don't need to do it again as Docker will remember your credentials.

Push to Docker Hub

Once logged in, you can directly run docker push larry850806/simple-express-server to push the image. You might think, This image is 677 MB, so it will take a long time to push!

However, that's not the case. Remember, the image is built in layers. In this case, the image is based on node:9.2.0. When you push, only the newly added layers are uploaded. The unchanged layers do not need to be pushed as they can use the existing node:9.2.0 layers.

You can see that many layers are Mounted from library/node. Only the top two layers need to be pushed. Thus, the push is relatively fast. Once the image is pushed to Docker Hub, it becomes publicly available for others to use.

Other Docker Registry

Although Docker Hub seems powerful and convenient, there are limitations. You can only have one private project on Docker Hub. Any more than that is not allowed.

However, you wouldn't want to make some projects public due to sensitive information such as your code and API keys. Therefore, you either need to pay for additional private projects or use other free Docker registries like Canister or Treescale. If security is a concern, you can even set up your own registry, but it will require more effort.

Conclusion

This is the second article in the series, covering how to push images to Docker Hub. If you want to easily deploy your own project or build an environment like PHP + MySQL to contribute to the community, give it a try.

In the next article, we'll discuss how to share files between containers and the local machine. If you're interested, feel free to follow along. Thank you!

Continuing with the next post in this series: Sustain Your Data: A Practical Guide to Using Docker Volumes.