Skip to content

Latest commit

 

History

History
123 lines (89 loc) · 5.05 KB

Docker Compose.md

File metadata and controls

123 lines (89 loc) · 5.05 KB

Docker Compose

Introduction to Docker Compose:

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define your entire application stack, including services, networks, and volumes, in a single YAML file called docker-compose.yml. Docker Compose simplifies the process of managing multiple containers, their dependencies, and configurations, making it especially useful for development, testing, and continuous integration environments.

Compose file syntax and versioning:

The docker-compose.yml file is structured using a specific syntax and versioning scheme. The file begins with a version number, followed by definitions for services, networks, and volumes. The version number determines the syntax and features available in the Compose file.

Example of a basic docker-compose.yml file:

version: '3.9' # Compose file version
services:
  web:
    build: ./web # Build context for the web service
    ports:
      - "8000:8000" # Port mapping
  db:
    image: "postgres:13" # Docker image for the database service
    environment:
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
      POSTGRES_DB: mydb
networks: # (Optional) Define custom networks
  my-network:
volumes: # (Optional) Define volumes for data persistence
  db-data:

Multi-container application deployment using Docker Compose:

To deploy a multi-container application using Docker Compose, follow these steps:

  1. Create a docker-compose.yml file in your project directory that describes your application stack. Define services, networks, and volumes as needed.

  2. Use the docker-compose up command to build and start your application. This command will read the docker-compose.yml file, build images (if necessary), create networks and volumes, and start all services defined in the file.

    Example: docker-compose up -d (The -d flag runs the services in the background.)

  3. To manage the lifecycle of your application, use the following Docker Compose commands:

    • docker-compose ps: Lists the containers and their statuses.
    • docker-compose logs: Displays the logs of the services.
    • docker-compose down: Stops and removes containers, networks, and volumes.
    • docker-compose restart: Restarts the services.
    • docker-compose stop: Stops the services without removing them.
  4. If you need to update your application, you can modify the docker-compose.yml file and then use docker-compose up again. Docker Compose will intelligently update the services with minimal downtime.

Tips and Best Practices

  1. Environment variables: Docker Compose allows you to use environment variables in your docker-compose.yml file. This can be helpful for managing configuration settings and secrets without hardcoding them in the file. You can use the env_file directive to load environment variables from a file or define them directly in the environment section of a service.

    Example:

    services:
      my-service:
        image: "my-image"
        env_file: .env # Loads environment variables from a file
        environment:
          MY_VARIABLE: my-value # Sets an environment variable directly
  2. Extending services: You can use the extends keyword to define common service configurations and extend them in your docker-compose.yml file. This can help reduce duplication and simplify maintenance.

    Example:

    services:
      base-service:
        build: ./base
        environment:
          MY_VARIABLE: my-value
    
      service-a:
        extends: base-service
        build: ./service-a
    
      service-b:
        extends: base-service
        build: ./service-b
  3. Dependency management: Docker Compose allows you to define dependencies between services using the depends_on keyword. This ensures that services are started in the correct order and can help prevent issues caused by missing dependencies.

    Example:

    services:
      db:
        image: "postgres:13"
    
      web:
        build: ./web
        depends_on:
          - db
  4. Health checks: You can define health checks for your services using the healthcheck directive. This allows Docker Compose to monitor the health of your services and restart them if they become unhealthy.

    Example:

    services:
      my-service:
        image: "my-image"
        healthcheck:
          test: ["CMD", "curl", "-f", "http://localhost:8080"]
          interval: 30s
          timeout: 5s
          retries: 3
  5. Scaling services: Docker Compose allows you to scale services horizontally by running multiple instances of a service. To scale a service, use the --scale flag with the docker-compose up command.

    Example: docker-compose up -d --scale web=3

  6. Development and production environments: You can use different docker-compose.yml files for development, testing, and production environments. This allows you to customize configurations for each environment while maintaining a consistent workflow. Use the -f flag to specify the compose file to use.

    Example: docker-compose -f docker-compose.prod.yml up -d