Skip to main content
Common Docker Compose Errors and How to Fix Them

Common Docker Compose Errors and How to Fix Them

KU
· Founder & Chief Editor
• 8 min read•Updated •88 views

The Compose failures that eat an afternoon are almost always the same six. Here's how to spot each one fast and the fix that actually sticks.

Key takeaways

  • Most Docker Compose errors come from six causes.
  • A .env file outside the project directory, depends_on without a healthcheck, a host port already in use, an undeclared network or volume, a stale image without --build, and a fixed container_name.
  • Run docker compose config first, because it shows exactly what Compose resolved.

Most Docker Compose errors trace back to six causes: a .env file Compose never read, depends_on without a healthcheck, a host port already taken, an undeclared network or volume, a stale image, and a fixed container_name. Run docker compose config first. It prints the fully resolved file, which settles half of these in seconds.

Verified against

Docker Compose v5.6.1 (latest release), against the Docker Compose docs on variable interpolation, startup order and the Compose file reference. Checked October 2026.

The debugging loop we use for every Compose problem:

code
$ docker compose config          # resolved file: interpolated vars, merged overrides
$ docker compose ps -a           # every container, including exited ones
$ docker compose logs --tail=50 api
$ docker compose up -d --build --force-recreate api

If config shows the wrong value, the bug is in parsing. If it shows the right value and the container still misbehaves, the bug is at runtime. That split is the whole method.

Environment variables that don't interpolate

You write ${DB_PASSWORD}, run docker compose up, and get:

code
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.

The container then starts with an empty password. Compose reads .env from the project directory, not from wherever your Dockerfile lives. Per the Docker docs, the project directory is --project-directory if you pass it, otherwise the directory of the first -f file, otherwise your current directory. Run docker compose -f deploy/compose.yaml up from the repo root and Compose looks for deploy/.env. Point it elsewhere explicitly with --env-file config/prod.env, and know that shell variables beat --env-file, which beats the project .env.

The second trap is two separate stages. ${VAR} in the Compose file is resolved at parse time from the shell and .env. A value in .env does not reach the container unless you pass it through environment: or env_file::

YAML
# compose.yaml
services:
  api:
    image: myapp/api:1.4
    env_file: .env
    environment:
      DATABASE_URL: ${DATABASE_URL:?DATABASE_URL must be set}

The :? form makes Compose exit with an error instead of silently substituting an empty string. We use it for every secret. For a literal dollar sign in a value (cron strings, bcrypt hashes), write $$.

depends_on doesn't wait for readiness

This is the big one. Plain depends_on uses the service_started condition: Compose starts api once the db container is running, not once Postgres accepts connections. Your app boots, queries a database still initializing, and dies with connection refused or could not connect to server.

Start order is not readiness. Put a healthcheck on the dependency and gate the dependent with condition: service_healthy:

YAML
# compose.yaml
services:
  db:
    image: postgres:18
    environment:
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d postgres"]
      interval: 10s
      timeout: 10s
      retries: 5
      start_period: 30s

  api:
    image: myapp/api:1.4
    depends_on:
      db:
        condition: service_healthy
        restart: true

restart: true tells Compose to restart api when db is restarted by a Compose command, so the app reconnects cleanly. For one-shot jobs like migrations, use condition: service_completed_successfully. Do not put sleep 30 in an entrypoint: it is too short on a cold CI runner, wasted time on a laptop, and never tells you why the dependency failed. Check health directly with docker inspect --format '{{.State.Health.Status}}' <container>.

Port and network conflicts

code
Error response from daemon: driver failed programming external connectivity on endpoint ...
Bind for 0.0.0.0:5432 failed: port is already allocated

Something already holds the host port: a stray container from last week, or a Postgres installed on the host. Find it:

code
$ docker ps --filter publish=5432
$ sudo lsof -iTCP:5432 -sTCP:LISTEN

Stop it, or remap the host side: "5433:5432" keeps the container on 5432 and exposes 5433. The full walkthrough is in fixing "port is already allocated".

Network conflicts are subtler. Overlapping bridge subnets or a pile of orphaned networks produce could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network. Run docker network prune to clear unused networks, and declare an explicit subnet under networks: if your VPN or office LAN overlaps Docker's default pools.

Undefined network or volume

code
service "api" refers to undefined volume pgdata: invalid compose project

