This step covers why web stopped with code 0 and how to keep it running.
A container runs one main process — the command from docker run or the image's command. While it runs, the container is Up. Once the process ends, the container stops, and the brackets after Exited hold its exit code: 0 means the command finished without an error.
Docker keeps everything the main process prints, for a stopped container too. You read it with docker logs <name>:
web has no output: httpd printed nothing.
The bad container was created with sudo docker run -d --name bad alpine sh -c 'echo starting; cat /etc/app.conf'. docker ps -a shows it as Exited (1). What will sudo docker logs bad print?
-
The container log gets both process streams, output and errors: the
catmessage about the file is there too. -
A running container is needed to start a new command inside it.
docker logsstarts nothing — it reads the saved output. -
Docker keeps the main process output after it stops:
docker logsworks for a container in theExitedstate too.
docker logs shows everything the main process wrote to its output and error stream, for a stopped container too. Here sh printed starting and cat did not find the file — hence code 1.
By default httpd goes to the background like a classic daemon: it starts a copy of itself and exits. For Docker that is the end of the main process: the container stops, and the background copy stops with it. Such servers have an option that keeps them in the foreground. You can read the httpd help in a temporary container:
--rm deletes the container as soon as its command has finished. The stopped web still holds its name: a second docker run --name web fails with a Conflict error. docker rm web frees the name.
Keep the server running
Start the web container again so that it keeps running: the same busybox image, the same httpd server on port 8080 inside the container.
Goals · 0 of 2
- Start a web container that does not stop not done yet
- The server inside answers on port 8080 not done yet