Sub topic 7
Starting and restarting containers
This chapter covers bringing stopped containers back to life with docker start and docker restart. It assumes that Docker is installed and the daemon is running. It also assumes you know the difference between an image and a container, and that you can list and stop containers with docker ps and docker stop from the earlier chapters.
Start reuses a container, run creates one
A stopped container is not gone. Docker keeps its configuration and its writable layer on disk until you remove it. docker start takes an existing container in the created or exited state and runs its main process again.
docker run is a shortcut for two steps: docker create followed by docker start. Knowing this split explains most of the behavior in this chapter.
| Command | Needs an existing container? | What it does |
|---|---|---|
| `docker run` | No | Creates a new container from an image, then starts it |
| `docker create` | No | Creates a container but does not start it |
| `docker start` | Yes | Starts a `created` or `exited` container |
| `docker restart` | Yes | Stops the container if it is running, then starts it |
Starting a container
The basic form takes one or more names or IDs. This example creates a container without starting it, then starts it:
docker create --name web -p 8080:80 nginx:alpine
docker ps -a --filter name=web
docker start web
docker ps --filter name=webThe first docker ps shows the status Created. After docker start web, the status becomes Up. docker start prints the name you gave it on success.
You can pass several containers in one command:
docker start web db cacheDocker starts them one after another, in the order you list them. If one fails, the others are still attempted. The command prints an error for the failure and exits with a non-zero status.
Starting a container that is already running does nothing. Docker prints the name and exits with status 0, so the command is safe to repeat in scripts.
Start is detached by default
docker run attaches your terminal to the container unless you pass -d. docker start does the opposite. It starts the container in the background and returns immediately, even if the container was originally created with docker run -it.
Two flags change that:
-a(--attach) attaches your terminal to the container's standard output and standard error. It also forwards signals such as Ctrl+C to the container.-i(--interactive) keeps standard input open, so you can type into the container.
An interactive shell is the usual case:
docker run -it --name sandbox alpine sh
# inside the container:
# echo "hello" > /note.txt
# exit
docker start -ai sandbox
# inside the container again:
# cat /note.txt
# exitThe file /note.txt is still there after the restart. The container kept its writable layer, so files you wrote while it ran are still present.
The -i flag only helps if the container was created with a TTY or stdin open (docker run -it or docker create -it). A container created without -t will not give you a proper terminal just because you add flags at start time.
What start keeps and what it cannot change
Starting a container runs it with the exact configuration it was created with. This includes:
- The image it was created from (the container stays on that image version)
- The command and entrypoint
- Environment variables
- Port mappings
- Volume and bind mounts
- Network attachments and the restart policy
You cannot pass new -e, -p, or -v options to docker start. To change those, remove the container and run a new one. A few settings, such as the restart policy and resource limits, can be changed on an existing container with docker update.
The main process starts again from the beginning. Anything held only in memory is lost, but anything on disk in the container's writable layer or in a volume remains. This example shows it:
docker run --name counter alpine sh -c 'echo "run" >> /log.txt; cat /log.txt'
docker start -a counter
docker start -a counterThe first command prints one line. The next docker start -a prints two lines, and the third prints three, because the same /log.txt file keeps growing. Remove the container when you are done:
docker rm counterRestarting a container
docker restart combines stop and start in one command:
docker restart webIf the container is running, Docker sends it SIGTERM, waits for the grace period, sends SIGKILL if it is still alive, and then starts it again. If the container is already stopped, restart just starts it.
Use -t (--timeout) to set the grace period in seconds, as with docker stop:
docker restart -t 30 webA restart is the usual way to make a service reread configuration files that live in a mounted volume or bind mount. It does not pick up a newer image. The container is tied to the image it was created from, so pulling nginx:alpine again and restarting changes nothing. To use the new image you have to remove the container and create a new one:
docker pull nginx:alpine
docker stop web
docker rm web
docker run -d --name web -p 8080:80 nginx:alpineWhat changes between runs
Some details are not preserved across a stop and start:
- Process IDs and memory. The main process is new, so it has a fresh PID and empty memory.
- IP address. On the default bridge network, Docker may assign a different IP address when the container starts. Use container names on user-defined networks instead of hard-coded IPs.
- Randomly assigned host ports. If you published ports with
-Por-p 80(no host port), Docker may pick different host ports after a restart. Check withdocker port web. Fixed mappings like-p 8080:80stay the same. - Start time and uptime.
docker psreports the time since the latest start.
The container ID, name, and writable layer do not change.
Letting Docker restart containers for you
A restart policy tells the Docker daemon to restart a container automatically. Set it when you create the container with --restart:
| Policy | Behavior |
|---|---|
| `no` | Never restart automatically (the default) |
| `on-failure[:max]` | Restart only if the container exits with a non-zero status, optionally up to `max` times |
| `always` | Restart whenever the container stops, and start it again when the daemon starts |
| `unless-stopped` | Like `always`, but not if you stopped the container manually before the daemon restarted |
docker run -d --name api --restart unless-stopped nginx:alpine
docker inspect -f '{{.HostConfig.RestartPolicy.Name}}' api
docker update --restart on-failure:3 api
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}}' apiThe first inspect prints unless-stopped. The second prints the new policy name and how many times Docker has restarted the container automatically (RestartCount).
Docker adds a delay between automatic restarts that grows after repeated failures, starting at 100 ms and doubling each time. A container that crashes in a loop does not spin the CPU at full speed, but it will keep trying. A manual docker stop does not trigger a restart under any policy until you start the container again or the daemon restarts, and with always the container does come back after a daemon restart.
Common problems
The container starts and exits immediately
Run docker ps -a. A container with status Exited (0) finished its command normally. One with a non-zero code failed. Read its output:
docker logs web
docker inspect -f '{{.State.ExitCode}} {{.State.Error}}' webCommon exit codes:
0: the command finished without error.1or other small numbers: the application reported an error.125,126,127: Docker could not run the command, the command was not executable, or it was not found.137: the process was killed with SIGKILL (128 + 9), often bydocker kill, a timeout duringdocker stop, or the out-of-memory killer.143: the process ended after SIGTERM (128 + 15).
A container whose main process is a shell with no TTY, such as docker run alpine, exits immediately because the shell has nothing to read. That is expected. Start it with docker start -ai only if it was created with -it.
Port is already allocated
If another process or container uses the same host port, docker start fails with an error such as port is already allocated or address already in use. Find the conflicting container with docker ps --filter publish=8080, stop it, or recreate your container with a different host port. You cannot change a published port on an existing container.
Name or ID is not found
No such container means the name is wrong or the container was removed. Check docker ps -a. Remember that docker ps without -a hides stopped containers, so a container you want to start will not appear there.
Changes to the image are not visible
As described above, a restart reuses the old image. If the code inside the container looks stale, recreate the container rather than restarting it.
Putting it together
A typical workflow for a development database looks like this:
docker run -d --name devdb -e POSTGRES_PASSWORD=secret -v devdata:/var/lib/postgresql/data postgres:16
docker stop devdb
docker start devdb
docker restart devdb
docker logs --tail 5 devdbThe named volume devdata holds the data. The container can be stopped, started, and restarted without losing it, and even removing the container leaves the volume in place.
Exercise
You ran docker run -d --name app -p 8080:80 -e MODE=dev nginx:alpine, then stopped it. You want it to run with MODE=prod. Can docker start -e MODE=prod app do this? What should you do instead?
Show answer
No. docker start has no -e option, and a container keeps the environment it was created with. Remove the container with docker rm app and create a new one with docker run -d --name app -p 8080:80 -e MODE=prod nginx:alpine. Data kept in volumes survives this, but anything in the old container's writable layer is lost.
Exercise
docker restart web completes with no error, but the site still serves an old version after you ran docker pull for the newer image. Why, and how do you fix it?
Show answer
A container is bound to the image it was created from, and restarting reuses that same container. The pull only updated the local image tag. Stop and remove the container, then run a new one from the pulled image, for example docker stop web, docker rm web, docker run -d --name web -p 8080:80 nginx:alpine.
Exercise
A container shows Exited (137) in docker ps -a. What does the code tell you, and which command would you run next to look for a cause?
Show answer
Exit code 137 is 128 + 9, so the process was killed by SIGKILL. That happens after docker kill, when docker stop ran out of its grace period, or when the kernel's out-of-memory killer ended the process. Run docker logs <name> and docker inspect -f '{{.State.OOMKilled}}' <name> to see the last output and whether memory limits caused it.