Compose validates references before it starts anything. A network or named volume used by a service must also appear in the top-level networks: or volumes: block:

YAML
# compose.yaml
services:
  api:
    networks: [backend]
    volumes:
      - pgdata:/var/lib/postgresql/data

networks:
  backend:

volumes:
  pgdata:

The top-level entry can be empty, but it has to exist.

The version field is obsolete

code
WARN[0000] /app/compose.yaml: the attribute `version` is obsolete, it will be ignored

The Compose file reference says the top-level version is "only informative" and Compose always validates against the most recent schema. Delete the line. While you are there, use docker compose (the v2+ plugin, now at v5) instead of the old Python docker-compose binary, which no longer gets fixes.

Build vs image confusion

You edit app code, rerun docker compose up, and nothing changes. Compose reuses an existing image unless you pass --build. If a service sets only image:, Compose pulls a prebuilt image and never reads your source at all. If it sets both build: and image:, Compose builds and tags the result with the image name. The fix is docker compose up --build, or docker compose build --no-cache api when a cached layer is the suspect.

Container name conflicts

code
Error response from daemon: Conflict. The container name "/my-api" is already in use by container "3f2a...".

A fixed container_name: my-api collides with a container a previous run left behind, and it also stops you from scaling the service. Drop container_name and let Compose name containers from the project and service. If you need it, clear the stale one with docker rm -f my-api, or run docker compose down --remove-orphans first.

Volumes that don't persist, and permission errors

Data vanishing on restart usually means an anonymous volume, or a mount target that doesn't match where the app writes. Use a named volume for anything you keep, and remember docker compose down -v deletes named volumes too.

permission denied on a mounted directory is a UID mismatch: the container process runs as a user that doesn't own the host path. Run the service as your host UID with user: "1000:1000", or chown the directory to the UID the image expects. Named volumes usually avoid this because Docker copies ownership from the image on first use; bind mounts don't. If the error is about /var/run/docker.sock rather than a mount, that is a different problem, covered in fixing Docker permission denied.

compose run behaves differently from up

docker compose run api curl http://web joins the project network, so service names resolve, but it does not publish the service's ports unless you add --service-ports. It also starts a fresh one-off container, so anything written outside a volume is gone when it exits. Add --rm so those containers don't accumulate.

Where this breaks

This list covers single-host Compose for development and small deployments. It does not cover Swarm stacks deployed with docker stack deploy, which handle startup ordering differently, Docker Desktop VM resource limits, or failures inside the image build itself, which belong to the Docker troubleshooting guide. If you are new to containers and the errors above all look unfamiliar, start with your first container before debugging Compose.

Questions people ask

Why is my .env file not being read by docker compose?

Compose reads .env from the project directory, which is the directory of the first -f file unless you set --project-directory, not necessarily your current directory. Run docker compose config to see what was resolved. Shell variables override --env-file, which overrides the project .env. Also remember .env only feeds interpolation; pass values into containers with environment: or env_file:.

How do I make docker compose wait for the database to be ready?

Add a healthcheck to the database service, for example pg_isready for Postgres, then set depends_on with condition: service_healthy on the app. Plain depends_on only waits for the container to start. Add restart: true so the app restarts when the database is restarted, and keep connection retries in the app itself for production.

Should I remove the version line from docker-compose.yml?

Yes. Docker's Compose file reference calls the top-level version element obsolete and only informative. Current Compose ignores it, prints a warning, and always validates against the latest schema. Removing it changes nothing about how your file runs and silences the warning.

The call we'd make

Put a healthcheck on every stateful service and gate dependents with condition: service_healthy. Use ${VAR:?message} for anything that must be set, drop the version line, drop container_name unless you truly need it, and name your volumes. Before changing anything, run docker compose config. Most lost Compose afternoons come from trusting start order to mean readiness, and a ten-line healthcheck ends that argument.

Sources

Explore topics:DockerDevOps
React
Share this post
Cite this article
Plain text

Kiril Urbonas (2026). Common Docker Compose Errors and How to Fix Them. DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/fix-docker-compose-errors

Markdown

[Common Docker Compose Errors and How to Fix Them](https://www.devopsness.com/blog/fix-docker-compose-errors) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)

KU

Kiril Urbonas

Founder & Chief Editor

586 articles
View all articles by Kiril Urbonas

You might have missed

Evergreen posts worth revisiting.