Home > OS >  docker uses default network despite specification on docker-compose
docker uses default network despite specification on docker-compose

Time:09-11

I have a docker-compose.yml file as such:

version: '3.8'

services:

  python:
    container_name: python
    build: .
    tty: true
    networks:
      - trade-net
    depends_on:
      - timescale
  
  timescale:
    image: timescale/timescaledb-ha:pg14-latest
    container_name: timescaledb
    ports:
      - "5432:5432"
    volumes:
      - timescale-volume:/home/postgres/pgdata/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=mypass
    networks:
      - trade-net
    
networks:
  trade-net:
    driver: bridge

volumes:
  timescale-volume:

When composing up both containers and inspecting my networks I find:

NETWORK ID     NAME                DRIVER    SCOPE
a096327fe1f6   ark-sql_trade-net   bridge    local
f1d39a23ec84   bridge              bridge    local
eae37b1b17a0   host                host      local
1ec3159d2c48   none                null      local
f622d73b359f   trade-net           bridge    local

So basically docker is creating a new network ark-sql_trade-net instead of using my own personal trade-net network to build the containers.

CodePudding user response:

Quotting from https://docs.docker.com/compose/networking/

Your app’s network is given a name based on the “project name”, which is based on the name of the directory it lives in.

To provide a custom name

networks:
  trade-net:
    name: CUSTOM_NAME
    driver: bridge

If you want your containers to join a pre-existing network, use the external option:

networks:
  default:
    external: true
    name: my-pre-existing-network

Instead of attempting to create a network called [projectname]_default, Compose looks for a network called my-pre-existing-network and connect your app’s containers to it.

  • Related