An image is an executable package that includes everything needed to run an application--the code, a runtime, libraries, environment variables, and configuration files.
A container is a runtime instance of an image--what the image becomes in memory when executed (that is, an image with the state, or a user process).
A Docker image can be version controlled just like code. As we store code in a repository like Git, Docker images can be stored in a Docker repository Hosting Service like Docker Hub.
Docker Hub is a service provided by Docker for hosting, finding, and sharing Docker Repositories. Just like git repo hosting services, a Docker repository can be public or private. A third-party repository hosting services also exists. The Docker Hub and other third party repository hosting services are called registries. For example, RedHat has their own registry to host their container images.
One important point to remember is that a registry has many repositories, while a repository has many different versions of the same image. These registries can be either private or public, depending on the needs of the organization. Docker Hub is one such example of a public registry.
Docker is configured to use Docker Hub as it's default registry. Use the
$ docker info
command to view the registry that Docker is currently using. By default, it points to https://index.docker.io/v1/ which is the registry location for Docker Hub.
On Docker Hub, there are two kinds of images - official and unofficial. Official images are trusted and optimized. They have clear documentation, promote best practices, and are designed for the most common use cases. On the other hand, an unofficial image is any image that is created by a user. Docker Hub follows some standards so that both can easily be identified. Official images contain only the <image_name>
as its image name but unofficial images have the syntax as <username>/<image_name>
. Also, the official image has official written in the listing as shown in the below screenshot.
Official Image of Ubuntu
Unofficial Image by User ubuntuz
Downloading Image from Docker Hub
Search the Docker Hub for Images
We could also look up and find images on Docker Hub by either using the search bar on the website or using the below command:
1
$ docker search <image_name>
Let us search for an image with the name of busybox:
Pull an Image or a Repository from a Registry
To download a particular image, or set of images (i.e., a repository), use docker pull
. If no tag is provided, Docker Engine uses the :latest
tag as a default.
1
$ docker pull <image_name>:<tag_name>
Lets list images we have: $ docker images -a
Creating a Docker Image
We could create our own Docker image in two ways:
1. Using Dockerfile to Create an Image
A Dockerfile is a simple text document that contains a series of commands that Docker uses to build an image. Several commands supported in Dockerfile are FROM, CMD, ENTRYPOINT, VOLUME, ENV, and more.See: https://violetstreamstechnology.blogspot.com/2020/10/all-you-need-to-know-about-docker.html on how to create docker image using Dockerfile
1
2FROM busybox:latest
CMD ["date"]
Note: An important point to remember is that this file should be named as Dockerfile.
Create a dockerfile with vi: $ vi Dockerfile
Save the file
The docker build
command builds an image from a Dockerfile. To build the image from our above Dockerfile : Use the below command:
Note: use -t To tag your image
$ docker build -t example_image .
To build
$ docker run -it --name example_app example_image
2. Create an Image from A container Docker-hub
Another way to create an image is by pulling a Docker image, creating a container from it, and then modifying or making changes in it like installing our app in that container. Now, using the docker commit
command, we can create a Docker image from the container.
Let's look at an example of how to create a Docker image from our example_app container:
1
2
3
4
5
6$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
c3df2dd33276 example-image "date" 5 seconds ago Exited (0) 4 seconds ago example_app
$ docker commit example_app example_image2:latest
sha256:7b48e8355aa7a7ea32d554f26d0bd21f4d069d8526c68f1d098acac9111a9adf
Now enter docker images -a to view newly created image
Publish an Image to Docker Hub
To be able to publish our Docker images to Docker Hub, there are some steps that we need to follow:
Step 1: Sign Up for Docker Hub
Before we can push our image to Docker Hub, we will first need to have an account on Docker Hub. Create an account by visiting this link. The signup process is relatively simple.
Step 2: Create a Repository on Docker Hub
For uploading our image to Docker Hub, we first need to create a repository. To create a repo:
- Sign in to Docker Hub
- Click on ‘Create Repository’ on the Docker Hub welcome page:
- Fill in the repository name as
example-image
, the Docker image that we created earlier using Dockerfile. Also, describe your repo like "My First Repository". Finally, click on the create button. Refer to the below screenshot:
Step 3: Push Image to Docker Hub
Now we will push our built image to the Docker Hub registry:
Log into the Docker public registry from your local machine terminal using Docker CLI:
1
$ docker login
shellTag the image
This is a crucial step that is required before we can upload our image to the repository. As we discussed earlier, Docker follows the naming convention to identify unofficial images. What we are creating is an unofficial image. Hence, it should follow that syntax. According to that naming convention, the unofficial image name should be named as follows:
<username>/<image_name>:<tag_name>
. In my case, I need to rename it asgauravvv/example_image:latest
Publish the image
1
$ docker push gauravvv/example_image:latest
shellUpload your tagged image to the repository using the
docker push
command. Once complete, you can see the image there on Docker Hub. That's it; you have successfully published your Docker image. If you want to test out your image, use the below command and launch a container from it:and launch a container from it:1
2$ docker pull gauravvv/example_image:latest
$ docker run -it gauravvv/example_image:latestshell
Bonus: Pushing to a Non-Docker Hub Registry
So far, we have learned how to create a new Docker image, tag it, and push it to the Docker Hub. While using Kubernetes or OpenShift, we would use the internally hosted Docker registry; some organizations also prefer to host their own internal private registry. Therefore, you should know how to publish your Docker image to an internally hosted Docker registry. Luckily, Docker made it more accessible, just like publishing to Docker Hub but a little bit different.
Step 1: Log in to a Non-Docker Hub Registry
Use the
docker login
command to log in to the specified registry and to tell Docker which registry we want to sign in to.1
$ docker login registry.example.com
shellStep 2: Tag the Image
As you may remember, for publishing our image to Docker Hub, we have tagged the image to include the username at first. Now, to publish to the internal registry, continuing on that pattern, we will also add the registry name at first, followed by a username, then followed by image name. Therefore, the final syntax will look like this:
<registry>/<username>/<image_name>:<tag>
. Let's take a look at an example:1
$ docker tag gauravvv/example_image registry.example.com/gauravvv/example_image
shellStep 3: Push the Image
The
docker push
command takes the name of the image. It will know where to push our Docker image by looking at the image name because the name contains the registry location. By looking at our image name, which isregistry.example.com/gauravvv/example_image
, it will determine that we are pushing an image name,example_image
, to a user's repository,gauravvv
, at theregistry registry.example.com
.1
$ docker push registry.example.com/gauravvv/example_image
No comments:
Post a Comment