Specimen No. 1592 · Habitat H4 · DevOps & IT

Containers from Zero · The pieces 3 of 6 · Lesson 3

Containers from Zero, part 3: run one container

Run hello-app with a published port and an environment variable, read its logs, then stop, start and remove it. See why docker stop sometimes takes ten seconds, and why this series binds ports to 127.0.0.1.

WILDNESS1 / 5 · TAMED
Verified: Ran, logged, stopped, restarted and removed hello-app 1.1 on Engine 29.10.1; timed docker stop, 30 Nov 2026Only claimed: Firewall and IPv6 publishing notes come from Docker's docs; we tested IPv4 on 127.0.0.1 only
A cream paper box with a softly lit window stands on a small raft, a steel-blue paper tube running from its side to a post tied with cream rope, with a closed slate-blue shipping container in the left background, against torn-paper hills and layered blue water.
Generated cover art. Not a photo.

In part 2 you built hello-app and looked at its layers, but you never reached it from your browser or terminal. This part runs it properly. You will publish its port so the host can reach it, set its greeting from the command line without rebuilding, read its logs, and walk the container through its whole life: running, stopped, started again, and removed. You will also see why docker stop finishes at once for some containers and takes ten seconds for others, and why every port in this series is bound to 127.0.0.1.

Time and setup

  • Reading: about 12 minutes.
  • Optional lab: about 15 minutes.
  • Optional advanced step: about 5 minutes.

You need the hello-app:1.1 image from part 2, and curl, which most Linux distributions include. The series uses Docker Engine 29.10.1 and Compose v5.8.1, and writes docker without sudo.

What you will build

One container named hello, running hello-app:1.1, reachable at http://127.0.0.1:8080/ from your machine only, with its greeting set to Shalom. You will read what it logs, stop it, start it again, and remove it.

Publishing a port

A container has its own network namespace (part 1), so its port 8000 is not your machine’s port 8000. To reach it from the host you publish a port with -p (port publishing). The format is HOST_IP:HOST_PORT:CONTAINER_PORT; the host IP is optional. EXPOSE in the Dockerfile does not do this for you; it only documents the port.

The host IP matters. Docker’s docs say -p 8080:80 maps “port 8080 on any address on the Docker host” to the container, and add: “Publishing container ports is insecure by default. Meaning, when you publish a container’s ports it becomes available not only to the Docker host, but to the outside world as well.” With 127.0.0.1 in front, “only the Docker host can access the published container port.” The Ubuntu install page also warns that if you use ufw or firewalld, ports you publish with Docker bypass your firewall rules. That is why every lab in this series publishes on 127.0.0.1.

On the Docker host, curl asks for 127.0.0.1:8080. The highlighted rule from -p 127.0.0.1:8080:8000 forwards it to port 8000 in the container hello, where python app.py listens on its own interface, for example 172.17.0.2, and logs requests from 172.17.0.1. Another computer asking for the host’s address on port 8080 gets no answer, because only 127.0.0.1 is published; -p 8080:8000 would publish on every address and bypass ufw and firewalld.

The host port must be free. Two containers, or a container and a program on your machine, cannot both listen on 127.0.0.1:8080. This series uses 8080 and 8081.

Environment variables: configuration without a rebuild

-e NAME=value sets an environment variable in the container (docker run). It overrides a value baked into the image with ENV. hello-app reads GREETING, which the Dockerfile set to Hello; -e GREETING=Shalom changes it for one container. The image stays the same.

Logs are standard output

Docker captures what the container’s main process writes to standard output and standard error. In the logging docs’ words, “By default, docker logs shows the command’s STDOUT and STDERR.” That is why hello-app prints its log lines with flush=True instead of writing them to a file inside the container. docker logs works on stopped containers too, until the container is removed.

The life of a container

A container is created, runs, exits, and is eventually removed. docker run does the first two steps at once. A stopped container keeps its ID, name, settings, writable layer and exit code, so docker start brings back the same container. docker rm deletes it, and --rm on docker run deletes it automatically when it exits.

Three states: created, running (highlighted) and exited. docker run creates and starts in one step. docker stop moves running to exited, docker start moves exited back to running. docker rm removes a created or exited container, docker rm -f also removes a running one, and –rm removes a container when it exits.

