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.
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.
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.
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 -ltnlists the ports in use. Because the address is127.0.0.1, nothing is opened to your network. Nothing is deleted except the containers this lab creates. To undo everything, rundocker rm -f hello hello2 once.
1. Run hello-app with a published port and a greeting.
docker run -d --name hello -p 127.0.0.1:8080:8000 -e GREETING=Shalom hello-app:1.1
docker psExplain this command
docker run -d --name hello -p 127.0.0.1:8080:8000 -e GREETING=Shalom hello-app:1.1
docker psdocker runCreate and start a new container.-dRun it in the background and print its ID.--name helloName the container hello.-p 127.0.0.1:8080:8000Publish a port. Host address 127.0.0.1, host port 8080, container port 8000. Only programs on this machine can connect.-e GREETING=ShalomSet the environment variable GREETING inside the container. It overrides the image's ENV GREETING=Hello.hello-app:1.1The image from part 2.docker psList 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.
curl http://127.0.0.1:8080/
curl http://127.0.0.1:8080/about
docker port helloExplain this command
curl http://127.0.0.1:8080/
curl http://127.0.0.1:8080/about
docker port hellocurl http://127.0.0.1:8080/Send an HTTP GET request to the published port and print the answer.curl http://127.0.0.1:8080/aboutAnother path. hello-app answers every path the same way.docker port helloList 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.
docker logs hello
docker logs --timestamps -n 1 helloExplain this command
docker logs hello
docker logs --timestamps -n 1 hellodocker logs helloPrint everything the container's main process wrote to standard output and standard error.--timestampsPut Docker's time stamp, in UTC, in front of each line.-n 1Show 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.
time docker stop hello
docker ps -a --filter name=hello
docker logs -n 2 helloExplain this command
time docker stop hello
docker ps -a --filter name=hello
docker logs -n 2 hellotimeShell keyword. Runs the command and prints how long it took.docker stop helloSend SIGTERM to the container's main process, wait up to 10 seconds, then send SIGKILL if it is still running.docker ps -aList all containers, including stopped ones.--filter name=helloShow only containers whose name matches hello.docker logs -n 2 helloThe 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.
docker start hello
curl http://127.0.0.1:8080/Explain this command
docker start hello
curl http://127.0.0.1:8080/docker start helloStart a stopped container again, with the same ID, settings and writable layer.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.
docker rm hello
docker rm -f helloExplain this command
docker rm hello
docker rm -f hellodocker rm helloRemove a container. It must be stopped, so this fails while hello runs.docker rm -f helloForce. 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:
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 helloExplain 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 hellodocker run -d --name hello -p 127.0.0.1:8080:8000 hello-app:1.1Starts hello on host port 8080 again, this time with the image's default greeting.docker run -d --name hello2 -p 127.0.0.1:8080:8000 hello-app:1.1Tries to start a second container on the same host port. Docker creates it, then fails to start it.docker ps -a --filter name=hello2 --format '{{.Names}} {{.Status}}'Shows hello2 and its state. --format prints only the fields named in the template.docker rm hello2Removes the container that never started.docker rm -f helloStops 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.
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=onceExplain 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=oncedocker run -d --rmRun in the background and remove the container automatically when it exits.--name onceThe container name.-p 127.0.0.1:8081:8000Publish container port 8000 on host port 8081.-e GREETING=HiSet the greeting for this container only.hello-app:1.1The image.curl http://127.0.0.1:8081/Ask it for a page on the new port.docker stop onceStop it. Because of --rm, Docker then removes it.docker ps -a --filter name=onceThe 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:
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 withinitExplain 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 withinitdocker run -d --name plain python:3.14-slim python -m http.server 8000Run Python's built-in web server directly as PID 1. It has no SIGTERM handler.time docker stop plainTime how long docker stop takes. Here it waits the full 10-second grace period.docker ps -a --filter name=plain --format '{{.Names}} {{.Status}}'Show the exit status. 137 means the process was killed by SIGKILL.docker run -d --init --name withinit python:3.14-slim python -m http.server 8000The same server, started under docker-init.--initRun an init process as PID 1 that passes signals on to your command and reaps zombie processes.time docker stop withinitNow SIGTERM reaches Python and the container stops at once.docker rm plain withinitRemove 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_PORTpublishes a port. Without a host IP it is open on every address, and it bypassesufwandfirewalld; this series uses127.0.0.1.-esets an environment variable for one container and overrides the image’sENV.docker logsshows what PID 1 writes to stdout and stderr, until the container is removed.- A stopped container keeps its settings and writable layer;
docker startreuses it anddocker rmdeletes it. docker stopsendsSIGTERM, waits 10 seconds, then sendsSIGKILL. PID 1 must handleSIGTERMitself, 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.

The Campfire
CommentsThanks. Your comment is waiting for the desk. Once it is approved it appears here within about a minute.
Add a comment