Difficulty: Beginner
Estimated Time: 10 minutes

Build your first Docker image step by step and see what's stored in it.

  • Commit a container as an image
  • Start a container from an image
  • Dig into layers

In real life, building an image is seldom done in the way we did. Docker provides an automatic building mechanism.

Details of automatic build is covered in the next scenario.

II. Images: Demystified

Step 1 of 5

1. Create your first image

Start a shell in a container that doesn't die immediately: docker run -ti alpine:latest ash

Mind the change of the prompt, you're inside the container now.

Let's make some changes in there, for instance, create a user: adduser -D johndoe

We should see Jonh in our /etc/shadow: grep johndoe /etc/shadow

If everything looks fine, exit the shell (exit). This will put the container to stopped status, shown as Exited if you run docker ps -l

Now that we have an ID of the container where we have just made some changes, a docker image can be created as such:

CONTAINER_ID=$(docker ps -lq) docker commit ${CONTAINER_ID} johnsimage

What we just did was "consolidating" the changes we made in that container into a new image, and naming it johnsimage,

To list all images available to the Docker daemon, run docker images. You should see alpine and johnsimage sit besides each other.

Pay attention to the size difference of the two images.