Build a Docker Container#

In the Say Hello to Docker lab you ran a pre-packaged Docker application. One of the great things about Docker is that there are so many good packaged apps. In this lab you’ll build your own container image. Container images are built from a base image. The base image is a starting point that you can customize. Any Docker image can be a base image (you can even start from scratch).

This lab will help you remember the uses of these Docker commands:

Command

Use

docker build

Build a container image from a Dockerfile

docker image ls

View container images that are present on your machine

docker run

Create a new container from an image

docker start

Start a container that was created with docker run but has been stopped or exited

docker ps

View running (and stopped with -a) containers

docker rm

Remove containers created with docker run

Create a Python Application#

Let’s start by creating a very simple Python application. Create a file called app.py with the following contents:

print("Hello Docker World")

Next we’ll package the application.

Create a Dockerfile#

The Dockerfile is a recipe for building a Docker container.

FROM docker.io/python:3.10 

COPY app.py /
CMD python3 /app.py 

The FROM line selects a base image. We’re using the official Python base image, which is a great fit for our Python app. There are many other images we could use too. The COPY line copies our application into the root directory of the container. The CMD line tells Docker what command to run in our container.

Build Your Container#

Run this command in the directory with your Dockerfile.

$ docker build -t mysite . 

The -t mysite names your container image. After you build you should see the name when you run docker image ls. Since you didn’t specify a build tag the latest tag was used.

Verify that your image was built correctly:

$ docker image ls
REPOSITORY                                      TAG                                        IMAGE ID       CREATED          SIZE
mysite                                          latest                                     37a10006319e   18 seconds ago   917MB
...

Notice that mysite is present.

Run Your Container#

Now that you have a packaged application you can run it like this:

$  docker run -it mysite
Hello Docker World

The -it flag connects the terminal so we can see the output of app.py. The container exits but it still exits. We can see it with docker ps. Run the command:

$ docker ps -a
CONTAINER ID   IMAGE                                 COMMAND                  CREATED         STATUS                     PORTS     NAMES
dc48253924ab   mysite                                "/bin/sh -c 'python3…"   6 seconds ago   Exited (0) 5 seconds ago             pensive_leakey

Notice that your container got a funny name (e.g. pensive_leakey). Names are automatically generated unless you use the --name option to docker run. We can start the container again:

$ docker start -i pensive_leakey
Hello Docker World

Our simple application isn’t like most Dockerized applications, it exits immediately and never listens for network connections. The next application we build will be more typical.

Clean Up#

Delete the container from docker run like this:

$ docker rm pensive_leakey