41

I trying to create network in docker-compose.vs.debug.yml file:

networks:
  myNetwork:
    driver: bridge

But docker adding some identifier:

docker network ls ->

NETWORK ID          NAME                                         DRIVER              SCOPE 
0e1fec1a9a30        dockercompose1163770330_myNetwork            bridge              local

If there any option to name it like this:

NETWORK ID          NAME                                         DRIVER              SCOPE
0e1fec1a9a30        myNetwork                                    bridge              local

I need it to connect automatically two containers on separate projects.

Mark Sabakov
  • 527
  • 1
  • 5
  • 9

4 Answers4

35

Per https://docs.docker.com/compose/networking/:

Networks can also be given a custom name (since version 3.5):

version: "3.5"
networks:
  frontend:
    name: custom_frontend
    driver: custom-driver-1

So you could choose to simply name your network myNetwork like this:

networks:
  myNetwork:
    name: myNetwork
    driver: bridge

Alternatively, if you want your container to join an existing network (I know you say you want docker-compose to create the network for you, but for people reading this question this might be helpful), https://docs.docker.com/compose/networking/ states:

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

networks:
  default:
    external:
      name: my-pre-existing-network
Frans
  • 3,670
  • 1
  • 31
  • 29
21

If you have docker-compose create the network, it will determine the name itself. Normally, it looks at the name of the directory where docker-compose.yml is located, and uses that as a prefix. Based on the name you have shown, it appears that this docker-compose.yml file is in a directory named dockercompose1163770330. It combines this with the myNetwork name you specified, and creates a network named dockercompose1163770330_myNetwork.

If you want to control the exact name of the network, you have two options.

  1. Use a network created outside of docker-compose (an "external" network).
networks:
  default:
    external:
      name: myNetwork

This implies something else has created the network already. If you don't have such a network already, creating it is easy.

docker network create myNetwork
  1. (If possible) determine how visual-studio names it paths, and change that path to something static. Whether this is possible, or how, I have no idea. But if you can control dockercompose1163770330 and make it something different that you prefer, then you can predict the network name created from it.
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
  • Thank you for answer, but i want to automate creation of network from code (from docker-compose.vs.yml file) if network doesn't exist without using cmd. Option with external works only with full network name: dockercompose1163770330_myNetwork, i can't rely that this name will be same because of identifier – Mark Sabakov Mar 12 '17 at 18:29
  • @MarkSabakov It is supposed to create it based on the directory it is found in. I am guessing this identifier is coming in from another tool (e.g. VS2017 that you tagged, or something else). docker-compose by itself gets that name from the name of the directory where it is stored. – Dan Lowe Mar 12 '17 at 19:14
  • @MarkSabakov [Docker Compose can't create the network](https://github.com/docker/compose/issues/2846) for you. – Matt Mar 13 '17 at 03:26
  • @Phil_1984_2 It doesn't have to be an external network, but the OP has tried doing this within compose/visual-studio and it is not creating the network with the name they desire. Using an external network like this allows for controlling the network's exact name because it is created by hand. – Dan Lowe Apr 07 '17 at 13:05
  • @Phil_1984_2 I updated my answer to be a bit more clear about how the current name is created, and why an external network would work around the issue. – Dan Lowe Apr 07 '17 at 13:15
  • It would be nice to be able to fully name the network in the `network` section of `docker-compose`... – steampowered Aug 04 '17 at 20:38
  • 2
    Just a quick tip here that `networks:` is a top-level tag and, therefore, must be at the same top level as the `services:` tag, which means **no indentation**! =:) You will get a syntax error otherwise. FYI: How to check syntax: `docker-compose -f ./dc.yaml config`. – NYCeyes Nov 08 '17 at 21:30
4

I assume you use a VS2017 dcproj. I solved this by creating the network manually in the dcproj project as a PreBuildEvent. Add a prebuild target to the .dcproj file:

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="echo Creating external docker network" />
    <Exec Command="docker network create customnetwork"  IgnoreExitCode="true" />
</Target>

Then in your docker-compose files you can specify an external network:

networks:
  default:
    external:
      name: customnetwork

Requires you to start the dcproj before other projects or add the same prebuild exec to all the projects that should share the network.

doorstuck
  • 2,299
  • 1
  • 22
  • 29
1

As for docker docs here -

"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. You can override the project name with either the --project-name flag or the COMPOSE_PROJECT_NAME environment variable."

A simpler solution to the network name issue is to create a .env file with the following entry

COMPOSE_PROJECT_NAME="myDeployment" 

This will name the default network with the same name.

Gautam
  • 1,030
  • 13
  • 37