如何在版本2 docker-compose定义文件中定义网络?

时间:2016-02-29 20:20:59

标签: docker docker-compose

documentation about networking is currently对此非常模糊 - 如何完成创建虚拟网络的docker-compose.yml ,让其定义的服务(容器)在该网络上进行通信?

此场景中的目标不是依赖于预定义的网络,而是为docker-compose定义的容器集合。而是在docker-compose定义文件中自定义网络定义。

使用预定义的网络,如果A中的应用程序使用名称B作为主机名来访问B内部的应用程序,并在其端口9000上进行侦听,则此功能将起作用它会用host:port B:9000mongodb://B:9000(更具体地说,在我的特定情况下为uri 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" 。)

concurrent = 1

[[runners]]
  url = "http://gitlab.xxxxx.com/ci"
  token = "xxxxxxxxxxxxxxxxxxxxxxx"
  tls-skip-verify = false
  tls-ca-file = ""
  name = "XXXXXXX"
  executor = "shell"

但我的观点是在docker-compose配置中定义一个网络,而不是假设一个是先验定义的......

TL; DR

自动创建默认网络。请参阅https://docs.docker.com/compose/networking/的开头部分,了解如何在此网络中处理容器。

2 个答案:

答案 0 :(得分:9)

只要您使用版本2格式,Compose就会为您创建一个default网络,但如果您想要自定义网络,那么文档就在这里:

您可以在Compose文件的顶层创建networks部分,并在每个服务的networks部分中引用它们。但您不需要,只需使用下面评论中所述的默认网络。

答案 1 :(得分:1)

请找到一个示例
其中包括网络定义,流量实现等
Areogear-推送服务器部署

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