How to Restart a Single Container with Docker Compose
TLDR
To restart a single container in a Docker Compose setup, use the docker compose restart <service_name> command. This allows you to restart a specific service without disrupting others.
Docker Compose simplifies managing multi-container applications. However, there are times when you need to restart just one container without affecting the rest. Here's how to do it.
Step 1: Identify the Service Name
The service name is defined in your compose.yaml file. Locate the service you want to restart. Originally, this file is named docker compose.yml, but it can also be named compose.yaml in newer versions which is the recommended format.
Example compose.yaml
services:
web:
image: nginx
ports:
- '80:80'
db:
image: postgres
environment:
POSTGRES_PASSWORD: example
In this example, the service names are web and db.
Step 2: Restart the Service
Use the docker compose restart command followed by the service name.
Command
docker compose restart web
Explanation
docker compose restart web: Restarts only thewebservice.- Other services, like
db, remain unaffected.
Step 3: Verify the Restart
Check the status of your containers to ensure the service restarted successfully.
Command
docker compose ps
Example Output
Name Command State Ports
----------------------------------------------------------------------------
project_web_1 nginx -g 'daemon off;' Up 0.0.0.0:80->80/tcp
project_db_1 docker-entrypoint.sh postgres Up 5432/tcp
The State column should show Up for the restarted service.
Best Practices
- Use Descriptive Service Names: Clearly name your services in
docker compose.yml. - Monitor Logs: Use
docker compose logs <service_name>to debug issues after a restart. - Minimize Downtime: Restart services during low-traffic periods if possible.
By following these steps, you can efficiently restart individual containers in a Docker Compose setup, ensuring minimal disruption to your application.
Related Resources
- Docker Compose: Up Only Certain Containers — selective startup
- Rebuild Docker Container in Compose — rebuild patterns
- Upgrade Docker Container After Image Changed — update images
- Introduction to Docker: Compose — Compose fundamentals
We earn commissions when you shop through the links below.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?
Related Posts
Also worth your time on this topic
Communication Between Multiple Docker-Compose Projects
Learn how to enable communication between multiple Docker-Compose projects using shared networks and environment configurations.
Running Docker Containers on Your Linux Server
Install Docker and Docker Compose on Ubuntu, run your first container, deploy a WordPress stack with docker-compose, and set up Nginx as a reverse proxy in front of your containers.
60 minutes
Docker Security Hardening Checklist
Comprehensive security checklist for hardening Docker containers, images, and runtime environments.
60-90 minutes