docker-compose外部容器

前端之家收集整理的这篇文章主要介绍了docker-compose外部容器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我使用以下命令启动了一个docker容器

docker run --name MysqL --restart always -p 3306:3306 -v /var/lib/MysqL:/var/lib/MysqL -e MysqL_ROOT_PASSWORD=password -d MysqL:5.7.14

然后想将wordpress网站与以下docker-compose.yml文件连接起来

version: '2'

services:
    wordpress:
        image: wordpress
        external_links:
            - MysqL:MysqL
        ports:
            - 80:80
    environment:
        wordpress_DB_USER: root
        wordpress_DB_PASSWORD: password
    volumes:
        - /var/www/somesite.com:/var/www/html

但我不断收到以下错误

Starting somesitecom_wordpress_1
Attaching to somesitecom_wordpress_1
wordpress_1  | 
wordpress_1  | Warning: MysqLi::MysqLi(): (HY000/2002): Connection refused in - on line 19
wordpress_1  | 
wordpress_1  | MysqL Connection Error: (2002) Connection refused

似乎external_links不起作用.

知道我做错了什么吗?

最佳答案
您的链接正常,但您在Docker内的不同网络上.从docker-compose.yml docs

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

解决此问题,您可以创建自己的网络:

docker network create dbnet
docker network connect dbnet MysqL

然后使用以下命令配置docker-compose.yml:

version: '2'

networks:
  dbnet:
    external:
      name: dbnet

services:
    wordpress:
        image: wordpress
        ports:
            - 80:80
        environment:
            wordpress_DB_USER: root
            wordpress_DB_PASSWORD: password
        volumes:
            - /var/www/somesite.com:/var/www/html
        networks:
          - dbnet

请注意,对于最新版本的Docker,您不需要链接容器,DNS服务应该为您执行名称解析.

猜你在找的Docker相关文章