In this topic, you will take your first step in learning the fundamentals of Docker containers. You will create a simple Dockerfile and understand how to build images with it. After which you will learn to find those images and explore their attributes. In the end, you will understand what an image name is and how to set and update it.
Creating a simple Dockerfile
To launch a container you need an image. Similarly, if you want to launch a customized container you need to create an image. The common format to describe image content is by creating a Dockerfile. To create a Dockerfile; open any editor, and create a file. Fill in the commands from the snippet below, and save the file by naming it Dockerfile. Using this Dockerfile, you will create a simple Ubuntu 22.04 image.
FROM ubuntu:22.04
LABEL author=HyperUser
ENTRYPOINT ["/bin/bash"]The first line is quite intuitive. You specify the Ubuntu version you want to use when building the image. The next two lines may not be clear at first glance, but don't worry. Later on, you will find out what their purpose is.
Further in the topic, you'll build the first image that you can run and get a container.
Building an image
Now that you have the Dockerfile, all you need to do is run it and you will get the required image. You can accomplish this task via a docker build command. However, it has different flags and options for different scenarios. For now, let's build a simple image using the docker build .. This is the bare minimum yet functional command. Here, the . indicates the build context which is a group of files or folders located in the specified PATH or URL that will be available to the Docker engine. In this case, Docker understands that the Dockerfile is in the current working directory. However, you can also specify another path using the following script:
docker build -f /path/to/my/Dockerfile .If it is the first time you are building a particular image, the build process starts by downloading the required components. If you already have the base image, Docker will use it. When the image is built successfully, you'll see the following output:
Sending build context to Docker daemon 12.8kB
Step 1/3 : FROM ubuntu:22.04
22.04: Pulling from library/ubuntu
5887826a0d8b: Pull complete
Digest: sha256:898852f07de8ff57e7810a5c28553e71f10db60ae79c2b7a06acc45bbb03741a
Status: Downloaded newer image for ubuntu:22.04
---> 2793294e6a90
Step 2/3 : LABEL author=HyperUser
Removing intermediate container 68bafd1194fb
---> 4f0e66ff57d5
Step 3/3 : ENTRYPOINT ["/bin/bash"]
Removing intermediate container 9baebdc3a08a
---> 97ac6dc27e4f
Successfully built 97ac6dc27e4fAs you can see, the build process included three steps each corresponding to the three lines of our Dockerfile. The last line shows Successfully built 97ac6dc27e4f. The final string of the last line indicates the resulting image ID.
Exploring the image
Docker provides you the functionality to list the images you build. If you run the docker images command, you'll see an output similar to the one shown below.
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 97ac6dc27e4f 28 seconds ago 70.2MB
ubuntu 22.04 63f3a805b672 6 days ago 126MBIt lists your images but shows only one built image on the first line since we don't have any other. In this output, there are two columns that show <none>. This is due to the fact that you didn't mention the name of the image when building it. The name of an image consists of its repository and its tag.
You may ask yourself why there are two images in the output if you created only one image. Here, the second line shows the official Ubuntu image that was used to build our image. Now, let's move to the next section and learn how to specify the image name.
Specifying image name
Before you learn to set the image name, let's look at its two main components:
A repository — A place where you store different versions of your image.
A tag — An alias of your Docker image ID. It is used to specify the version of your Docker image.
Image name can also include a username and a hostname. They are necessary when working with Docker Hub and networks accordingly. You can read more about it in the official documentation. For now, we won't focus on these attributes.
To set the image name, you need to use the -t flag combined with the repository and tag name in the [repository]:[tag] format. If you want to set your repository name to ubuntu with the tag v1, the command to do so will look like this:
docker build -t ubuntu:v1 .Let's apply it and check the result with the docker images command. This time, after you set the image name, the docker images command will not list our first image whose id was 97ac6dc27e4f. To output all images regardless of whether you assigned them a name or not, use the docker images -a command. Later you will understand its purpose in detail.
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 22.04 d50d2365accc 4 seconds ago 127MB
ubuntu v1 c47d2c2e680e 3 minutes ago 127MBNote that if you don't specify the tag name it will be set to latest automatically. To check it, let's run docker build -t ubuntu ..
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 7009d4503cec 16 seconds ago 128MB
ubuntu 22.04 d50d2365accc 4 seconds ago 127MB
ubuntu v1 c47d2c2e680e 3 minutes ago 127MBActually, you can set two or more tags at once. For instance, docker build -t ubuntu:v1 -t ubuntu:latest . command will build two instances of the same image with the tags; v1 and latest.
You can also change the name of the existing image. Previously you built an image without a name. Let's fix this issue by renaming our previous image to ubuntu:v0. Copy its ID from the docker images -a output. Run the docker image tag <id> ubuntu:v0 command, and check the result. In this case, the command will be docker image tag 97ac6dc27e4f ubuntu:v0.
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 7009d4503cec 16 seconds ago 128MB
ubuntu 22.04 d50d2365accc 4 seconds ago 127MB
ubuntu v1 c47d2c2e680e 3 minutes ago 127MB
ubuntu v0 7009d4503cec 2 minutes ago 128MBCongratulations! You renamed an old image. If you closely look at the IDs of our images; both old and new, you will see that the two images are the same but have different tags.
Conclusion
You now know how to create Dockerfiles. Along with this, you can build images from respective Dockerfiles using the docker build command. Docker also allows you to list the images you build. You can do so using the docker images command. Using this information, you are able to find images you built and identify them. The docker images command also allows you to change the name of an existing image. You can use docker image tag <image-ID> <new-repository>:<new-tag> command to do so. This was just an introduction to working with Docker images. Docker has many more commands to operate with images.
At this point, you are one step away from running a container. For this task, you can use the docker run command with its different modes. But let's save this for another day.