Skip to content

Docker Compose

Commands

Services are defined in the docker-compose.yml. This must be located in the current directory. <Service...> can be replaced by one or more services. If no service is explicitly specified, then the command is applied to all services.

Command Description
docker compose up -d <Service...> Create and start one or more services
docker compose down Stop and delete all services (incl. networks)
docker compose down -v Stop and delete all services, networks, volumes
docker compose build <Service...> Build one or more services
docker compose pull <Service...> Pull the images of one or more services
docker compose up -d --build --pull always <Service...> Build or pull services; Start services
docker compose logs <Service...> Output the logs from one or more services
docker compose ps Display the running containers
docker compose top <Service...> List the running processes of one or more services
docker compose create <Service...> Create one or more services
docker compose start <Service...> Start one or more services
docker compose stop <Service...> Stop one or more services
docker compose rm <Service...> Delete one or more services
docker compose exec <Service> <Command> Execute command in container with TTY
docker compose exec -T <Service> <Command> Execute command in container without TTY

docker-compose.yml

All entries beginning with cw- are freely selectable names. All lines that begin with # are comments.

Structure

services:
  # Create one or more services
  cw-webapp:
    # build points to a directory containing the Dockerfile.
    # This file will be built
    build: ./path/to/directory
    # Define the image with optional tag. Will be downloaded if not present
    image: <image>:<tag>
    # Most of the time, it's either 'build' or 'image'. They can be used in combination though. For more information: https://docs.docker.com/reference/compose-file/build/

    # Optional: overwrite entrypoint
    entrypoint: /bin/bash
    # Optional: overwrite command
    command: ["echo", "$MYSQL_USER"]

    # List of dependent services
    ## This service (cw-webapp) will be started after all
    ## dependent services have started
    depends_on:
      - cw-database

    # List of port forwarding entries
    ports:
      -  5000:5000

    # List of volumes
    volumes:
      - cw-webapp-volume:/app/data/notes

    # List of networks
    networks:
      - cw-database-network


    # List of environment variables
    environment:
      - "MYSQL_USER=example-user"
      - "MYSQL_PASSWORD=..."

  # Second service:
  cw-database:
    [...]

# (Optional) Create one or more managed volumes
volumes:
  cw-webapp-volume:

# (Optional) Create one or more networks
networks:
  cw-database-network: