docker-compose +外部容器 [英] docker-compose + external container
问题描述
我已经使用以下命令启动了一个Docker容器
I have started a docker container with the following command
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文件
and then would like to connect a wordpress site with the following docker-compose.yml file
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
但是我不断收到以下错误消息
But I keep getting the following error
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文档:
Your link is working, but you're on separate networks inside of Docker. From the docker-compose.yml docs:
注意:如果您使用的是版本2文件格式,则外部创建的容器必须至少连接到与其链接的服务相同的网络之一.
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.
要解决此问题,您可以创建自己的网络:
To solve this, you can create your own network:
docker network create dbnet
docker network connect dbnet mysql
然后使用以下命令配置您的docker-compose.yml:
Then configure your docker-compose.yml with:
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服务应为您进行名称解析.
Note with recent versions of Docker, you shouldn't need to link the containers, the DNS service should do the name resolution for you.
这篇关于docker-compose +外部容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!