Docker Compose Networking

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                         null

You 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: postgres

If 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-1

Custom 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-1

Pre-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-this

In 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.



本文转自 zbill 51CTO博客,原文链接:http://blog.51cto.com/dek701/1983495,如需转载请自行联系原作者

优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/521636

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
Mario,低调大师唯一一个Java游戏作品

Mario,低调大师唯一一个Java游戏作品

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的、适应高吞吐量的数据库方案。

Apache Tomcat7、8、9(Java Web服务器)

Apache Tomcat7、8、9(Java Web服务器)

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

Eclipse(集成开发环境)

Eclipse(集成开发环境)

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。