skip to Main Content

I have a docker-compose file that includes the following services:

  • Dind (where inside this container there are other containers responsible for consuming a RabbitMQ queue)
  • RabbitMQ

I would like the child containers of Dind to consume messages from the RabbitMQ within the same docker-compose file as Dind.

version: '3.8'

services:
  dind:
    build:
      context: docker-compose-command-api
      dockerfile: Dockerfile
    container_name: docker-compose-command-api
    privileged: true
    ports:
      - "8080:8080"
    volumes:
      - ./message-consumer-worker:/app/message-consumer-worker:ro
    networks:
      - default

  rabbitmq:
    image: rabbitmq:3.12.10-management
    container_name: rabbitmq
    environment:
      - RABBITMQ_DEFAULT_USER=guest
      - RABBITMQ_DEFAULT_PASS=guest
    ports:
      - "5672:5672"
      - "15672:15672"
    volumes:
      - ./rabbitmq/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf:ro
      - ./rabbitmq/definitions.json:/etc/rabbitmq/definitions.json:ro
    healthcheck:
      test: [ "CMD", "rabbitmqctl", "status" ]
      interval: 5s
      timeout: 20s
      retries: 5
    networks:
      - default

networks:
  default:
    driver: bridge

The docker-compose that will run inside the Dind:

version: '3.8'

services:
  app:
    build:
      context: message-consumer-worker
      dockerfile: Dockerfile
    container_name: message-consumer-worker

From what I researched, it would be possible to use an external network. However, I didn’t implement it due to a lack of knowledge.

2

Answers


  1. I think you need to use network_mode: host in the docker-compose file. This will make the inside container use the same network of the host (outside container).

    Login or Signup to reply.
  2. A Docker network is specific to an instance of the Docker daemon. If you’re running a Docker-in-Docker setup, the nested Docker daemon has its own images, containers, volumes, and networks; you can’t share any of these resources between the two Dockers. Compose external: true networks allow one Compose file to attach to networks created outside this specific Compose file, but not in other Docker daemons. The Docker Hub docker image doesn’t obviously provide anything like the host.docker.internal host name available on Docker Desktop to call out to the daemon’s network environment.

    The easiest answer here is to run the RabbitMQ broker and worker in the same Compose file. You don’t need a nested Docker daemon here, and I might delete the DinD container entirely.

    version: '3.8'
    services:
      rabbitmq: { ... }
      app:
        build: message-consumer-worker
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search