文艺青年最爱的;试图让wordpress docker-compose容器与另一个docker-compose容器对话.
在我的Mac上,我有一个wordpress&我使用链接的MySQL服务器构建和配置的MySQL容器.在生产中我计划使用Google Cloud MysqL存储实例,因此计划从docker-compose文件中删除MysqL容器(取消链接),然后将可以在多个docker容器中使用的共享容器分开.
我遇到的问题是我无法将wordpress容器连接到单独的MysqL容器.是否有人能够阐明我将如何解决这个问题?
我尝试创建一个网络并尝试通过/ etc / hosts文件创建一个本地盒引用的固定IP(我的首选配置,因为我可以根据ENV更新文件)
WP:
version: '2'
services:
wordpress:
container_name: spmfrontend
hostname: spmfrontend
domainname: spmfrontend.local
image: wordpress:latest
restart: always
ports:
- 8080:80
# creates an entry in /etc/hosts
extra_hosts:
- "ic-MysqL.local:172.20.0.1"
# Sets up the env,passwords etc
environment:
wordpress_DB_HOST: ic-MysqL.local:9306
wordpress_DB_USER: root
wordpress_DB_PASSWORD: root
wordpress_DB_NAME: wordpress
wordpress_TABLE_PREFIX: spm
# sets the working directory
working_dir: /var/www/html
# creates a link to the volume local to the file
volumes:
- ./wp-content:/var/www/html/wp-content
# Any networks the container should be associated with
networks:
default:
external:
name: ic-network
MysqL的:
version: '2'
services:
MysqL:
container_name: ic-MysqL
hostname: ic-MysqL
domainname: ic-MysqL.local
restart: always
image: MysqL:5.7
ports:
- 9306:3306
# Create a static IP for the container
networks:
ipv4_address: 172.20.0.1
# Sets up the env,passwords etc
environment:
MysqL_ROOT_PASSWORD: root # TODO: Change this
MysqL_USER: root
MysqL_PASS: root
MysqL_DATABASE: wordpress
# saves /var/lib/MysqL to persistant volume
volumes:
- perstvol:/var/lib/MysqL
- backups:/backups
# creates a volume to persist data
volumes:
perstvol:
backups:
# Any networks the container should be associated with
networks:
default:
external:
name: ic-network
最佳答案
你可能想要做的是为两个容器创建一个共享的Docker网络,并将它们指向它.您可以使用docker network create< name>创建网络.我将使用sharednet作为示例,但您可以使用任何您喜欢的名称.
原文链接:https://www.f2er.com/docker/435765.html一旦网络到达,您就可以将两个容器指向它.当您使用docker-compose时,您可以在YAML文件的底部执行此操作.这将在文件的顶层,即一直到左边,如卷:.
networks:
default:
external:
name: sharednet
要在普通容器(外部compose)上执行相同的操作,可以传递–network参数.
docker run --network sharednet [ ... ]