HOWTO · Docker
How to Restart a Docker Container
Learn when to use docker restart or docker start, how to check a container's state, and why a container may exit immediately.
On this page
Use docker restart <container> to restart a container, including a running one. Use docker start <container> to start a container that has stopped. The commands act on an existing container; docker run creates a container from an image. The examples below use an Nginx container named web-app so you can follow the state changes with the same container.
Restart a Docker Container
A restart stops and starts the existing container, keeping its configuration and writable container layer. Starting a stopped container with docker start does not recreate it. Choose the command for the action you need: use docker restart web-app to restart it, or docker start web-app when it is stopped and you only need to start it.
For a small, reproducible example of the command used in this article, run:
container_name = "web-app"
command = ["docker", "restart", container_name]
print(" ".join(command))
docker restart web-app
This Python snippet prints a command string; it does not contact Docker or change a container. To perform the restart, run the printed Docker command in a terminal with access to the Docker daemon.
Prepare a Running Container
First, download the Nginx image with docker pull nginx:alpine. Create a detached container named web-app and publish host port 8000 to container port 80 with docker run --name web-app -d -p 8000:80 nginx:alpine. The --name option gives the container a stable name for later commands, -d runs it in the background, and -p 8000:80 maps the host port to Nginx’s container port.
Check the running containers with docker ps. To include stopped containers in the listing, use docker ps -a. The listing includes a container ID, image, status, published ports, and name; for this example, look for the web-app name and an Up status. IDs, elapsed times, and exact status wording vary by run.
Restart a Running Container
Run docker restart web-app to restart the Nginx container. Docker prints the container name when the command completes; the exact timing and status shown by later inspection can differ. Verify that it is running with docker ps, or inspect its state directly with docker inspect --format '{{.State.Status}}' web-app. The latter reports a state such as running when the container is up.
Start a Stopped Container
Stop the example with docker stop web-app. Confirm that it remains in Docker’s container list and is stopped by running docker ps -a; its status will include Exited. Start that same container with docker start web-app, then check it with docker ps or the inspection command above. docker start starts the existing container and uses its configured command. It does not create a new container or change the image. If you want to replace a container, remove it and create another with docker run instead.
Why a Container Exits Immediately
A container stays running only while its main process is running. For example, docker run --name temp-web-app nginx:alpine pwd starts the image’s pwd process as the container’s main process. That process prints the working directory and exits, so the container stops. The precise directory in the output depends on the image; the command and output here are illustrative and have not been run in this article workflow. Because -p 8080:80 is unnecessary for this one-shot command, omit it; port publishing does not keep a container alive.
Use docker ps -a to find temp-web-app and see that it exited. Running docker restart temp-web-app starts the same configured process again, so it exits again when pwd finishes. A restart does not replace the image’s command. For a service that should remain available, use an image or command whose main process runs the service in the foreground, and check its logs with docker logs temp-web-app when diagnosing why it stopped.