compose: [BUG] docker compose up failing to connect to postgres container on Apple Silicon Macs

Description

Current Behavior

When running docker compose up to set up a dev environment, my custom containers depend on a postgres container. On most attempts of running docker compose up, the custom containers fail to make a connection with postgres, however there are occasions when it works. This issue is occuring for two out of three members of our team with Apple Silicon machines (M1 and M2), but it does not occur for the member of our team with an Intel Mac. We have already tried switching to the arm64v8/postgres version of the postgres image but that has not fixed the issue.

version: '3'
services:
  postgres:
    image: postgres
    environment:
      - POSTGRES_USER=REDACTED
      - POSTGRES_PASSWORD=REDACTED
      - POSTGRES_DB=REDACTED
    # volumes:
    #   - ./learn_prisma_default:/var/lib/docker/volumes/learn_prisma_postgres/_data
  redis:
    image: redis
    env_file:
      - .env

# My two custom containers
  venture-copilot:
    image: venture-copilot
    ports:
      - "8080:8080"
    env_file:
      - .env
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - postgres
      - redis

  dashboard:
    image: dashboard
    ports:
      - "80:80"
    env_file:
      - .env
    build:
      context: dashboard
      dockerfile: Dockerfile
    depends_on:
      - postgres
      - redis

Here are the logs from a successful and failed example: https://gist.github.com/taimurshaikh/c497167b00c4bc06c05fded18ca9cd0e

Steps To Reproduce

No response

Compose Version

Docker Compose version v2.12.2

Docker Environment

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc., v0.9.1)
  compose: Docker Compose (Docker Inc., v2.12.2)
  dev: Docker Dev Environments (Docker Inc., v0.0.3)
  extension: Manages Docker extensions (Docker Inc., v0.2.13)
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
  scan: Docker Scan (Docker Inc., v0.21.0)
WARNING: Plugin "/Users/taimurshaikh/.docker/cli-plugins/docker-init" is not valid: failed to fetch metadata: fork/exec /Users/taimurshaikh/.docker/cli-plugins/docker-init: no such file or directory
WARNING: Plugin "/Users/taimurshaikh/.docker/cli-plugins/docker-scout" is not valid: failed to fetch metadata: fork/exec /Users/taimurshaikh/.docker/cli-plugins/docker-scout: no such file or directory

Server:
 Containers: 4
  Running: 0
  Paused: 0
  Stopped: 4
 Images: 4
 Server Version: 20.10.21
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 1c90a442489720eec95342e1789ee8a5e1b9536f
 runc version: v1.1.4-0-g5fd4c4d
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
  cgroupns
 Kernel Version: 5.15.49-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: aarch64
 CPUs: 4
 Total Memory: 7.667GiB
 Name: docker-desktop
 ID: 45IS:TCZX:YB3F:E5KM:ZGAU:SVYO:CEAW:YNSQ:IIPF:QYK4:5P2V:QIIT
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5000
  127.0.0.0/8
 Live Restore Enabled: false

Anything else?

List of things we have tried so far:

  • Adding a healthcheck to the postgres container, experimenting with different timeout lengths and number of retries
  • Upgrading Docker Desktop to latest version

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 19 (8 by maintainers)

Most upvoted comments

I’m getting the same error as previously mentioned. Docker is not creating the postgres container. It is just creating the app build. As my app depends on the postgres the build is failing every time.

Here is my docker-compose file: Screenshot 2023-11-09 at 9 29 18 PM

Here is the error I’m getting: Screenshot 2023-11-09 at 9 30 21 PM

I’m facing a similar issue with random containers. Right now, I’m using Redis, MongoDB, RabbitMQ, MariaDB, and Postgres.

It randomly fails. According to my inspection, the main container is not being attached to the network of the Database and attaching correctly at others.

At my company we have this issue happening even with Linux Systems, so it should not be ann issue that happens only on Macs.

