2.1. Containers

In this lab, you run a ready-made container, then build and run an image with a Python script of your own.

Step 1: Run a Container

Start the web server nginx in a container named web:

docker run -d --name web -p 8080:80 nginx:1.29

Docker prints the ID of the new container, a long random string. If the image isn’t on your machine yet, Docker downloads it first.

Open http://localhost:8080 in your browser. You see the page Welcome to nginx!. If it doesn’t load yet, wait a moment and reload it.

Port 8080 is already in use

Remove the container with docker rm web. Then repeat the command with another port on the left, e.g. -p 8081:80, and open that port.

Explanation

docker run created a container from the image nginx:1.29 and started it. nginx is the name of the image and 1.29 is its tag, usually the version.

FlagEffect
-dRuns the container in the background
--name webNames the container, so you can refer to it in other commands
-p 8080:80Forwards port 8080 of your machine to port 80 of the container

Step 2: Manage the Container

List the running containers:

docker ps
CONTAINER ID   IMAGE        COMMAND                   CREATED         STATUS         PORTS                                     NAMES
ba1559cf69e7   nginx:1.29   "/docker-entrypoint..."   2 seconds ago   Up 2 seconds   0.0.0.0:8080->80/tcp, [::]:8080->80/tcp   web

If you run other containers, you see more lines. Read the logs of web:

docker logs web
...
/docker-entrypoint.sh: Configuration complete; ready for start up
...
172.65.247.105 - - [22/Nov/2026:17:51:36 +0000] "GET / HTTP/1.1" 200 896 "-" "curl/8.7.1" "-"

The last lines are requests to the page. Yours come from your browser, so their addresses and details differ. Stop the container:

docker stop web
web

Remove it:

docker rm web
web

Explanation

docker logs shows what the process in the container printed. docker stop ends that process, but the stopped container still exists, with its logs and files, until docker rm removes it. The image stays on your machine, so the next container from it starts without a download.


Step 3: Write a Script and a Dockerfile

Create a directory for the files of this lab and change into it:

mkdir -p ~/labs/2.1
cd ~/labs/2.1

Create the file hello.py in ~/labs/2.1 with your editor or IDE and the following content:

print("hello")

Create the file Dockerfile (without a file extension) in the same directory:

FROM python:3.14-slim
WORKDIR /app
COPY hello.py .
CMD ["python", "hello.py"]

Explanation

A Dockerfile is the recipe for an image. Docker works through it from top to bottom:

InstructionEffect
FROMStarts from an image that already contains Python
WORKDIRCreates the directory /app and changes into it
COPYCopies the script from your machine into the image
CMDSets the command that a container runs when it starts

Step 4: Build the Image

Build an image named hello with the tag 1.0 from your Dockerfile. The first build downloads the Python image, which can take a moment:

docker build -t hello:1.0 .

The output contains these lines, with more details around them:

...
 => [2/3] WORKDIR /app
 => [3/3] COPY hello.py .
...
 => => naming to docker.io/library/hello:1.0
...

Explanation

-t sets the name and the tag of the image. The . at the end is the directory that Docker builds from: it finds the files Dockerfile and hello.py there. The image now contains Python and your script, so it runs on any machine with Docker and the same CPU architecture, without installing Python.


Step 5: Run Your Image

Run a container named hello from the image hello:1.0, in the foreground and without a port:

docker run --name hello hello:1.0
hello

List all containers, including the stopped ones:

docker ps -a
CONTAINER ID   IMAGE       COMMAND             CREATED         STATUS                     PORTS     NAMES
c32ed80420e2   hello:1.0   "python hello.py"   2 seconds ago   Exited (0) 2 seconds ago             hello

Explanation

A container runs as long as its main process. nginx waits for requests until you stop it, but your script ends after printing hello, so its container exits. The 0 in Exited (0) is the exit code of the script: it ended without an error. -a lists stopped containers as well, which docker ps hides.


Optional Tasks

Finished early? Try these tasks. The following labs don’t depend on them. Each task lists the commands you need. Open the solution if you get stuck.

Task 1: Look Around

Start an nginx container named look, open a shell in it and find the files of the page Welcome to nginx! in /usr/share/nginx/html.

CommandEffect
docker exec -it <container> shOpens a shell inside a running container
exitLeaves the shell, the container runs on
Solution

Start the container:

docker run -d --name look nginx:1.29

Open a shell in it:

docker exec -it look sh

List the files at the prompt of the container:

ls /usr/share/nginx/html
50x.html  index.html

Leave the shell:

exit

The container has a filesystem of its own, which comes from the image. These files don’t exist on your machine.

Task 2: Short Memory

Create the file /tmp/note in an nginx container named memory. Replace the container with a new one from the same image and look for the file.

CommandEffect
docker exec <container> <command>Runs a command inside a running container
docker rm -f <container>Stops and removes a container in one command
Solution

Start a container and create the file in it:

docker run -d --name memory nginx:1.29
docker exec memory touch /tmp/note

Replace the container:

docker rm -f memory
docker run -d --name memory nginx:1.29

Look for the file:

docker exec memory ls /tmp/note
ls: cannot access '/tmp/note': No such file or directory

What a container writes belongs to this one container and is gone with it. A new container starts with the files of the image.

Task 3: Cache Hit

Let the script print hello again, build it as hello:1.1 and find out which step Docker didn’t repeat. Then run the new image.

CommandEffect
docker run --rm <image>Removes the container as soon as it exits
Solution

Change hello.py in ~/labs/2.1:

print("hello again")

Build the new tag:

docker build -t hello:1.1 ~/labs/2.1
...
 => CACHED [2/3] WORKDIR /app
 => [3/3] COPY hello.py .
...

Run it:

docker run --rm hello:1.1
hello again

Every instruction that changes the files creates a layer of the image. Docker reused the layers up to WORKDIR (CACHED) and repeated only the steps from the first change on, here COPY.


Clean Up

Remove the containers of this lab, including those of the optional tasks. -f also removes running containers:

docker rm -f web hello look memory

Remove the images you built. rmi removes images, and -f forces the removal:

docker rmi -f hello:1.0 hello:1.1

Ignore errors about containers or images that don’t exist.


Complete files

~/labs/2.1/hello.py:

print("hello")

~/labs/2.1/Dockerfile:

FROM python:3.14-slim
WORKDIR /app
COPY hello.py .
CMD ["python", "hello.py"]