Reputation: 13746
The documentation about networking is currently very vague on this ― how do you accomplish a docker-compose.yml
that creates a virtual network, letting the services (containers) defined by it communicate on that network?
Goal in this scenario being not relying on a pre-defined network, for an ensemble of containers defined for docker-compose. Rather have the network definition self-contained in the docker-compose definition file.
With a pre-defined network, this below would work if the application in A
used the name B
as the hostname for accessing the application packaged inside B
listening on its port 9000. The host:port
it would use for it would be B:9000
(more specifically the uri mongodb://B:9000
in my particular case).
foo:
net: my-pre-defined-network
container_name: A
image: foo
bar:
net: my-pre-defined-network
container_name: B
image: bar
ports:
- "9000:9000"
But my point is defining a network inside the docker-compose configuration, not assuming one was a-priori defined...
A default network is automatically created. See the beginning section of https://docs.docker.com/compose/networking/ for how to address containers within this network.
Upvotes: 12
Views: 47431
Reputation: 3615
Please find below example which include network definition, traffic implementation etc.
version: '2.2'
services:
unifiedpushserver:
image: docker.io/aerogear/unifiedpush-wildfly:2.1.0
networks:
- network
volumes:
- ./helper:/ups-helper:z
entrypoint: "/ups-helper/exportKeycloakHost.sh"
depends_on:
unifiedpushDB:
condition: service_healthy
environment: <br>
POSTGRES_SERVICE_HOST: ${POSTGRES_SERVICE_HOST}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_SERVICE_PORT: ${POSTGRES_SERVICE_PORT}
POSTGRES_DATABASE: ${POSTGRES_DATABASE}
KEYCLOAK_SERVICE_HOST: ${KEYCLOAK_SERVICE_HOST}
KEYCLOAK_SERVICE_PORT: ${KEYCLOAK_SERVICE_PORT}
labels:
traefik.backend: "aerogear"
traefik.docker.network: "appliance"
traefik.domain: "notification.com"
traefik.enable: "true"
traefik.frontend.entryPoints: "http, https"
traefik.frontend.redirect: "false"
traefik.frontend.rule: "Host: notification.com"
links:
- unifiedpushDB:unifiedpush
- keycloakServer:keycloak
ports:
- 9999:8080
unifiedpushDB:
image: postgres:9.6
networks:
- network
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DATABASE}
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
timeout: 20s
retries: 10
keycloakServer:
networks:
- network
image: docker.io/jboss/keycloak:4.1.0.Final
command: "-b 0.0.0.0 -Dkeycloak.import=/ups-keycloak-config/ups-realm-sample.json"
volumes:
- ./keycloak-realm:/ups-keycloak-config:z
environment:
KEYCLOAK_USER: ${KEYCLOAK_USER}
KEYCLOAK_PASSWORD: ${KEYCLOAK_PASSWORD}
networks:
network:
external:
name: appliance
Upvotes: 1
Reputation: 28160
Compose will create a default
network for you as long as you use the version 2 format, but if you'd like to customize the networks the docs are here:
You can create a networks
section at the top level of the Compose file and reference them in the networks
section of each service. But you don't need to, just use the default network as described in the comments below.
Upvotes: 8