Build a simple image and view its history
In this step, we will learn how to build a simple Docker image and view its history. Building a Docker image involves creating a Dockerfile
, which is a text file that contains all the commands a user could call on the command line to assemble an image.
First, navigate to the ~/project
directory if you are not already there.
cd ~/project
Now, let's create a simple Dockerfile
. We will create a file named Dockerfile
in the ~/project
directory.
nano Dockerfile
Add the following content to the Dockerfile
:
FROM ubuntu:latest
RUN echo "Hello, Docker!" > /app/hello.txt
CMD ["cat", "/app/hello.txt"]
This Dockerfile
does the following:
FROM ubuntu:latest
: This line specifies the base image for our new image. We are using the latest version of the Ubuntu image from Docker Hub.
RUN echo "Hello, Docker!" > /app/hello.txt
: This line executes a command during the image build process. It creates a directory /app
and writes the text "Hello, Docker!" into a file named hello.txt
inside that directory.
CMD ["cat", "/app/hello.txt"]
: This line specifies the default command to run when a container is started from this image. It will execute the cat /app/hello.txt
command, which will print the content of the hello.txt
file.
Save the file and exit the nano editor (Press Ctrl + X
, then Y
, then Enter
).
Now, let's build the Docker image using the docker build
command. We will tag the image with the name my-hello-image
and the tag latest
. The .
at the end of the command indicates that the Dockerfile
is located in the current directory.
docker build -t my-hello-image:latest .
You will see output indicating the build process, showing each step being executed.
After the image is built, you can view the history of the image using the docker history
command. This command shows the layers that make up the image and the commands that were used to create each layer.
docker history my-hello-image:latest
The output will show a table with information about each layer, including the layer ID, the command used, the creation time, and the size. This history is useful for understanding how an image was built and for debugging build issues.