gpt4 book ai didi

postgresql - 使用多个postgres数据库为开发和生产创建docker容器

转载 作者:行者123 更新时间:2023-12-02 19:49:04 26 4
gpt4 key购买 nike

我正在尝试使用docker-compose创建2个docker容器用于开发,一个用于prod。这两个容器应链接到单独的postgres数据库。
我尝试了以下操作,但似乎每次都只创建一个容器和一个数据库。

docker-compose.yml

version: "3"
services:
db:
image: postgres:latest
restart: always
container_name: myinstance-postgres-database
environment:
- POSTGRES_USER= dbuser
- POSTGRES_PASSWORD= dbpass
- POSTGRES_DB= ProductionDB
ports:
- 127.17.0.1:5432:5432
volumes:
- myinstance-postgres-db:/var/lib/postgresql/data
app:
image: service/platform:latest
restart: always
container_name: prod-app
environment:
DB_SETUP: "true"
DB_VENDOR: "postgresql"
DB_HOST: db
DB_USER: "dbuser"
DB_PASSWORD: "dbpass"
DB_NAME: "ProductionDB"
DB_WAIT: 10
ports:
- 8443:8443
volumes:
- myinstance-postgres-git:/usr/local/tomcat/webapps/ROOT/WEB-INF/git
depends_on:
- db

volumes:
myinstance-postgres-db:
myinstance-postgres-git:

docker-compose.dev.yml
version: "3"
services:
db:
image: postgres:latest
restart: always
container_name: myinstancedev-postgres-database
environment:
- POSTGRES_USER= dbuser
- POSTGRES_PASSWORD= dbpass
- POSTGRES_DB= DevDB
ports:
- 127.17.0.1:5432:5432
volumes:
- myinstancedev-postgres-db:/var/lib/postgresql/data
app:
image: service/platform:latest
restart: always
container_name: dev-app
environment:
DB_SETUP: "true"
DB_VENDOR: "postgresql"
DB_HOST: db
DB_USER: "dbuser"
DB_PASSWORD: "dbpass"
DB_NAME: "DevDB"
DB_WAIT: 10
ports:
- 8444:8443
volumes:
- myinstancedev-postgres-git:/usr/local/tomcat/webapps/ROOT/WEB-INF/git
depends_on:
- db

volumes:
myinstancedev-postgres-db:
myinstancedev-postgres-git:

然后我运行:
sudo docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d

结果,我有一个容器是dev-app,并且只创建了一个数据库。
有什么办法吗?

最佳答案

如果要同时运行它们,请使用

sudo docker-compose -f docker-compose.yml -p prod up -d && sudo docker-compose -f docker-compose.dev.yml -p dev up -d

当您在同一个 docker-compose命令中传递多个文件时,它不会创建您想要的单独容器。而是合并它们。检查 Share Compose configurations between files and projects

另请注意,主机中可能存在 PORT冲突错误。因为在两个撰写文件中,您都公开了相同的端口 54328443
我的输出在不同端口上有2张 Alpine postgres图像。
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS                    NAMES
74a8e5a8fad0 postgres:alpine "docker-entrypoint.s…" 1 second ago Up Less than a second 0.0.0.0:5430->5432/tcp prod_web_1
23f2b995d499 postgres:alpine "docker-entrypoint.s…" 2 seconds ago Up 2 seconds 0.0.0.0:5431->5432/tcp dev_web_1

另外,请考虑使用 env files for compose

关于postgresql - 使用多个postgres数据库为开发和生产创建docker容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61999589/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com