macos – 使用共享的MySQL容器

前端之家收集整理的这篇文章主要介绍了macos – 使用共享的MySQL容器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

文艺青年最爱的;试图让wordpress docker-compose容器与另一个docker-compose容器对话.

在我的Mac上,我有一个wordpress&我使用链接MySQL服务器构建和配置的MySQL容器.在生产中我计划使用Google Cloud MysqL存储实例,因此计划从docker-compose文件删除MysqL容器(取消链接),然后将可以在多个docker容器中使用的共享容器分开.

我遇到的问题是我无法将wordpress容器连接到单独的MysqL容器.是否有人能够阐明我将如何解决这个问题?

我尝试创建一个网络并尝试通过/ etc / hosts文件创建一个本地盒引用的固定IP(我的首选配置,因为我可以根据ENV更新文件)

WP:

  1. version: '2'
  2. services:
  3. wordpress:
  4. container_name: spmfrontend
  5. hostname: spmfrontend
  6. domainname: spmfrontend.local
  7. image: wordpress:latest
  8. restart: always
  9. ports:
  10. - 8080:80
  11. # creates an entry in /etc/hosts
  12. extra_hosts:
  13. - "ic-MysqL.local:172.20.0.1"
  14. # Sets up the env,passwords etc
  15. environment:
  16. wordpress_DB_HOST: ic-MysqL.local:9306
  17. wordpress_DB_USER: root
  18. wordpress_DB_PASSWORD: root
  19. wordpress_DB_NAME: wordpress
  20. wordpress_TABLE_PREFIX: spm
  21. # sets the working directory
  22. working_dir: /var/www/html
  23. # creates a link to the volume local to the file
  24. volumes:
  25. - ./wp-content:/var/www/html/wp-content
  26. # Any networks the container should be associated with
  27. networks:
  28. default:
  29. external:
  30. name: ic-network

MysqL的:

  1. version: '2'
  2. services:
  3. MysqL:
  4. container_name: ic-MysqL
  5. hostname: ic-MysqL
  6. domainname: ic-MysqL.local
  7. restart: always
  8. image: MysqL:5.7
  9. ports:
  10. - 9306:3306
  11. # Create a static IP for the container
  12. networks:
  13. ipv4_address: 172.20.0.1
  14. # Sets up the env,passwords etc
  15. environment:
  16. MysqL_ROOT_PASSWORD: root # TODO: Change this
  17. MysqL_USER: root
  18. MysqL_PASS: root
  19. MysqL_DATABASE: wordpress
  20. # saves /var/lib/MysqL to persistant volume
  21. volumes:
  22. - perstvol:/var/lib/MysqL
  23. - backups:/backups
  24. # creates a volume to persist data
  25. volumes:
  26. perstvol:
  27. backups:
  28. # Any networks the container should be associated with
  29. networks:
  30. default:
  31. external:
  32. name: ic-network
最佳答案
你可能想要做的是为两个容器创建一个共享的Docker网络,并将它们指向它.您可以使用docker network create< name>创建网络.我将使用sharednet作为示例,但您可以使用任何您喜欢的名称.

一旦网络到达,您就可以将两个容器指向它.当您使用docker-compose时,您可以在YAML文件底部执行此操作.这将在文件的顶层,即一直到左边,如卷:.

  1. networks:
  2. default:
  3. external:
  4. name: sharednet

要在普通容器(外部compose)上执行相同的操作,可以传递–network参数.

  1. docker run --network sharednet [ ... ]

猜你在找的Docker相关文章