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 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.
| Flag | Effect |
|---|---|
-d | Runs the container in the background |
--name web | Names the container, so you can refer to it in other commands |
-p 8080:80 | Forwards port 8080 of your machine to port 80 of the container |
Step 2: Manage the Container
List the running containers:
If you run other containers, you see more lines. Read the logs of web:
The last lines are requests to the page. Yours come from your browser, so their addresses and details differ. Stop the container:
Remove it:
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:
Create the file hello.py in ~/labs/2.1 with your editor or IDE and the following content:
Create the file Dockerfile (without a file extension) in the same directory:
Explanation
A Dockerfile is the recipe for an image. Docker works through it from top to bottom:
| Instruction | Effect |
|---|---|
FROM | Starts from an image that already contains Python |
WORKDIR | Creates the directory /app and changes into it |
COPY | Copies the script from your machine into the image |
CMD | Sets 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:
The output contains these lines, with more details around them:
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:
List all containers, including the stopped ones:
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.
| Command | Effect |
|---|---|
docker exec -it <container> sh | Opens a shell inside a running container |
exit | Leaves the shell, the container runs on |
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.
| Command | Effect |
|---|---|
docker exec <container> <command> | Runs a command inside a running container |
docker rm -f <container> | Stops and removes a container in one command |
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.
| Command | Effect |
|---|---|
docker run --rm <image> | Removes the container as soon as it exits |
Clean Up
Remove the containers of this lab, including those of the optional tasks. -f also removes running containers:
Remove the images you built. rmi removes images, and -f forces the removal:
Ignore errors about containers or images that don’t exist.