Difficulty: Beginner
Estimated Time: 10 minutes

In the eariler scenario, we discussed how you could 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 flask python server using ubuntu.

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.

In this scenario we covered how to write a Dockerfile to define how to build and configure your image to run as a Docker container. A Dockerfile is key to creating repeatable images that can define how your applications are configured and deployed in development and production.

Don’t stop now! The next scenario will only take about 10 minutes to complete.

Docker Images

Step 1 of 6

Step 1 - 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. For example, in this scenario, we require ubuntu to be configured and running on the system before we can deploy our flask server. As such we want to use ubuntu as our base image.

Dockerfile's are simple text files with a command on each line. To define a base image we use the instruction FROM :

Task: Building a Dockerfile

To complete this step you need to execute the following code:

FROM ubuntu

Caution

It's tempting to use the tag :latest however this can result in you building your image against a version which you were not expecting. We recommend that you always use a particular version number as your tag and manage the updating yourself.

Dockerfile