Sub topic 7
Starting and Restarting Containers
This chapter assumes 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 containers with docker ps. Here you will bring stopped containers back, and learn what a restart does and does not preserve.
Start versus run
docker run creates a new container from an image and starts it. docker start does something different. It starts a container that already exists and is stopped. It does not create anything.
This matters because a stopped container keeps its identity. Its name, ID, configuration (ports, environment variables, mounts) and its writable layer all survive a stop. docker start brings back that same container with those settings.
Begin with a container that has been stopped:
docker run -d --name web nginx:alpine
docker stop web
docker ps -aThe web container now shows a status such as Exited (0) 3 seconds ago. Start it again:
docker start web
docker psdocker start prints the name or ID you gave it. The container returns to the running state with the same name, ID and settings.
How start works
When you run docker start web, Docker runs the container's original command again. That command comes from the image's CMD or ENTRYPOINT, or from whatever you passed to docker run. You cannot change the command, ports, or environment at this point. Those were fixed when the container was created.
If you need different settings, create a new container with docker run.
The diagram below shows the states involved.
Starting several containers
docker start accepts more than one name or ID:
docker start web db cacheEach container starts independently. If one fails, Docker reports an error for it and continues with the others.
To start every stopped container, combine it with a filtered listing. The -q flag prints only IDs:
docker start $(docker ps -aq --filter "status=exited")Be careful with this. It starts every exited container, including ones you stopped on purpose or forgot about.
Attaching to output
By default docker start runs the container in the background and returns right away. Two flags change that:
-a(attach) connects your terminal to the container's output and signals.-i(interactive) keeps standard input open, so you can type into the container.
docker start -ai mycontainerThis is useful for a container created with docker run -it, such as a shell. Starting it with -ai puts you back at the prompt. Without -a, the shell starts, finds no terminal attached, and may exit again.
Restarting
docker restart stops a container and starts it again in one command:
docker restart webIt works on a running container and on a stopped one. For a running container it sends the same stop signal as docker stop, waits for the grace period, then starts it. The -t option sets that wait in seconds:
docker restart -t 30 webA restart is useful when an application reads its configuration only at startup, or when a process has hung. It is not a way to apply new docker run options.
What a restart keeps and resets
| Item | After stop and start or restart |
|---|---|
| Name and ID | Kept |
| Port mappings, environment variables, mounts | Kept |
| Files in the writable layer | Kept |
| Data in volumes | Kept |
| Running processes and memory | Lost, the main process starts fresh |
| Container IP address | May change |
Restart policies
Sometimes you want Docker to start containers for you, for example after a crash or a host reboot. Set a restart policy when you create the container:
docker run -d --name web --restart unless-stopped nginx:alpineCommon values are no (the default), on-failure, always, and unless-stopped. With unless-stopped, Docker restarts the container after a crash or daemon restart, but not if you stopped it manually. You can change the policy on an existing container with docker update --restart unless-stopped web.
Common errors
No such container: web: the container was removed, or the name is wrong. Checkdocker ps -a. A removed container cannot be started. Usedocker run.Conflict. The container name "/web" is already in use: this comes fromdocker run --name webwhen a stopped container with that name exists. Eitherdocker start webor remove the old one first.- The container starts and exits at once: the main process finished or crashed. Read the output with
docker logs web. port is already allocated: another process or container uses the published host port.
Exercise
You ran docker run -d --name api -p 8080:80 nginx:alpine, then docker stop api. You now want the container back on the same port 8080. Which command do you use, and why is docker run --name api -p 8080:80 nginx:alpine the wrong choice?
Show answer
Use docker start api. The stopped container still exists with its port mapping, so it returns with 8080:80. Running docker run --name api ... tries to create a second container with the same name, which fails with a name conflict error.
Exercise
A running container app reads its settings file only at startup. You edited the file inside a bind mount. Which single command applies the change, and what happens to the processes inside?
Show answer
docker restart app. Docker sends the stop signal, waits for the grace period, and starts the container again. The main process starts fresh and reads the new file. The old processes and their memory are lost.