How to Run and Manage Containers with Docker Compose
Category: Software Install and Setup
Managing multiple Docker containers manually can quickly become overwhelming, especially when dealing with complex applications that require multiple services. Docker Compose simplifies this by allowing you to define and manage multi-container applications with a single configuration file. In this guide, we’ll cover everything you need to know about Docker Compose, from installation to practical usage.
What is Docker Compose?
Docker Compose is a tool that enables you to define and run multi-container Docker applications using a simple YAML configuration file. Instead of managing individual containers manually, you can define services, networks, and volumes in a single file and deploy everything with a single command.
1. Install Docker Compose
Before using Docker Compose, ensure Docker is installed on your system. Then, install Docker Compose:
- Windows & macOS: Docker Compose comes bundled with Docker Desktop.
- Linux: Run the following command:
sudo apt-get install docker-compose
2. Creating a Docker Compose File
Docker Compose uses a docker-compose.yml
file to define the services. Below is an example for running a simple web application with Nginx and a PostgreSQL database:
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80"
depends_on:
- db
db:
image: postgres:latest
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mydatabase
3. Running Containers with Docker Compose
Once your docker-compose.yml
file is ready, start your services:
docker-compose up -d
- The
-d
flag runs the containers in detached mode. - Check running containers with:
docker-compose ps
4. Managing Containers with Docker Compose
You can manage your containers efficiently using the following commands:
- Stop all services:
docker-compose down
- Restart services:
docker-compose restart
- View logs:
docker-compose logs -f
- Rebuild services:
docker-compose up --build
5. Using Environment Variables
Instead of hardcoding credentials, use an .env
file:
POSTGRES_USER=user
POSTGRES_PASSWORD=password
POSTGRES_DB=mydatabase
Modify the docker-compose.yml
file to load the environment variables:
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
6. Networking in Docker Compose
Docker Compose automatically creates a default network for your services. However, you can define a custom network:
networks:
my_network:
driver: bridge
Assign services to the network:
services:
web:
networks:
- my_network
db:
networks:
- my_network
7. Volume Management
To persist database data, define a volume:
volumes:
db_data:
Attach it to a service:
services:
db:
volumes:
- db_data:/var/lib/postgresql/data
Conclusion
Docker Compose simplifies container management, making it easier to handle multi-container applications. By defining services, networks, and volumes in a single file, you can deploy complex applications with minimal effort. For further learning, visit the official Docker Compose documentation.