Compose: Specify network name in docker-compose file

Created on 15 Jul 2016  ·  26Comments  ·  Source: docker/compose

Is it possible to provide ability to set a name for network in docker-compose file?

Like this:

networks:
  custom_network:
    name: custom_network_name

Thanks in advance!

Most helpful comment

@roboto-cloud

networks:
  foo:
    name: bar

Make sure that you're using version: '3.5' or higher

All 26 comments

for overlay network:
network_mode: custom_network_name

@vingrad does it work for overlay network only?

no, but for other networks you need to use othe syntax: https://docs.docker.com/compose/compose-file/#/networks

@vingrad resulting network name is project name + _ + network name in compose file. Is it possible to set resulting network name?

@ivstas If you want full control over the network name, create it yourself and then reference it as external in your Compose file:

networks:
  custom_network:
    external:
      name: custom_network_name

@aanand I know about this opportunity. Also, the same could be told about container names, but we do have an opportunity to set container name in compose file, right?

Yes, but I don't think adding that was a good idea.

@aanand Why is that?

Aliases work fine for most cases but a Kerberos client, for example, uses reverse DNS Lookup to figure out the canonical hostname of a service, and reverse DNS lookup always yields <container_name>.<network_name>, so being able to hardcode both would be really useful.

Yeah, it will be quite useful.
You really could use particular names in commands like this:

$ docker inspect --format "{{.NetworkSettings.Networks.my___name.IPAddress}}" my-nginx

I have two projects one is started using docker run ( with systemd ) and other one is started using docker-compose How am I suppose to put them both on the same network?
With run I only had to use --net=bridge but with docker-compose its a whole another story. Nothing I try works... :/

@tarach I think this answers your question: https://docs.docker.com/compose/networking/#using-a-pre-existing-network

Yes, I was thinking more of a way of setting the default network for docker-compose.
The fact that d run uses bridge network by default and compose creates their own is a little bit confusing.

Since I was unable to set bridge network in docker-compose because it apparently is for user defined networks only I've obviously created a network and assigned it to both d run and d-compose.

I think allowing a network to be named, like container_name works today, is valuable. I also think this interacts with #2075 in that you should be able to specify a create_if_missing property that will allow a network to be shared between multiple services, defined in separate compose files, without resulting in a network name conflict.

Our workaround for solving this use case at my company is to wrap docker-compose in a shell script. Essentially:

create_docker_network_if_exists $NETWORK
docker-compose "$@"

Which is invoked with: NETWORK=custom-net ./scripts/docker-compose.sh up [...]. Obviously this is less than ideal and we'd enjoy a more elegant solution.

If any core devs are able to provide me with context on what should be taken into consideration re: implementing a solution, then I'd be more than happy to submit a PR.

And this is a another example of small features request that are demanded by users but "implicitly wontfix" because it seems not valuable for devs... 😕

For me docker-compose.yml is a STATIC way to declare FULL deploy configuration. My case requires specific network names to work properly. I want to write everything into .yml file, but docker-compose is forcing prefix for my networks. I need to make workarounds with totally separate flow to prepare networks manually, mark networks as external in .yml file, and finally whole application.

docker-compose should take care of everything, but it's not doing that...

Adding configurable network name is absolutely the same business-case that container_name - it's have drawbacks that you need to be aware of. Only the notation of network name could be problematic - but it should not be a blocker.

What the progress on this?

Also need this feature, it's like container_name.

Here is the case: each application can consist of several microservices, so each application can have own docker-compose file, but all applications lives in one network.

Fixed by #5448

@shin- ← ok but how use it?

@roboto-cloud

networks:
  foo:
    name: bar

Make sure that you're using version: '3.5' or higher

Thx for the feature
Seems to work only with docker-compose, not docker stack deploy

@ivstas If you want full control over the network name, create it yourself and then reference it as external in your Compose file:

networks:
  custom_network:
    external:
      name: custom_network_name

I am using network alias for this container is there any way to provide that in here?

@roboto-cloud

networks:
  foo:
    name: bar

Make sure that you're using version: '3.5' or higher

Best answer.

I use version 3.5 and I get that property name was unexpected.

ERROR: The Compose file './docker-compose.yml' is invalid because:
networks.foo value Additional properties are not allowed ('name' was unexpected)

I use 3.5 version as well and i get the same error as @onzag

ERROR: The Compose file './docker-compose.yml' is invalid because:
networks.khala value Additional properties are not allowed ('name' was unexpected)
networks:
  khala:
    name: khala
    driver: bridge

@juan-montilla Can you post your full compose file and the docker version? I just tested with Docker version 19.03.8 and a compose file version of 3.8 on macOS and it worked.

@fishtenors sure!

version: "3.5"

networks:
  khala:
    name: khala
    driver: bridge

volumes:
  nginx:
    driver: local

services:
  nginx:
    image: nginx:alpine
    build: ./nginx
    volumes:
      - ~/federation/www:/var/www
    ports:
      - ${NGINX_PORT}:80
      - ${NGINX_SSL_PORT}:443
    environment:
      - NGINX_HOST=${NGINX_HOST}
    networks:
      - khala
Docker version 19.03.11, build 42e35e61f3

I do not know if relevant.. but i am using Ubuntu 18.04

@juan-montilla Your compose file worked for me. Since you're using Docker 19.03+, you could try bumping the version up to 3.8. But that's just taking a stab in the dark. What version of compose are you running (docker-compose --version)? Maybe try upgrading compose to the latest?

Was this page helpful?
0 / 5 - 0 ratings