Sub topic 5
Running the container and publishing ports
This chapter assumes you have built the myapp:1.0 image from the previous chapter's Dockerfile, that Docker is installed, and that you know what an HTTP port is. Here you start a container from that image and make it reachable from your machine.
From image to running container
An image is a read-only template. docker run creates a container from it by adding a thin writable layer and starting the image's CMD as the container's main process. When that process exits, the container stops.
docker build -t myapp:1.0 .
docker run --rm -d -p 3000:3000 --name myapp myapp:1.0The flags in the run command:
| Flag | Meaning |
|---|---|
| `--rm` | Remove the container automatically when it stops. |
| `-d` | Detached mode. Run in the background and print the container ID. |
| `-p 3000:3000` | Publish a port, in the form `HOST:CONTAINER`. |
| `--name myapp` | Give the container a fixed name instead of a random one. |
Why publishing is needed
Each container has its own network namespace. The EXPOSE 3000 line in the Dockerfile only documents which port the app listens on. It does not open anything. Without -p, nothing on your host can reach the container.
With -p 3000:3000, Docker forwards traffic arriving at port 3000 on the host to port 3000 inside the container.
Test it:
curl http://localhost:3000Choosing different host ports
The two numbers are independent. If port 3000 is already used on your host, change only the left side:
docker run --rm -d -p 8080:3000 --name myapp myapp:1.0
curl http://localhost:8080The app still listens on 3000 inside the container. Only the host side changed.
To publish on the loopback interface only, so other machines on your network cannot connect, prefix the host address:
docker run --rm -d -p 127.0.0.1:3000:3000 --name myapp myapp:1.0Inspecting and stopping
docker ps
docker port myapp
docker stop myappdocker ps lists running containers. docker port myapp shows the active port mappings. docker stop sends SIGTERM to the main process, waits ten seconds by default, then sends SIGKILL. Because of --rm, the stopped container is deleted.
Common errors
port is already allocated: another process or container already uses that host port. Pick a different host port or stop the other one.The container name "/myapp" is already in use: an older container has that name. Rundocker rm -f myapp, or use--rmso stopped containers do not linger.- Connection reset or empty reply from
curl: the app is listening on127.0.0.1inside the container. Loopback inside a container is not reachable from outside it. Make the server listen on0.0.0.0. In Express, callapp.listen(3000, "0.0.0.0"). - Connection refused right after start: the app may still be booting. Check
docker logs myappbefore assuming a mapping problem.
Exercise
A container's app listens on port 5000. You want to reach it at http://localhost:9000 on your machine, and only from your machine. Which -p value do you use?
Show answer
-p 127.0.0.1:9000:5000. The host side (9000) comes first, the container side (5000) second, and the 127.0.0.1 prefix restricts the binding to the loopback interface.
Exercise
You run docker run -d -p 3000:3000 myapp:1.0, but curl localhost:3000 returns an empty reply. The logs show "listening on 127.0.0.1:3000". What is wrong and how do you fix it?
Show answer
The server is bound to the container's loopback interface, so forwarded traffic cannot reach it. Change the app to listen on 0.0.0.0 (for example app.listen(3000, "0.0.0.0")), rebuild the image, and run it again.