0
0
Dockerdevops~3 mins

Restarting containers in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes a container stops working correctly or needs to apply new settings. Restarting a container stops it and starts it again, fixing temporary issues or applying changes without deleting it.
When a web server container becomes unresponsive and you want to refresh it quickly.
When you update environment variables and want the container to use the new values.
When a container crashes and you want to try running it again without recreating it.
When you want to apply new network settings to a running container.
When you want to clear temporary glitches in a container without losing its data.
Commands
List all running containers to find the container ID or name you want to restart.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b7c9f1a2d3e4 nginx:1.23.3 "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:8080->80/tcp my-nginx
Restart the container named 'my-nginx'. This stops and then starts the container again.
Terminal
docker restart my-nginx
Expected OutputExpected
my-nginx
Verify that the container 'my-nginx' is running again after the restart.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b7c9f1a2d3e4 nginx:1.23.3 "/docker-entrypoint.…" 2 hours ago Up 5 seconds 0.0.0.0:8080->80/tcp my-nginx
Key Concept

If you remember nothing else from this pattern, remember: restarting a container quickly fixes temporary problems by stopping and starting it without deleting it.

Common Mistakes
Trying to restart a container using its image name instead of its container name or ID.
Docker restart requires the container identifier, not the image name, so the command fails.
Use the container name or ID shown by 'docker ps' to restart the container.
Expecting 'docker restart' to apply changes made to the Dockerfile or image automatically.
Restarting a container does not update the container image; it only restarts the existing container instance.
Rebuild the image and recreate the container to apply image changes.
Summary
Use 'docker ps' to find the container you want to restart.
Run 'docker restart container_name' to stop and start the container again.
Verify the container is running with 'docker ps' after restarting.