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,如需转载请自行联系原作者

相关文章
|
人工智能 前端开发 Docker
从本地到云端:用 Docker Compose 与 Offload 构建可扩展 AI 智能体
在 AI 智能体开发中,开发者常面临本地调试与云端部署的矛盾。本文介绍如何通过 Docker Compose 与 Docker Offload 解决这一难题,实现从本地快速迭代到云端高效扩容的全流程。内容涵盖多服务协同、容器化配置、GPU 支持及实战案例,助你构建高效、一致的 AI 智能体开发环境。
1219 2
从本地到云端:用 Docker Compose 与 Offload 构建可扩展 AI 智能体
|
运维 数据可视化 开发者
2025年 三个 Docker Compose 可视化管理器测评
本文对比了三款主流的 Docker Compose 可视化管理工具。随着 Docker 的普及,Compose 已成为多容器应用部署的标准,但 YAML 配置复杂、协作困难等问题也日益突出。三款工具各有侧重:Docker Desktop 适合个人本地开发,Portainer 适合小团队运维管理,而 Websoft9 则通过 GitOps 实现了强大的版本控制与团队协作能力。文章从可视化编辑、部署便捷性、版本管理等方面进行评测,为不同使用场景提供了推荐方案,展望了未来 Compose 管理向 GitOps 深度融合的发展趋势。
1686 1
2025年 三个 Docker Compose 可视化管理器测评
|
数据可视化 开发工具 git
GitOps 驱动的 Docker Compose 可视工具化来了,图形化编辑器上玩转容器编排
Docker Compose 简化了多容器应用的部署,但随着应用复杂度上升,文本配置方式逐渐暴露出维护难、协作效率低等问题。基于 GitOps 的可视化 Docker Compose 工具应运而生,通过图形界面降低使用门槛,提升配置准确性和团队协作效率。结合 GitOps,实现配置变更的版本追踪、自动化部署与环境一致性,为多容器应用管理提供高效、安全的解决方案。
|
NoSQL Redis Docker
使用Docker Compose工具进行容器编排的教程
以上就是使用Docker Compose进行容器编排的基础操作。这能帮你更有效地在本地或者在服务器上部署和管理多容器应用。
969 11
|
网络协议 Ubuntu Docker
Docker Compose--命令说明
Docker Compose--命令说明
2501 30
|
网络协议 NoSQL Redis
Docker Compose--模板文件
Docker Compose--模板文件
1561 29
|
Linux Docker Windows
Docker Compose
Docker Compose
2071 29
|
NoSQL 安全 Redis
Docker Compose :从入门到企业级部署
Docker Compose 是用于定义和运行多容器应用的工具,支持服务、网络和卷三大核心要素。通过简洁的 YAML 文件,可实现应用的快速部署与管理,适用于开发、测试及生产环境。
999 1
|
监控 NoSQL 时序数据库
《docker高级篇(大厂进阶):7.Docker容器监控之CAdvisor+InfluxDB+Granfana》包括:原生命令、是什么、compose容器编排,一套带走
《docker高级篇(大厂进阶):7.Docker容器监控之CAdvisor+InfluxDB+Granfana》包括:原生命令、是什么、compose容器编排,一套带走
896 78