Docker 从另一个容器连接到数据库
如果可能的话,请帮助我 我需要用一个数据库启动两个应用程序 我有两份申请。第一个domain.com,第二个api.domain.com。每个应用程序都有docker-compose.yaml文件 domain.com-CMSDocker 从另一个容器连接到数据库,docker,docker-compose,Docker,Docker Compose,如果可能的话,请帮助我 我需要用一个数据库启动两个应用程序 我有两份申请。第一个domain.com,第二个api.domain.com。每个应用程序都有docker-compose.yaml文件 domain.com-CMS version: "3.8" services: web: container_name: domain_web build: context: ./docker/php dockerfile: Docke
version: "3.8"
services:
web:
container_name: domain_web
build:
context: ./docker/php
dockerfile: Dockerfile
working_dir: /var/www/html
#command: composer install
volumes:
- ./:/var/www/html
- ./docker/php/app.conf:/etc/apache2/sites-available/000-default.conf
- ./docker/php/hosts:/etc/hosts
networks:
domain:
ipv4_address: 10.9.0.5
networks:
domain:
driver: bridge
ipam:
config:
- subnet: 10.9.0.0/16
gateway: 10.9.
volumes:
bel_baza:
api.domain.com-Laravel 5.6
version: "3.8"
services:
web:
container_name: api_domain_web
build:
context: ./docker/php
dockerfile: Dockerfile
working_dir: /var/www/html
# command: composer install
volumes:
- ./:/var/www/html
- ./docker/php/app.conf:/etc/apache2/sites-available/000-default.conf
- ./docker/php/hosts:/etc/hosts
networks:
api_domain:
ipv4_address: 10.15.0.5
db:
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
restart: always
container_name: api_domain_db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: domain
MYSQL_USER: user
MYSQL_PASSWORD: user
volumes:
- api_domain_baza:/var/lib/mysql
- ./docker/db:/docker-entrypoint-initdb.d
networks:
api_domain:
ipv4_address: 10.15.0.6
phpmyadmin:
image: phpmyadmin
restart: always
container_name: api_domain_pma
networks:
api_domain:
ipv4_address: 10.15.0.7
redis:
image: redis:3.0
container_name: api_domain_redis
networks:
api_domain:
ipv4_address: 10.15.0.10
networks:
api_domain:
driver: bridge
ipam:
config:
- subnet: 10.15.0.0/16
gateway: 10.15.0.1
volumes:
api_domain_baza:
api_域已成功启动
我需要将domain.com连接到数据库api\u domain\u db
。为了连接主机,我使用了IP地址10.15.0.6。第一个应用程序未从第二个应用程序连接到数据库
我有什么问题
如何将domain.com与第二个应用程序的数据库连接?您的问题是,您正在为每个应用程序使用单独的docker compose应用程序。默认情况下,这些应用程序不能访问彼此的内部部件:
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
医生来了-
因此,它就像为每个docker组合创建单独的网络一样
谢谢你帮了我。
docker network create --subnet 10.1.0.0/24 network_name
networks:
default:
external:
name: network_name
services:
.....
app:
image: ...
networks:
default:
ipv4_address: 10.1.0.10