How to Restart a Container in Docker: Simple Commands
To restart a Docker container, use the
docker restart [container_name_or_id] command. This stops and then starts the container immediately, applying any changes or refreshing its state.Syntax
The basic syntax to restart a Docker container is:
docker restart [OPTIONS] CONTAINER
Where:
CONTAINERis the container's name or ID you want to restart.OPTIONScan include-tto specify the timeout in seconds before killing the container.
bash
docker restart [OPTIONS] CONTAINER
Example
This example shows how to restart a running container named my_app. The command stops the container and starts it again immediately.
bash
docker restart my_app
Output
my_app
Common Pitfalls
Common mistakes when restarting containers include:
- Using the wrong container name or ID, which causes an error.
- Not waiting for the container to fully stop before starting dependent services.
- Assuming
docker restartupdates the container image; it only restarts the existing container.
Always verify the container name with docker ps before restarting.
bash
docker restart wrong_name # Error: No such container: wrong_name docker restart my_app # Correct usage restarts the container named my_app
Output
Error response from daemon: No such container: wrong_name
my_app
Quick Reference
Here is a quick summary of useful commands related to restarting containers:
bash
docker ps # Lists running containers docker restart my_app # Restarts container named my_app docker stop my_app # Stops container docker start my_app # Starts container
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
123abc456def nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours my_app
my_app
my_app
my_app
Key Takeaways
Use
docker restart [container_name_or_id] to quickly stop and start a container.Verify container names with
docker ps before restarting to avoid errors.Restarting a container does not update its image; recreate the container to apply image changes.
Use the
-t option to set a timeout before forcefully stopping the container.Restarting helps refresh container state without manual stop and start commands.