Docker-compose up / Connection aborted, Connection Refused Error

Im using MacOS Ventura and tried to reinstall my docker and docker-compose a couple of times, and also restart my OS to see if was an error with the path of zshrc, but im keeping getting this error when i run docker-compose up --build into my app folder:

Traceback (most recent call last):
  File "urllib3/connectionpool.py", line 677, in urlopen
  File "urllib3/connectionpool.py", line 392, in _make_request
  File "http/client.py", line 1277, in request
  File "http/client.py", line 1323, in _send_request
  File "http/client.py", line 1272, in endheaders
  File "http/client.py", line 1032, in _send_output
  File "http/client.py", line 972, in send
  File "docker/transport/unixconn.py", line 43, in connect
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "requests/adapters.py", line 449, in send
  File "urllib3/connectionpool.py", line 727, in urlopen
  File "urllib3/util/retry.py", line 410, in increment
  File "urllib3/packages/six.py", line 734, in reraise
  File "urllib3/connectionpool.py", line 677, in urlopen
  File "urllib3/connectionpool.py", line 392, in _make_request
  File "http/client.py", line 1277, in request
  File "http/client.py", line 1323, in _send_request
  File "http/client.py", line 1272, in endheaders
  File "http/client.py", line 1032, in _send_output
  File "http/client.py", line 972, in send
  File "docker/transport/unixconn.py", line 43, in connect
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionRefusedError(111, 'Connection refused'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "docker/api/client.py", line 214, in _retrieve_server_version
  File "docker/api/daemon.py", line 181, in version
  File "docker/utils/decorators.py", line 46, in inner
  File "docker/api/client.py", line 237, in _get
  File "requests/sessions.py", line 543, in get
  File "requests/sessions.py", line 530, in request
  File "requests/sessions.py", line 643, in send
  File "requests/adapters.py", line 498, in send
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionRefusedError(111, 'Connection refused'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "docker-compose", line 3, in <module>
  File "compose/cli/main.py", line 81, in main
  File "compose/cli/main.py", line 200, in perform_command
  File "compose/cli/command.py", line 70, in project_from_options
  File "compose/cli/command.py", line 153, in get_project
  File "compose/cli/docker_client.py", line 43, in get_client
  File "compose/cli/docker_client.py", line 170, in docker_client
  File "docker/api/client.py", line 197, in __init__
  File "docker/api/client.py", line 222, in _retrieve_server_version
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', ConnectionRefusedError(111, 'Connection refused'))
[9] Failed to execute script docker-compose

docker-version:

Docker version 24.0.7, build afdd53b4e3

docker-compose version:

docker-compose version 1.29.2, build 5becea4

Dockerfile:

FROM python:3.11-alpine

WORKDIR /app

RUN apk add --no-cache gcc musl-dev linux-headers

RUN apk add --update --no-cache --virtual .tmp-build-deps \
    gcc libc-dev linux-headers postgresql-dev \
    && apk add libffi-dev

RUN pip install poetry

COPY pyproject.toml poetry.lock ./

RUN poetry config virtualenvs.create false \
    && poetry install --no-dev --no-interaction --no-ansi

COPY . /app


ENV FLASK_APP=app


EXPOSE 8000

CMD ["python", "app.py"]

docker-compose:

version: '3.9'

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:8000"
    depends_on:
      - mongodb
      - rabbitmq
      - nginx
    environment:
      - CELERY_BROKER_URL=${CELERY_BROKER_URL} 
      - MONGO_URI=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@mongodb:27017/
      - MONGO_DB=${MONGO_DB}
      - TIMEZONE=${TIMEZONE}
  nginx:
    image: nginx:1.25-alpine
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro

  mongodb:
    image: mongo:4.2.3-bionic
    ports:
      - "27017:27017"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=${MONGO_INITDB_ROOT_USERNAME}
      - MONGO_INITDB_ROOT_PASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
    volumes:
      - ./db/seed.js:/docker-entrypoint-initdb.d/seed.js:ro
      - mongodb_data:/data/db

  mongo-express:
    image: mongo-express:1-20-alpine3.18
    ports:
      - "8081:8081"
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=${ME_CONFIG_MONGODB_ADMINUSERNAME}
      - ME_CONFIG_MONGODB_ADMINPASSWORD=${ME_CONFIG_MONGODB_ADMINPASSWORD}
      - ME_CONFIG_MONGODB_SERVER=mongodb
      - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
      - ME_CONFIG_BASICAUTH_USERNAME=${ME_CONFIG_BASICAUTH_USERNAME}
      - ME_CONFIG_BASICAUTH_PASSWORD=${ME_CONFIG_BASICAUTH_PASSWORD}
    depends_on:
      - mongodb

  rabbitmq:
    image: rabbitmq:management
    container_name: rabbitmq
    restart: always
    environment:
      - RABBITMQ_DEFAULT_USER=${RABBITMQ_DEFAULT_USER}
      - RABBITMQ_DEFAULT_PASS=${RABBITMQ_DEFAULT_PASS}
      - RABBITMQ_DEFAULT_VHOST=/
    ports:
      - 5672:5672
      - 15672:15672
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq

  celery-worker:
    build:
      context: .
    restart: always
    environment:
      - CELERY_BROKER_URL=amqp://admin:admin@rabbitmq:5672
      - MONGO_DB={MONGO_DB}
    command: celery -A app.adapters.queue.celery_consumer worker --loglevel=info 
    depends_on:
      - rabbitmq
 
volumes:
  mongodb_data: 
  rabbitmq_data:

does anyone know how to fix this?

  • 1

    That message is saying Compose can’t connect to the Docker daemon. The docker-compose version 1 at this point is outdated and unsupported, and it might not support some newer Docker CLI features like contexts. Does a newer version of Compose work better?

    – 

  • It works @DavidMaze, can you put this as an answer? I would like to vote it as a correct answer to my problem 🙂

    – 

You are using a fairly current version of Docker (24.0.7 is current as of this writing) against an old version of the Compose tool (the Python-based Compose 1.x has been unsupported since July 2023). On a MacOS host, you need to be using Docker Desktop or a similar VM-based solution so that your containers can use a Linux kernel.

More specifically here, recent versions of Docker Desktop set up a Docker context, to point at a socket file in the user’s home directory. Contexts are a relatively new Docker concept, and it’s likely that the older Compose implementation just doesn’t support them. This would result in the error you’re seeing, that Compose is trying to talk to the wrong Docker socket file.

If you’ve installed Docker Desktop, the simplest documented path is to run docker compose with a space, rather than docker-compose with a hyphen. You should uninstall a separate docker-compose tool if you have one.

If this doesn’t work, then you’ll need to manually download the Compose plugin. This isn’t supposed to be necessary using Docker Desktop, which will include most MacOS systems.

Leave a Comment