With Docker Compose, you can easily create and control multi container Docker applications using a YAML configuration file. This is extremely helpful for applications with multiple interdependent services, such as a web server and database operating together or a caching service functioning seamlessly.
Enhanced Multi Container Management: You can manage several containers using a single config file.
Improved Networking: A dedicated network for all services is automatically set up.
Automated Service Dependency Management: Dependencies between services are defined to ensure the services are started in the correct order.
Unified Environments: The configuration for development, testing and production processes is consistent.
Ensure Docker is installed on your system before proceeding with Docker Compose. You can get your copy using the official documentation:
# Linux and Mac
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# Verify Installation
docker-compose --version
For docker-compose.yml There are two parts for writing and using docker-compose files.
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
depends_on:
- db
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: mydatabase
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
1. Start Containers
To turn on/off any services mentioned in the docker file, use the commands below:
docker-compose up -d
You will notice I added ‘-d’ flag which runs in detached mode (in the background).
2. Halting Containers
To halt all containers that are currently running, execute the following command on the command line:
docker-compose down
3. Fetch logs
To obtain logs for all services that are running, execute the command:
docker-compose logs -f
4. Service Reboot
To restart a particular service, for example, web, execute the command below:
docker-compose restart web
5. Service Scaling
To scale services, for instance, having several instances for the web service running:
docker-compose up --scale web=3 -d
You can define custom networks as opposed to the auto-created default network that docker-compose creates:
networks:
my_network:
driver: bridge
services:
app:
networks:
- my_network
Use named volumes for persistent data storage as shown below:
volumes:
my_data:
driver: local
For sensitive data use an .env file as illustrated below:
MYSQL_ROOT_PASSWORD=rootpassword
MYSQL_DATABASE=mydatabase
And use it in docker-compose.yml like so:
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
The use of Docker Compose makes managing multi container applications easier through reusable, automated configurations. Deployment is also easier, with improved scaling across various environments.
Ready to transform your business with our technology solutions? Contact Us today to Leverage Our DevOps Expertise.