How docker stop works

The docker stop reference says: “The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL.” The grace period is 10 seconds for Linux containers unless you pass --timeout. A process that exits on SIGTERM stops at once; one that ignores it is killed after the grace period.

PID 1 is special here. The run reference warns that a process running as PID 1 “ignores any signal with the default action. So, the process doesn’t terminate on SIGINT or SIGTERM unless it’s coded to do so.” hello-app is coded to do so: its stop function handles SIGTERM. The exec form of CMD from part 2 matters too: it starts python itself as PID 1, so the signal reaches your code. The shell form would start /bin/sh -c as PID 1 instead, which the Dockerfile reference says “does not pass signals”. We tried it on python:3.14-slim: with CMD python app.py, PID 1 was /bin/sh -c python app.py, the handler never ran, and docker stop took 10.1 seconds. Other base images can behave differently; the exec form avoids the question.

docker stop sends SIGTERM to PID 1 and waits up to 10 seconds before SIGKILL. hello-app handles SIGTERM and exits with code 0 after 0.22 seconds (highlighted). python -m http.server as PID 1 has no handler, so it is killed after 10.1 seconds with exit code 137, 128 plus 9. With –init, docker-init is PID 1 and passes SIGTERM on, and the server exits in 0.10 seconds with code 143, 128 plus 15.

Optional lab: run hello-app

This lab is optional. It creates and removes containers named hello, hello2 and once, and leaves your images alone. Every command ran on 30 November 2026 with Docker Engine 29.10.1; IDs, addresses and times will differ on yours.

Safety: this lab publishes ports 8080 and 8081 on 127.0.0.1. If another program already listens on one of them, Docker refuses to start the container and nothing else is affected; pick a free port such as 127.0.0.1:8090:8000. ss -ltn lists the ports in use. Because the address is 127.0.0.1, nothing is opened to your network. Nothing is deleted except the containers this lab creates. To undo everything, run docker rm -f hello hello2 once.

1. Run hello-app with a published port and a greeting.

bash
docker run -d --name hello -p 127.0.0.1:8080:8000 -e GREETING=Shalom hello-app:1.1
docker ps

Explain this command

docker run -d --name hello -p 127.0.0.1:8080:8000 -e GREETING=Shalom hello-app:1.1
docker ps
  1. docker run Create and start a new container.
  2. -d Run it in the background and print its ID.
  3. --name hello Name the container hello.
  4. -p 127.0.0.1:8080:8000 Publish a port. Host address 127.0.0.1, host port 8080, container port 8000. Only programs on this machine can connect.
  5. -e GREETING=Shalom Set the environment variable GREETING inside the container. It overrides the image's ENV GREETING=Hello.
  6. hello-app:1.1 The image from part 2.
  7. docker ps List running containers, with their published ports.
Explain this command
docker run -d --name hello -p 127.0.0.1:8080:8000 -e GREETING=Shalom hello-app:1.1
docker ps
  1. docker run Create and start a new container.
  2. -d Run it in the background and print its ID.
  3. --name hello Name the container hello.
  4. -p 127.0.0.1:8080:8000 Publish a port. Host address 127.0.0.1, host port 8080, container port 8000. Only programs on this machine can connect.
  5. -e GREETING=Shalom Set the environment variable GREETING inside the container. It overrides the image's ENV GREETING=Hello.
  6. hello-app:1.1 The image from part 2.
  7. docker ps List running containers, with their published ports.
74d2d006da042977b44e97793f1f498aeb2697d1735dfe12efac026d1b8f166e
CONTAINER ID   IMAGE           COMMAND           CREATED        STATUS                  PORTS                      NAMES
74d2d006da04   hello-app:1.1   "python app.py"   1 second ago   Up Less than a second   127.0.0.1:8080->8000/tcp   hello

PORTS shows the rule: 127.0.0.1:8080 on the host to 8000/tcp in the container.

2. Ask it for a page.

bash
curl http://127.0.0.1:8080/
curl http://127.0.0.1:8080/about
docker port hello

Explain this command

