Build an image and view its history
In this step, you will learn how to build a Docker image from a Dockerfile and view its history. The history shows the layers that make up the image and the commands used to create each layer.
First, let's create a simple Dockerfile in your ~/project
directory. This Dockerfile will create an image based on the ubuntu
image and install the curl
package.
cd ~/project
nano Dockerfile
Add the following content to the Dockerfile
:
FROM ubuntu:latest
RUN apt-get update && apt-get install -y curl
Save the file and exit the editor (Ctrl+X, Y, Enter).
Now, let's build the Docker image using the docker build
command. We will tag the image with the name my-ubuntu-curl
and the tag latest
. The .
at the end of the command indicates that the Dockerfile is in the current directory.
docker build -t my-ubuntu-curl:latest .
You will see output indicating the build process, showing each step defined in the Dockerfile being executed.
After the build is complete, you can view the history of the image using the docker history
command.
docker history my-ubuntu-curl:latest
The output will show a table with information about each layer, including the layer ID, creation time, size, and the command used to create it. This history is useful for understanding how an image was built and for debugging purposes.