How to use a pre-existing network in Docker Compose

Viewed 35
Docker version 20.10.16, build aa7e414

docker-compose version 1.29.2, build 5becea4c

macOS Monterey
Version 12.2.1
MacBook Air (M1)

The following are the existing Docker networks:

NETWORK ID     NAME             DRIVER    SCOPE
53c30c122cc6   bridge           bridge    local
06f81782db26   host             host      local
eba839136a82   none             null      local

I want to create a new mongodb container and connect it to the existing bridge network shown above.

I tried different Compose files but none of them worked:

Approach 1)

version: "3.7"
services:
  mongodb_container:
    image: mongo
    container_name: mongodb_demo
    ports:
      - 27020:27017
    networks:
          - b
networks:
  b:
    external:
      name: bridge

Approach 2)

version: "3.7"
services:
  mongodb_container:
    image: mongo
    container_name: mongodb_demo
    ports:
      - 27020:27017
    networks:
      - bridge

networks:
  bridge:
    external: true

Approach 3)

version: "3.7"
services:
  mongodb_container:
    image: mongo
    container_name: mongodb_demo
    ports:
      - 27020:27017

networks:
  default:
    name: bridge
    external: true

All 3 approaches give me the following error:

Starting mongodb_demo ... error
ERROR: for mongodb_demo network-scoped alias is supported only for containers in user defined networks
ERROR: for mongodb_container network-scoped alias is supported only for containers in user defined networks ERROR: Encountered errors while bringing up the project.

0 Answers
Related