paul23
paul23

Reputation: 9425

Network different than defined name after docker-compose?

Well I try to create a network of several dockers in a single compose file, however the network that should be created isn't there, even while docker-compose up runs fine:

docker compose:

networks:
  allsports.test:
    driver: bridge
services:
  allsports.test.nginx:
    build:
      context: ./allsports.test.nginx/
    container_name: allsports.test.nginx
    image: allsports.test.nginx
    networks:
    - allsports.test
    ports:
    - 380:80
    restart: on-failure
  allsports.test.redis:
    build:
      context: ./allsports.test.redis/
    container_name: allsports.test.redis
    image: allsports.test.redis
    networks:
    - allsports.test
    restart: on-failure
version: '3.7'

After running docker-compose up -d:

Starting allsports.test.nginx ... done
Starting allsports.test.redis ... done

However now I try to inspect the network: sudo docker inspect allsports.app says that the network is not existing. If I do sudo docker network ls I'm also not seeing the network.

If I check one of the created dockers:

sudo docker inspect allsports.test.nginx -f "{{.NetworkSettings.Networks}}"

> map[allsportstest_allsports.test:0xc000604000]

What causes this renaming and (how) can I logically find the actual networks created by inspecting the compose file?

Upvotes: 0

Views: 2346

Answers (1)

Zeitounator
Zeitounator

Reputation: 44605

Default docker names created by docker-compose are using the compose project name as a prefix to avoid naming conflicts.

The project name is by default the name of the folder containing your docker-compose.yml. You can customize it through the COMPOSE_PROJECT_NAME environment variable (in your shell or in a .env file) or with the -p command line option

The way the default docker names are calculated depends on the type of object. For example, default containers names are created from the pattern <projectName>_<serviceIdentifier>_<containerIncrement>. But you can usually use you own custom name when needed (as you did in your above compose file by naming your unique services containers).

For networks, the default name is <projectName>_<networkIdentifier> as reported by your last inspect command in your question. So you can definitely infer that name from the compose project name and the network identifier in your file.

Meanwhile, as with you containers, you can use the network name option to configure you own custom name that will be used as is. In that case, you must take care of potential naming conflicts by yourself.

networks:
  allsports.test:
    driver: bridge
    name: allports.test

Upvotes: 2

Related Questions