设为首页 收藏本站
查看: 428|回复: 0

[经验分享] Docker Compose Networking

[复制链接]
发表于 2018-5-27 06:17:11 | 显示全部楼层 |阅读模式
Docker Compose Networking
Docker Compose sets up a single network for your application(s) by default, adding each container for a service to the default network. Containers on a single network can reach and discover every other container on the network.
Networking Basics
Running the command docker network ls will list out your current Docker networks; it should look similar to the following:
$ docker network ls
NETWORK ID          NAME                         DRIVER
17cc61328fef        bridge                       bridge
098520f7fce0        composedjango_default        bridge
1ce3c572afc6        composeflask_default         bridge
8fd07d456e6c        host                         host
3b578b919641        none                         nullYou can alter the network name with the -p or --project-name flags or the COMPOSE_PROJECT_NAME environment variable. (In the event you need to run multiple projects on a single host, it’s recommended to set project names via the flag.)
In our compose_django example, web can access the PostgreSQL database from postgres://postgres:5432. We can access web from the outside world via port 8000 on the Docker host (only because the web service explicitly maps port 8000.
Updating Containers on the Network
You can change service configurations via the Docker Compose file. When you run docker-compose up to update the containers, Compose removes the old container and inserts a new one. The new container has a different IP address than the old one, but they have the same name. Containers with open connections to the old container close those connections, look up the new container by its name, and connect.
Linking Containers
You may define additional aliases that services can use to reach one another. Services on the same network can already reach one another. In the example below, we allow web to reach db via one of two hostnames (db or database):
version: '2'services:
    web:
        build: .
        links:
            - "db:database"
    db:
        image: postgresIf you do not specify a second hostname (for example, - db instead of - "db:database"), Docker Compose uses the service name (db). Links express dependency like depends_ondoes, meaning links dictate the order of service startup.
Networking with Multiple Hosts
You may use the overlay driver when deploying Docker Compose to a Swarm cluster. We’ll cover more on Docker Swarm in a future article.
Configuring the Default Network
If you desire, you can configure the default network instead of (or in addition to) customizing your own network. Simply define a default entry under networks:
verision: '2'services:
    web:
        build: .
        ports:
            - "8000:8000"
    db:
        image: postgresnetworks:
    default:
        driver: custom-driver-1Custom Networks
Specify your own networks with the top-level networks key, to allow creating more complex topologies and specify network drivers (and options). You can also use this configuration to connect services with external networks Docker Compose does not manage. Each service can specify which networks to connect to with its service-level networks key.
The following example defines two custom networks. Keep in mind, proxy cannot connect to db, as they do not share a network; however, app can connect to both. In the frontnetwork, we specify the IPv4 and IPv6 addresses to use (we have to configure an ipam block defining the subnet and gateway configurations). We could customize either network or neither one, but we do want to use separate drivers to separate the networks (review Basic Networking with Docker for a refresher):
version: '2'services:
    proxy:
        build: ./proxy
        networks:
            - front
    app:
        build: ./app
        networks:
            # you may set custom IP addresses
            front:
                ipv4_address: 172.16.238.10
                ipv6_address: "2001:3984:3989::10"
            - back
    db:
        image: postgres
        networks:
            - backnetworks:
    front:
        # use the bridge driver, but enable IPv6
        driver: bridge
        driver_opts:
            com.docker.network.enable_ipv6: "true"
        ipam:
            driver: default
            config:
                - subnet: 172.16.238.0/24
                gateway: 172.16.238.1
                - subnet: "2001:3984:3989::/64"
                gateway: "2001:3984:3989::1"
    back:
        # use a custom driver, with no options
        driver: custom-driver-1Pre-Existing Networks
You can even use pre-existing networks with Docker Compose; just use the external option:
version: '2'networks:
    default:
        external:
            name: i-already-created-thisIn this case, Docker Compose never creates the default network; instead connecting the app’s containers to the i-already-created-this network.
Common Issues
You’ll need to use version 2 of the Compose file format. (If you follow along with these tutorials, you already do.) Legacy (version 1) Compose files do not support networking. You can determine the version from the version: line in the docker-compose.yml file.
General YAML
Use quotes (“” or ‘’) whenever you have a colon (:) in your configuration values, to avoid confusion with key-value pairs.
Updating Containers
Container IP addresses change on update. Reference containers by name, not IP, whenever possible. Otherwise you’ll need to update the IP address you use.
Links
If you define both links and networks, linked services must share at least one network to communicate.
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-481518-1-1.html 上篇帖子: Kubernetes(K8S)集群管理Docker容器(部署篇) 下篇帖子: 运行第一个 Service
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表