Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding default external network in docker-compose

I am trying to learn docker and understanding docker-compose

As I was trying out the external network section:

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

I understand that 'my-pre-existing-network' needs to be created.

Is it possible to create a new default external network from within the compose file itself?

This is more from a learning/understanding perspective And also alternative to the docker network create command. Thanks.

like image 913
Vipin Menon Avatar asked Mar 29 '26 16:03

Vipin Menon


2 Answers

First of all, check your version of the file. For version 3.6, the following examples can meet your needs:

Example 1:

version: '3.6'
services:
  webserver:
...
#add existing database network 
networks:
  default:
    external:
      name: proxy_host

Example 2:

version: '3.6'
services:
  webserver:
...
#add existing database network 
networks:
  default:
    name: proxy_host
    external: true

Example 3: This configuration creates new networks.

version: '3.6'
services:
  webserver:
    networks:
      - proxy_host
      - database_host
...
networks:
  proxy_host: {}
  database_host: {}
like image 99
Marin Avatar answered Mar 31 '26 05:03

Marin


In case you create a network within your compose-file then that one is not considered to be "external". You can create a custom network using network section:

version: '3'

services:
    my-service:
    # can be a pre-built image like this or built locally (check reference)
        image: some-image:latest 
        networks:
            - custom-network

networks:
    custom-network:
        driver: bridge

If you are going to use your compose file with swarm, you might want to choose driver: overlay. Additional information can be found here.

like image 23
Farzad Vertigo Avatar answered Mar 31 '26 05:03

Farzad Vertigo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!