services:
  service-one:
    build:
      context: service-one
      dockerfile: Dockerfile
    container_name: service-one
    image: "kelvin/service_one"
    ports:
      - "8080:8080"
      - "5002:5005"
    environment:
      - GITHUB_ACTOR=$GITHUB_ACTOR
      - GITHUB_PASS=$GITHUB_PACKAGES_IMPORT_TOKEN
      - JPDA_OPTS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
      - >-
        JAVA_TOOL_OPTIONS=-javaagent:/usr/lib/elastic-apm-agent.jar
    env_file: env-files/service_one.env.docker
    depends_on:
      redis:
        condition: service_started
      mariadb:
        condition: service_started
      mongodb:
        condition: service_started
      rabbitmq:
        condition: service_healthy
    volumes:
      - "./docker_data/pics:/pics"
    networks:
      - service_one_api
      - mariadb_api
      - mongodb_api
      - redis_api
      - rabbit_api
      - dynamodb_api
      - localstack_api
    entrypoint: [ "catalina.sh", "jpda", "run" ]
  service-two:
    build:
      context: service-two
      dockerfile: Dockerfile
      args:
        - GITHUB_ACTOR=$GITHUB_ACTOR
        - GITHUB_PACKAGES_IMPORT_TOKEN=$GITHUB_PACKAGES_IMPORT_TOKEN
    env_file:
      - env-files/service_two.env.docker
    container_name: service_two
    image: "kelvin/service_two"
    ports:
      - "7777:8080"
      - "5007:5005"
    depends_on:
      redis:
        condition: service_started
      postgres:
        condition: service_healthy
      rabbitmq: 
        condition: service_healthy
    environment:
      - >-
        JAVA_TOOL_OPTIONS=-javaagent:/usr/lib/reactor-tools.jar -javaagent:/usr/lib/elastic-apm-agent.jar
        -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:5005
    networks:
      - postgres_api
      - rabbit_api
      - redis_api
      - service_two_api
  localstack:
    container_name: localstack
    image: localstack/localstack
    ports:
      - '4566:4566'            # LocalStack Gateway
    networks:
      - localstack_api
    environment:
      - SERVICES=sqs
      - DEBUG=0
      - DOCKER_HOST=unix:///var/run/docker.sock
      - AWS_DEFAULT_REGION=sa-east-1
      - AWS_ID=test
      - AWS_KEY=test
    volumes:
      - ./localstack_setup/ready.sh:/etc/localstack/init/ready.d/init-aws.sh
  redis:
    container_name: redis
    image: redis:7.0.3-alpine
    restart: always
    ports:
      - '6379:6379'
    command: redis-server --save 60 1 --loglevel warning
    networks:
      - redis_api
    environment:
      - REDIS_PASSWORD=defaultpass
  mariadb:
    container_name: mariadb
    image: "mariadb:10.11.4"
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "kelvin"
      MYSQL_DATABASE: "kelvin"
      MYSQL_USER: "kelvin"
      MYSQL_PASSWORD: "kelvin"
    networks:
      - mariadb_api
    volumes:
      - "./database-scripts/monolith_schema.sql:/docker-entrypoint-initdb.d/1.sql"
      - "./database-scripts/monolith_seed.sql:/docker-entrypoint-initdb.d/2.sql"
  rabbitmq:
    container_name: rabbitmq
    image: rabbitmq:management-alpine
    ports:
      - "5672:5672"
      - "61613:61613"
      - "15672:15672"
    environment:
      RABBITMQ_DEFAULT_USER: user
      RABBITMQ_DEFAULT_PASS: password
    networks:
      - rabbit_api
    command: "/bin/bash -c \"rabbitmq-plugins enable --offline rabbitmq_stomp; rabbitmq-server\""
    healthcheck:
      test: rabbitmq-diagnostics check_port_connectivity
      interval: 1s
      timeout: 3s
      retries: 30
  postgres:
    image: "postgres:15.3"
    container_name: postgres
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: "kelvin"
      POSTGRES_PASSWORD: "kelvin"
      POSTGRES_DB: "kelvin"
    volumes:
      - "./database-scripts/database_creation.sql:/docker-entrypoint-initdb.d/0.sql"
      - "./database-scripts/ttlock_schema.sql:/docker-entrypoint-initdb.d/1.sql"
      - "./database-scripts/ttlock_seed.sql:/docker-entrypoint-initdb.d/2.sql"
    networks:
      - postgres_api
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U loopkey"]
      interval: 5s
      timeout: 5s
      retries: 5
  mongodb:
    container_name: mongodb
    image: "mongo:6.0.6"
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: kelvin
      MONGO_INITDB_ROOT_PASSWORD: kelvin
    networks:
      - mongodb_api
networks:
  service_one_api:
    name:  service_one_api
  mariadb_api:
    name: mariadb_api
  mongodb_api:
    name: mongodb_api
  redis_api:
    name: redis_api
  rabbit_api:
    name: rabbit_api
  postgres_api:
    name: postgres_api
  service_two_api:
    name:  service_two_api

As I’ve mentioned the service-one or service-two randomly fails to connect to Redis, MariaDB, Postgres, or RabbitMQ. By inspection, the problem is occurring because sometimes the services are not connected to the right network.

In my last error case, the service-one cannot connect to redis_api and service_two connect. Additionally, both of them are connected to rabbit_api.