Difficulty: beginner
Estimated Time: 30-35 minutes

In the first scenario, we discussed how you can start containers based on pre-existing images from the Docker Registry. This scenario explains how to build an image based on your requirements.

For this scenario, the container will be running a static HTML application using Nginx, a high-performance web server. In future, we'll explain how to deploy other stacks such as Node.js or Java.

Docker Images

Docker images are built based on a Dockerfile. A Dockerfile defines all the steps required to create a Docker image with your application configured and ready to be run as a container. The image itself contains everything, from operating system to dependencies and configuration required to run your application.

Having everything within the image allows you to migrate images between different environments and be confident that if it works in one environment, then it will work in another.

The Dockerfile allows for images to be composable, enabling users to extend existing images instead of building from scratch. By building on an existing image, you only need to define the steps to setup your application. The base images can be basic operating system installations or configured systems which simply need some additional customisations.

To help you complete the steps, an environment has been created with Docker configured. The editor allows you to write a Dockerfile which defines how to build the Docker image.

Now that you have an understanding of the projects you will use throughout this course, let's get started!

Building Container Images

Step 1 of 3

Base Images

All Docker images start from a base image. A base image is the same images from the Docker Registry which are used to start containers. Along with the image name, we can also include the image tag to indicate which particular version we want, by default, this is latest. These base images are used as the foundation for your additional changes to run your application.

What is an image?

  • Image = files + metadata
  • These files form the root filesystem of our container.
  • The metadata can indicate a number of things, e.g.:
    • the author of the image
    • the command to execute in the container when starting it
    • environment variables to be set
    • etc.
  • Images are made of layers, conceptually stacked on top of each other.
  • Each layer can add, change, and remove files and/or metadata.
  • Images can share layers to optimize disk usage, transfer times, and memory use.

Pull new image

To pull an ubuntu image from Docker official register, run the following command:

docker pull ubuntu

To see the list of Docker images on the system, you can issue the following command:

docker images

From the above output, you can see that the server has one image which is ubuntu. Each image has the following attributes:

  • TAG − This is used to logically tag images.
  • Image ID − This is used to uniquely identify the image.
  • Created − The number of days since the image was created.
  • Virtual Size − The size of the image.

Inspect the image

The command docker inspect <image name:tag> provides more details about the downloaded image including Environment variables, the cmd that runs when the container created, etc.

docker inspect ubuntu:latest

Take a moment to inspect the output of the command, and feel free to ask for assistance in understanding the output.