Docker Compose - docker/getting-started

Docker Compose is a tool for simplifying multi-container application definitions and deployment. It uses a YAML file, called docker-compose.yml, to configure services, networks, volumes, and more. Here’s an overview of the key features and usage of Docker Compose.

Key Features and Design Philosophy

Docker Compose helps define and run multi-container applications. It uses a YAML file to configure services, networks, and volumes, and allows for:

  • Versioning: Docker Compose supports versioning, making it easy to manage changes to your application’s infrastructure.
  • Multiple environments: You can define different environments (e.g., development, staging, production) using separate docker-compose.yml files.
  • Dependency management: Docker Compose manages dependencies between services, ensuring they start and stop in the correct order.
  • Configuration reuse: You can reuse and extend configurations across multiple projects.

Programming Languages

Docker Compose files are written in YAML format, which is a human-readable data serialization language. YAML is programming language-agnostic and can be used with any programming language.

Error Handling

Docker Compose provides detailed error messages when it encounters issues with your configuration files. It also supports various flags to help with debugging, such as --verbose for more detailed output.

Online Documentation

Docker Compose Commands

Here are some common Docker Compose commands:

  • Start services: docker-compose up --build
  • Start services in the background: docker-compose up --build -d
  • Stop services: docker-compose down
  • View logs: docker-compose logs
  • Build images: docker-compose build
  • Remove stopped services: docker-compose rm

Docker Compose File Options

The docker-compose.yml file supports various options. Here are some examples:

  • Services: Define services, such as web, worker, or database.
version: "3.9"
services:
web:
image: my-web-image
ports:
- "8000:8000"
  • Networks: Define networks for service communication.
version: "3.9"
services:
web:
image: my-web-image
networks:
- my-network
networks:
my-network:
  • Volumes: Define volumes for data persistence.
version: "3.9"
services:
web:
image: my-web-image
volumes:
- my-volume:/path/in/container
volumes:
my-volume:
  • Environment Variables: Define environment variables for services.
version: "3.9"
services:
web:
image: my-web-image
environment:
- MY_ENV_VAR=value
  • Secrets: Define secrets for services.
version: "3.9"
services:
web:
image: my-web-image
secrets:
- my-secret
secrets:
my-secret:
file: path/to/secret
  • Configs: Define configs for services.
version: "3.9"
services:
web:
image: my-web-image
configs:
- my-config
configs:
my-config:
file: path/to/config
  • Extending Configurations: Extend configurations from other files.
version: "3.9"
services:
web:
extends:
file: common-services.yml
service: web

Examples

Additional Resources