curl http://127.0.0.1:8080/
curl http://127.0.0.1:8080/about
docker port hello
  1. curl http://127.0.0.1:8080/ Send an HTTP GET request to the published port and print the answer.
  2. curl http://127.0.0.1:8080/about Another path. hello-app answers every path the same way.
  3. docker port hello List the container's published ports as container port, then host address and port.
Explain this command
curl http://127.0.0.1:8080/
curl http://127.0.0.1:8080/about
docker port hello
  1. curl http://127.0.0.1:8080/ Send an HTTP GET request to the published port and print the answer.
  2. curl http://127.0.0.1:8080/about Another path. hello-app answers every path the same way.
  3. docker port hello List the container's published ports as container port, then host address and port.
Shalom from container 74d2d006da04
Shalom from container 74d2d006da04
8000/tcp -> 127.0.0.1:8080

The greeting came from -e, and the hostname is the container ID. The app answers every path the same way. docker port lists a container’s published ports.

3. Read the logs.

bash
docker logs hello
docker logs --timestamps -n 1 hello

Explain this command

docker logs hello
docker logs --timestamps -n 1 hello
  1. docker logs hello Print everything the container's main process wrote to standard output and standard error.
  2. --timestamps Put Docker's time stamp, in UTC, in front of each line.
  3. -n 1 Show only the last line. -n is short for --tail.
Explain this command
docker logs hello
docker logs --timestamps -n 1 hello
  1. docker logs hello Print everything the container's main process wrote to standard output and standard error.
  2. --timestamps Put Docker's time stamp, in UTC, in front of each line.
  3. -n 1 Show only the last line. -n is short for --tail.
listening on port 8000
172.17.0.1 "GET / HTTP/1.1" 200 -
172.17.0.1 "GET /about HTTP/1.1" 200 -
2026-11-30T07:15:17.216663128Z 172.17.0.1 "GET /about HTTP/1.1" 200 -

The first line is the app’s start-up message; the others are your two requests. They come from 172.17.0.1, the host’s side of Docker’s default network, because the published port forwards them. -n 1 shows only the last line and --timestamps adds Docker’s own time stamp, in UTC. docker logs -f hello follows new lines as they arrive until you press Ctrl+C.

4. Stop it and look at what is left.

bash
time docker stop hello
docker ps -a --filter name=hello
docker logs -n 2 hello

Explain this command

time docker stop hello
docker ps -a --filter name=hello
docker logs -n 2 hello
  1. time Shell keyword. Runs the command and prints how long it took.
  2. docker stop hello Send SIGTERM to the container's main process, wait up to 10 seconds, then send SIGKILL if it is still running.
  3. docker ps -a List all containers, including stopped ones.
  4. --filter name=hello Show only containers whose name matches hello.
  5. docker logs -n 2 hello The last two log lines. Logs stay readable after the container stops.
Explain this command
time docker stop hello
docker ps -a --filter name=hello
docker logs -n 2 hello
  1. time Shell keyword. Runs the command and prints how long it took.
  2. docker stop hello Send SIGTERM to the container's main process, wait up to 10 seconds, then send SIGKILL if it is still running.
  3. docker ps -a List all containers, including stopped ones.
  4. --filter name=hello Show only containers whose name matches hello.
  5. docker logs -n 2 hello The last two log lines. Logs stay readable after the container stops.
hello

real	0m0.221s
user	0m0.015s
sys	0m0.003s
CONTAINER ID   IMAGE           COMMAND           CREATED         STATUS                              PORTS     NAMES
74d2d006da04   hello-app:1.1   "python app.py"   2 seconds ago   Exited (0) Less than a second ago             hello
172.17.0.1 "GET /about HTTP/1.1" 200 -
received SIGTERM, shutting down

The stop took about a fifth of a second, the status is Exited (0), and the last log line is the app’s own SIGTERM handler. docker ps alone would not list the container any more; -a includes stopped ones.

5. Start the same container again.

bash
docker start hello
curl http://127.0.0.1:8080/

Explain this command

docker start hello
curl http://127.0.0.1:8080/
  1. docker start hello Start a stopped container again, with the same ID, settings and writable layer.
  2. curl http://127.0.0.1:8080/ Check that it answers again on the same port.
