docker-compose 中的 external-links

在使用服务时使用同一台服务器的 mysql 的 docker 服务。想到 external-links 这个配置。

按照文档的配置添加了如下配置

1
2
3
external_links:
- mysql_mysql_1:mysql

但是配置无论如何都不成功。

https://docs.docker.com/compose/compose-file/#external_links

官方文档相关的描述,

If you’re using the version 2 or above file format, the externally-created containers must be connected to at least one of the same networks as the service that is linking to them.

也就是说想要使用 external_links 是需要两个服务在同一个网络段,这里只提了一句,对第一次写配置文件的开发者非常的不友好。

  1. 创建网络

    1
    2
    3
    $ docker network create -d bridge my_default
    cb1e8d02683ad3dc0bff2881dccb2d3b05cab855b1d463d2cb45685a97dd196b

  2. 给mysql 服务添加网络主要是最后一段

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    version: '2'
    services:
    mysql:
    image: mysql:5.7
    networks:
    - my_default
    networks:
    my_default:
    external: true

  3. 给服务添加网络

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    services:
    project:
    networks:
    - my_default
    external_links:
    - mysql_mysql_1:mysql
    - redis_redis_1:redis
    networks:
    my_default:
    external: true

以上的配置中删除了和网络配置不相关的配置。

docker-compose 中的 external-links

https://beixiu.net/dev/docker-compose-external-links/

作者

张巍

发布于

2018-12-26

更新于

2018-12-26

许可协议

评论