Browse topics

Starting a Compose project

Version
29
Review
Draft

Summary#

docker compose up creates and starts the services in a Compose file. --detach --wait returns once they are running or healthy. docker compose down removes the project's containers and networks.

Syntax#

sh
docker compose up --detach --wait
docker compose ps
docker compose logs --follow SERVICE
docker compose down
docker compose down --volumes
docker compose --project-name NAME --file compose.yaml up

Example#

The example uses a local busybox:1 image.

sh
(
  set -eu
  project="dlr-docker-commands-up-$$"
  dir=$(mktemp -d)
  compose() {
    docker compose --progress quiet --project-name "$project" \
      --file "$dir/compose.yaml" "$@"
  }
  trap 'compose down --volumes --timeout 1 >/dev/null 2>&1 || true; rm -rf -- "$dir"' EXIT

  cat > "$dir/compose.yaml" <<'EOF'
services:
  web:
    image: busybox:1
    pull_policy: never
    command: ["sh", "-c", "echo 'hello from compose' > /site/index.html; echo serving; exec httpd -f -p 8080 -h /site"]
    volumes:
      - site:/site
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://127.0.0.1:8080/"]
      interval: 1s
      retries: 10
volumes:
  site:
EOF

  compose up --detach --wait
  compose ps --format '{{.Service}} {{.State}} {{.Health}}'
  compose exec -T web wget -qO- http://127.0.0.1:8080/
  compose logs --no-log-prefix web

  compose down --volumes --timeout 1
  docker volume ls --quiet --filter "label=com.docker.compose.project=$project" |
    wc -l | tr -d ' '
)

Expected output#

Text
web running healthy
hello from compose
serving
0

Common mistakes#

down --volumes deletes the project's declared named volumes and the anonymous volumes attached to its containers, together with their data. Plain down keeps them. If you start a project with --project-name, use the same name for later commands, or they address a different project.

Version applicability#

Checked with Docker Engine 29 and Docker Compose 5. The docker compose plugin is versioned separately from the Engine. External networks and volumes are not removed by down.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0