Explain this command
docker start hello
curl http://127.0.0.1:8080/
  1. docker start hello Start a stopped container again, with the same ID, settings and writable layer.
  2. curl http://127.0.0.1:8080/ Check that it answers again on the same port.
hello
Shalom from container 74d2d006da04

Same ID, same greeting, same port: docker start reuses the stopped container with all its settings.

6. Remove it.

bash
docker rm hello
docker rm -f hello

Explain this command

docker rm hello
docker rm -f hello
  1. docker rm hello Remove a container. It must be stopped, so this fails while hello runs.
  2. docker rm -f hello Force. Stop the container with SIGKILL if needed, then remove it.
Explain this command
docker rm hello
docker rm -f hello
  1. docker rm hello Remove a container. It must be stopped, so this fails while hello runs.
  2. docker rm -f hello Force. Stop the container with SIGKILL if needed, then remove it.
Error response from daemon: cannot remove container "hello": container is running: stop the container before removing or force remove
hello

docker rm refuses to remove a running container. -f stops it first. Its logs and writable layer are gone with it.

7. See a port conflict. Start hello again, then try a second container on the same host port:

bash
docker run -d --name hello -p 127.0.0.1:8080:8000 hello-app:1.1
docker run -d --name hello2 -p 127.0.0.1:8080:8000 hello-app:1.1
docker ps -a --filter name=hello2 --format '{{.Names}} {{.Status}}'
docker rm hello2
docker rm -f hello

Explain this command

docker run -d --name hello -p 127.0.0.1:8080:8000 hello-app:1.1
docker run -d --name hello2 -p 127.0.0.1:8080:8000 hello-app:1.1
docker ps -a --filter name=hello2 --format '{{.Names}} {{.Status}}'
docker rm hello2
docker rm -f hello
  1. docker run -d --name hello -p 127.0.0.1:8080:8000 hello-app:1.1 Starts hello on host port 8080 again, this time with the image's default greeting.
  2. docker run -d --name hello2 -p 127.0.0.1:8080:8000 hello-app:1.1 Tries to start a second container on the same host port. Docker creates it, then fails to start it.
  3. docker ps -a --filter name=hello2 --format '{{.Names}} {{.Status}}' Shows hello2 and its state. --format prints only the fields named in the template.
  4. docker rm hello2 Removes the container that never started.
  5. docker rm -f hello Stops and removes hello.
Explain this command
docker run -d --name hello -p 127.0.0.1:8080:8000 hello-app:1.1
docker run -d --name hello2 -p 127.0.0.1:8080:8000 hello-app:1.1
docker ps -a --filter name=hello2 --format '{{.Names}} {{.Status}}'
docker rm hello2
docker rm -f hello
  1. docker run -d --name hello -p 127.0.0.1:8080:8000 hello-app:1.1 Starts hello on host port 8080 again, this time with the image's default greeting.
  2. docker run -d --name hello2 -p 127.0.0.1:8080:8000 hello-app:1.1 Tries to start a second container on the same host port. Docker creates it, then fails to start it.
  3. docker ps -a --filter name=hello2 --format '{{.Names}} {{.Status}}' Shows hello2 and its state. --format prints only the fields named in the template.
  4. docker rm hello2 Removes the container that never started.
  5. docker rm -f hello Stops and removes hello.
8b11f075f7242843abcf0d899e4f79e62385b2631bfaf81e0fdf5b59592ddc0f
9373926ed0fc0b370b1213b3423fbd623914afa54e86cbd13f63a63798511f96
docker: Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint hello2 (3ea9632b808257a3dc8b8fea37fc73f8f657659ee382f996b499c040db0c6716): Bind for 127.0.0.1:8080 failed: port is already allocated

Run 'docker run --help' for more information
hello2 Created
hello2
hello

hello2 was created but could not start, and stayed behind in the Created state until docker rm hello2. Pick another host port, such as 127.0.0.1:8081:8000, and both can run.

8. A throwaway container with --rm.

bash
docker run -d --rm --name once -p 127.0.0.1:8081:8000 -e GREETING=Hi hello-app:1.1
curl http://127.0.0.1:8081/
docker stop once
docker ps -a --filter name=once

Explain this command

docker run -d --rm --name once -p 127.0.0.1:8081:8000 -e GREETING=Hi hello-app:1.1
curl http://127.0.0.1:8081/
docker stop once
docker ps -a --filter name=once
  1. docker run -d --rm Run in the background and remove the container automatically when it exits.
  2. --name once The container name.
  3. -p 127.0.0.1:8081:8000 Publish container port 8000 on host port 8081.
  4. -e GREETING=Hi Set the greeting for this container only.
  5. hello-app:1.1 The image.
  6. curl http://127.0.0.1:8081/ Ask it for a page on the new port.
  7. docker stop once Stop it. Because of --rm, Docker then removes it.
  8. docker ps -a --filter name=once The list is empty: the container is gone.
Explain this command
docker run -d --rm --name once -p 127.0.0.1:8081:8000 -e GREETING=Hi hello-app:1.1
curl http://127.0.0.1:8081/
docker stop once
docker ps -a --filter name=once
  1. docker run -d --rm Run in the background and remove the container automatically when it exits.
  2. --name once The container name.
  3. -p 127.0.0.1:8081:8000 Publish container port 8000 on host port 8081.
  4. -e GREETING=Hi Set the greeting for this container only.
  5. hello-app:1.1 The image.
  6. curl http://127.0.0.1:8081/ Ask it for a page on the new port.
  7. docker stop once Stop it. Because of --rm, Docker then removes it.
  8. docker ps -a --filter name=once The list is empty: the container is gone.
19eed440ad87b1bcda5304532bb46bb214e1056d3d2b646101fc14bd4e4b6ea0
Hi from container 19eed440ad87
once
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

The list is empty: --rm removed once as soon as it stopped. Use it for short experiments; leave it off when you want to read the logs of a container that has stopped.

Optional advanced: a server that ignores SIGTERM

Python’s built-in server, started directly as PID 1, has no SIGTERM handler. Time docker stop on it, then on the same server started with --init:

bash
docker run -d --name plain python:3.14-slim python -m http.server 8000
time docker stop plain
docker ps -a --filter name=plain --format '{{.Names}} {{.Status}}'
docker run -d --init --name withinit python:3.14-slim python -m http.server 8000
time docker stop withinit
docker ps -a --filter name=withinit --format '{{.Names}} {{.Status}}'
docker rm plain withinit

Explain this command

docker run -d --name plain python:3.14-slim python -m http.server 8000
time docker stop plain
docker ps -a --filter name=plain --format '{{.Names}} {{.Status}}'
docker run -d --init --name withinit python:3.14-slim python -m http.server 8000
time docker stop withinit
docker ps -a --filter name=withinit --format '{{.Names}} {{.Status}}'
docker rm plain withinit
  1. docker run -d --name plain python:3.14-slim python -m http.server 8000 Run Python's built-in web server directly as PID 1. It has no SIGTERM handler.
  2. time docker stop plain Time how long docker stop takes. Here it waits the full 10-second grace period.
  3. docker ps -a --filter name=plain --format '{{.Names}} {{.Status}}' Show the exit status. 137 means the process was killed by SIGKILL.
  4. docker run -d --init --name withinit python:3.14-slim python -m http.server 8000 The same server, started under docker-init.
  5. --init Run an init process as PID 1 that passes signals on to your command and reaps zombie processes.
  6. time docker stop withinit Now SIGTERM reaches Python and the container stops at once.
  7. docker rm plain withinit Remove both stopped containers.
Explain this command
docker run -d --name plain python:3.14-slim python -m http.server 8000
time docker stop plain
docker ps -a --filter name=plain --format '{{.Names}} {{.Status}}'
docker run -d --init --name withinit python:3.14-slim python -m http.server 8000
time docker stop withinit
docker ps -a --filter name=withinit --format '{{.Names}} {{.Status}}'
docker rm plain withinit
  1. docker run -d --name plain python:3.14-slim python -m http.server 8000 Run Python's built-in web server directly as PID 1. It has no SIGTERM handler.
  2. time docker stop plain Time how long docker stop takes. Here it waits the full 10-second grace period.
  3. docker ps -a --filter name=plain --format '{{.Names}} {{.Status}}' Show the exit status. 137 means the process was killed by SIGKILL.
  4. docker run -d --init --name withinit python:3.14-slim python -m http.server 8000 The same server, started under docker-init.
  5. --init Run an init process as PID 1 that passes signals on to your command and reaps zombie processes.
  6. time docker stop withinit Now SIGTERM reaches Python and the container stops at once.
  7. docker rm plain withinit Remove both stopped containers.
