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.
Manage a user-defined bridgeUse the docker network create command to create a user-defined bridge network. You can specify the subnet, the IP address range, the gateway, and other options. See the docker network create reference or the output of docker network create --help for details.
If you want to add a container to a network after the container is already running, use the docker network connect subcommand. You can connect multiple containers to the same network. Once connected, the containers can communicate using only another container's IP address or name.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With