In the previous sections, we covered the basics of Docker Compose, including how to define and run multi-container Docker applications. In this advanced section, we will delve into more sophisticated configurations and features that can help you manage complex applications with greater efficiency.
Docker Compose is a powerful tool for defining and running multi-container Docker applications. It allows you to configure your application services, networks, and volumes in a single YAML file. This makes it easier to share and deploy applications across different environments.
Scaling services allows you to run multiple instances of a service, which can help distribute load and improve availability. This is particularly useful for stateless applications.
To scale a service, you use the docker-compose up command with the --scale option. For example:
Here's an example of using environment variables for configuration.
1version: '3'2services:3app:4image: my-app5env_file:6- .env
And the .env file:
1API_KEY=1234567890abcdef2DATABASE_URL=postgres://user:password@db:5432/mydb
This example demonstrates setting up a custom network and configuring a health check.
1version: '3'2services:3web:4image: my-web-app5networks:6- my-network7healthcheck:8test: ["CMD", "curl", "-f", "http://localhost/health"]9interval: 10s10timeout: 5s11retries: 31213networks:14my-network:15driver: bridge