Reputation: 3302
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.
Upvotes: 9
Views: 28722
Reputation: 71
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: {}
Upvotes: 7
Reputation: 39
Maybe someone still looking for answer. Probably this will work.
version: '3.8'
services:
api:
networks:
- test
networks:
test:
driver: bridge
external: true
Upvotes: 3
Reputation: 4132
Ref. the "use a pre-existing network" documentation: "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." - docker-compose
will not attempt to create the network.
The external
network must already exist (e.g. "my-pre-existing-network
"), it could be a docker network
created from a different docker-compose
environment or a docker network
created using the docker network create
command.
Note: docker-compose
networks are prefixed with COMPOSE_PROJECT_NAME
. You can use docker network ls
to list existing networks.
Upvotes: 2
Reputation: 2818
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.
Upvotes: 3