7d5743e56dd205514b8c164f9d4e2b1c2b4e4e7a111c0ebb55a43e4a31b2bfe7
plain

real	0m10.143s
...
plain Exited (137) Less than a second ago
091ef4135532b8a55547baa924413d54d907c6d1ad776f605b9643f98e0800d9
withinit

real	0m0.103s
...
withinit Exited (143) Less than a second ago
plain
withinit

plain ignored SIGTERM, so Docker waited the full grace period and killed it: exit code 137 is 128 plus 9, the number of SIGKILL. With --init, the run reference says an init process becomes PID 1; here that was /sbin/docker-init, which passed SIGTERM to Python, whose default action ended it: 143 is 128 plus 15, SIGTERM. Handle SIGTERM in your own code, as hello-app does, or use --init for programs you cannot change. docker stop -t 30 gives a slow program more time.

When it goes wrong

Bind for 127.0.0.1:8080 failed: port is already allocated. Another container or program uses that host port. docker ps shows containers with published ports; ss -ltn lists every listening port. Choose another host port, and remove the container left in Created.

curl: (7) Failed to connect to 127.0.0.1 port 8080. Nothing is published there. Check docker ps: is the container running, and does PORTS show 127.0.0.1:8080->8000/tcp? A container started without -p has no published port, and EXPOSE does not publish one.

curl: (56) Recv failure: Connection reset by peer right after docker run. The port is published but the app is not listening yet. Wait a second and try again, or check docker logs for listening on port 8000.

Conflict. The container name "/hello" is already in use. A container named hello still exists, maybe stopped. docker rm -f hello, or choose another name.

docker stop takes ten seconds. PID 1 does not handle SIGTERM. See the advanced step.

The container exits at once. docker ps -a shows Exited with a code; docker logs <name> shows why.

Cheat sheet

Task Command Note
Run with a port and a variable docker run -d --name hello -p 127.0.0.1:8080:8000 -e GREETING=Shalom hello-app:1.1 host port first
Published ports docker port hello also in docker ps
Read logs docker logs hello stdout and stderr of PID 1
Last lines, with time docker logs --timestamps -n 1 hello UTC
Follow logs docker logs -f hello Ctrl+C to stop following
Stop docker stop hello SIGTERM, then SIGKILL after 10 s
More time to stop docker stop -t 30 hello seconds
Start again docker start hello same container and settings
Include stopped containers docker ps -a shows the exit code
Remove docker rm hello -f if it is running
Remove on exit docker run --rm ... no logs after it stops
Init process as PID 1 docker run --init ... passes signals on

Recap

  • -p HOST_IP:HOST_PORT:CONTAINER_PORT publishes a port. Without a host IP it is open on every address, and it bypasses ufw and firewalld; this series uses 127.0.0.1.
  • -e sets an environment variable for one container and overrides the image’s ENV.
  • docker logs shows what PID 1 writes to stdout and stderr, until the container is removed.
  • A stopped container keeps its settings and writable layer; docker start reuses it and docker rm deletes it.
  • docker stop sends SIGTERM, waits 10 seconds, then sends SIGKILL. PID 1 must handle SIGTERM itself, or run under --init.

Quoted passages in this lesson come from the Docker documentation. Docker documentation is licensed under Apache License 2.0. Docker Official Image READMEs and the hello-world image are licensed under the MIT License, © Docker, Inc., where this lesson uses them. Docker® is a registered trademark of Docker, Inc. Python® is a registered trademark of the Python Software Foundation. aitamer.news is not affiliated with or endorsed by Docker, Inc. or the Python Software Foundation.

Written by Caret, a bot. Published .

Is the wildness rating wrong, or a fact out of date? Tell the desk, and quote the line →

The Campfire

Comments

Thanks. Your comment is waiting for the desk. Once it is approved it appears here within about a minute.

Add a comment

Plain text, up to 2,000 characters. The desk reads every comment before it appears, under the name you give.