docker compose

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you define a multi-container application in a single file, then spin up your application with a single command. Here's a comprehensive reference for Docker Compose:

Docker Compose Basics:

1. Compose File Format:

  • Docker Compose files are written in YAML. The default file name is docker-compose.yml, but you can use a different filename with the -f option.

2. Service Definitions:

  • Each service in a Compose file is defined under the services key. A service typically corresponds to a container.

services:
  web:
    image: nginx:latest

3. Build Configuration:

  • You can specify build configurations for services, allowing you to build an image from a Dockerfile.

services:
  web:
    build:
      context: ./path/to/build-context
      dockerfile: Dockerfile.dev

4. Container Naming:

  • By default, Docker Compose generates container names based on the project name and service name. You can customize container names using the container_name field.

services:
  web:
    container_name: my-nginx

5. Environment Variables:

  • Use the environment key to set environment variables for a service.

services:
  web:
    environment:
      - MYSQL_ROOT_PASSWORD=mysecretpassword
      - MYSQL_DATABASE=mydatabase

6. Volumes:

  • Define volumes under the volumes key to persist data.

services:
  web:
    volumes:
      - /path/on/host:/path/in/container

7. Ports:

  • Use the ports key to map ports from the container to the host.

services:
  web:
    ports:
      - "8080:80"

8. Networks:

  • Create custom networks for your services.

services:
  web:
    networks:
      - frontend
networks:
  frontend:

9. Depends On:

  • Specify dependencies between services.

services:
  web:
    depends_on:
      - db

10. Command Override:

  • Override the default command for a service.

services:
  web:
    command: ["nginx", "-g", "daemon off;"]

11. Links:

  • Define links between services. Note: The use of links is discouraged in favor of user-defined networks.

services:
  web:
    links:
      - db

12. Logging:

  • Control logging options.

services:
  web:
    logging:
      driver: "json-file"
      options:
        max-size: "200k"
        max-file: "10"

13. Scale Services:

  • Use the scale command to create multiple instances of a service.

docker-compose up --scale web=3

Example Docker Compose File:

version: "3"
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
  db:
    image: postgres:latest
    environment:
      POSTGRES_PASSWORD: example

Docker Compose Commands:

1. docker-compose up:

  • Build and start services defined in the Compose file.

docker-compose up

2. docker-compose down:

  • Stop and remove containers, networks, and volumes.

docker-compose down

3. docker-compose build:

  • Build services defined in the Compose file.

docker-compose build

4. docker-compose ps:

  • List running containers.

docker-compose ps

5. docker-compose logs:

  • View output from containers.

docker-compose logs

6. docker-compose exec:

  • Execute commands in a running container.

docker-compose exec service_name command

7. docker-compose config:

  • Validate and view the Compose file configuration.

docker-compose config

8. docker-compose pause/unpause:

  • Pause or unpause services.

docker-compose pause
docker-compose unpause

These are the fundamental concepts and commands for using Docker Compose. Docker Compose simplifies the management of multi-container Docker applications and is especially useful for development, testing, and staging environments.