Just like we use a version control system for source code management, in a similar way, we can use the container registry to manage container images. There are numerous cloud-hosted container registries available in the market. Some of the popular container registries include – Docker Hub, Amazon ECR, Azure Container Registry, JFrog Container Registry, etc.
This tutorial will discuss sharing Docker images with or without the Docker Hub registry.
1. Creating a Custom Image
$ cat Dockerfile FROM alpine:latest # Metadata LABEL version="1.0" LABEL description="Alpine based image for demo" LABEL maintainer="howtodoinjava.com" # Install curl command RUN apk add --update curl && rm -rf /var/cache/apk/* # Run command at container startup CMD /bin/sh
Now, let’s create an image using the image build child command:
$ docker image build -t narendra01/curl-demo:1.0 .
In this example, narendra01/curl-demo represents the name of the Docker Hub repository whereas the 1.0 represents the tag associated with the image. Finally, let’s verify that the image has been created successfully. We can achieve this using the image list child command:
$ docker image list REPOSITORY TAG IMAGE ID CREATED SIZE narendra01/curl-demo 1.0 e04d9dc30592 2 minutes ago 7.68MB
2. Sharing an Image with Docker Hub
Using the Docker Hub registry is one of the easiest and most preferred ways to share container images. Let’s discuss the steps for the same.
First, let’s authenticate with the Docker Hub using the login child command. We provide a password from the standard input stream.
$ docker login --username=<user-name>
Next, let’s use the image push child command to publish the Docker image:
$ docker image push narendra01/curl-demo:1.0 The push refers to repository [docker.io/narendra01/curl-demo] eb45f27a1b43: Pushed e5e13b0c77cb: Mounted from library/alpine 1.0: digest: sha256:83fc552ca4842687655c7b4bba3606fcc35b89ae8cbd8f34ee88d6ad7e5d0e32 size: 739
The above command uploads an image to the Docker Hub, which we can verify by logging into the docker hub from the browser.
From now onwards, other users can download this image using the image pull child command.
3. Sharing an Image without Docker Hub
Another but less popular way to save and share docker images is a tar bundle. It allows to share the Docker images just like any other regular files.
At a high level, this method consists of the following three steps:
- Save the image as a tar bundle
- Copy the tar bundle to the remote machine
- Load the tar bundle as a Docker image
3.1. Save the Image as a tar Bundle
We can use the image save child command to create a tar bundle of the narendra01/curl-demo:1.0 image:
$ docker image save -o curl-demo.tar narendra01/curl-demo:1.0
We can verify that the tar bundle has been created successfully:
$ ls -1 curl-demo.tar
3.2. Copy the tar Bundle to the Remote Machine
The next step is transferring the tar bundle to the remote machine. We can use various methods for this, such as – using the scp, ftp, uploading it to the cloud storage, and so on.
Optionally, a good practice is to compress the tar bundle before transferring it to the remote machine to reduce the data transfer time. There are various compression utilities available in Linux, such as – bzip2, lzip, gzip, and so on.
So, let’s use the bzip2 command to do the in-place compression:
$ bzip2 curl-demo.tar $ ls -1 curl-demo.tar.bz2
3.3. Load the tar Bundle as a Docker Image
On another machine, we use the -d option of the bzip2 command to uncompress the tar bundle:
[remote-machine]$ bzip2 -d curl-demo.tar.bz2 [remote-machine]$ ls -1 curl-demo.tar
Next, use the image load child command to load the uncompressed image:
[remote-machine]$ docker image load --input curl-demo.tar e5e13b0c77cb: Loading layer [==================================================>] 5.828MB/5.828MB eb45f27a1b43: Loading layer [==================================================>] 2.395MB/2.395MB Loaded image: narendra01/curl-demo:1.0
We can verify that the image has been loaded successfully:
[remote-machine]$ docker image list REPOSITORY TAG IMAGE ID CREATED SIZE narendra01/curl-demo 1.0 e04d9dc30592 2 hours ago 7.68MB
4. Advantages of using Docker Hub
The Docker Hub is a cloud-hosted container registry and provides the following benefits:
- Higher data protection
- Add-on security features such as vulnerability or CVE scanning
- Reusable cache layers
- Simple interface to upload and download Docker images. For example, push and pull commands
This Docker tutorial discussed how to share the Docker image with and without the Docker Hub. First, we built a custom image and shared the image by pushing it to Docker Hub. Next, we discussed sharing the same image without a Docker Hub as tar file. Finally, we learned a few advantages of using the Docker Hub registry.
Happy Learning !!