How to Use Docker Compose Restart Command
Use the
docker compose restart [SERVICE] command to stop and then start one or more services defined in your docker-compose.yml file. If you omit SERVICE, all services will restart. This command helps apply changes or recover services without rebuilding containers.Syntax
The basic syntax of the docker compose restart command is:
docker compose restart [SERVICE...]: Restarts specified services.- If no
SERVICEis provided, all services in the Compose file restart.
This command stops the container(s) and then starts them again immediately.
bash
docker compose restart [SERVICE...]
Example
This example shows how to restart a specific service and all services using Docker Compose.
bash
version: '3.8' services: web: image: nginx:alpine ports: - "8080:80" db: image: postgres:alpine environment: POSTGRES_PASSWORD: example # Restart only the web service $ docker compose restart web # Restart all services $ docker compose restart
Output
Restarting web ... done
Restarting web ... done
Restarting db ... done
Common Pitfalls
Common mistakes when using docker compose restart include:
- Trying to restart a service name that does not exist in the Compose file, which causes an error.
- Expecting
restartto rebuild images or apply configuration changes; it only restarts containers. - Using the old
docker-composecommand instead of the newerdocker compose(note the space) which is the current recommended syntax.
bash
Wrong:
$ docker compose restart unknown_service
# Error: No such service: unknown_service
Right:
$ docker compose restart web
# Restarts the 'web' service containerQuick Reference
| Command | Description |
|---|---|
| docker compose restart | Restart all services defined in docker-compose.yml |
| docker compose restart web | Restart only the 'web' service |
| docker compose restart db web | Restart 'db' and 'web' services |
| docker compose restart unknown | Error if 'unknown' service does not exist |
Key Takeaways
Use
docker compose restart [SERVICE] to stop and start containers quickly.Omitting the service name restarts all services in the Compose file.
Restarting does not rebuild images or apply config changes; use
docker compose up --build for that.Always use the modern
docker compose syntax, not the legacy docker-compose.Check service names carefully to avoid errors when restarting.