Node.js docker compose为节点上的Postgres恢复
此错误与相同。但是实现方式不同,那么我将在这里问另一个问题 这是docker compose.yml文件Node.js docker compose为节点上的Postgres恢复,node.js,postgresql,docker,docker-compose,sequelize.js,Node.js,Postgresql,Docker,Docker Compose,Sequelize.js,此错误与相同。但是实现方式不同,那么我将在这里问另一个问题 这是docker compose.yml文件 version: '3' services: server: build: context: . volumes: # Mounts the project directory on the host to /app inside the container, # allowing you to modify the code wit
version: '3'
services:
server:
build:
context: .
volumes:
# Mounts the project directory on the host to /app inside the container,
# allowing you to modify the code without having to rebuild the image.
- .:/app
# Just specify a path and let the Engine create a volume.
# Data present in the base image at the specified mount point will be copied
# over to the new volume upon volume initialization.
# node_modules from this new volume will be used and not from your local dev env.
- /app/node_modules/
# Expose ports [HOST:CONTAINER}
ports:
- "4040:4040"
# Set environment variables from this file
env_file:
- .env
# Overwrite any env var defined in .env file (if required)
environment:
- NODE_ENV=development
# Link to containers in another service.
# Links also express dependency between services in the same way as depends_on,
# so they determine the order of service startup.
links:
- postgres
postgres:
image: "postgres:9.6"
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: 123456
POSTGRES_USER: postgres
POSTGRES_DB: postgres
这是我用来存储数据库信息的database.json
文件
{
"development": {
"username": "postgres",
"password": "123456",
"database": "mydb",
"host": "127.0.0.1",
"dialect": "postgres",
"pool": {
"max": 100,
"min": 0,
"idle": 10000
}
},
"test": {
"username": "postgres",
"password": "123456",
"database": "mytestdb",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"username": "postgres",
"password": "123456",
"database": "mydb",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
并使用Sequelize连接数据库
import database from '../../config/database.json'
const sequelize = new Sequelize(dbConfig.database, dbConfig.username, dbConfig.password, dbConfig)
我知道,当我在容器中运行应用程序时,它们并非都在locahost上,然后我必须更改主机,但我可以在这里更改。我通过更新host
到postgres
来解决这个问题。这是可行的,但解决方案不是我想要的
顺便问一下,我如何在这里创建DB
postgres|u 1 |致命:数据库“starflow”不存在
您需要做两件事。一种是在DB的网络上移动应用程序,这样DB就可以在主机上使用。这需要在服务中添加网络模式。请参阅更新的yaml
version: '3'
services:
server:
build:
context: .
volumes:
# Mounts the project directory on the host to /app inside the container,
# allowing you to modify the code without having to rebuild the image.
- .:/app
# Just specify a path and let the Engine create a volume.
# Data present in the base image at the specified mount point will be copied
# over to the new volume upon volume initialization.
# node_modules from this new volume will be used and not from your local dev env.
- /app/node_modules/
# Expose ports [HOST:CONTAINER}
# ports:
# - "4040:4040"
network_mode: service:postgres
# Set environment variables from this file
env_file:
- .env
# Overwrite any env var defined in .env file (if required)
environment:
- NODE_ENV=development
# Link to containers in another service.
# Links also express dependency between services in the same way as depends_on,
# so they determine the order of service startup.
links:
- postgres
postgres:
image: "postgres:9.6"
ports:
- "5432:5432"
- "4040:4040"
environment:
POSTGRES_PASSWORD: 123456
POSTGRES_USER: postgres
POSTGRES_DB: postgres
请注意,端口将移动到提供网络的服务。我们在postgres
网络上运行server
服务。这样,两者都可以在本地主机上相互访问,并且不需要在环境配置中进行任何更改
这仅建议在开发或测试环境中使用,而不建议在生产环境中使用。因此,如果您正在开发生产中使用的docker部署,请不要使用这种方法
接下来要自定义postgres图像以创建不同的数据库,请按照以下图像文档进行操作
如何扩展此图像
如果要在从此映像派生的映像中执行其他初始化,请在/docker entrypoint initdb.d下添加一个或多个*.sql、*.sql.gz或*.sh脚本(如有必要,请创建目录)。在入口点调用initdb创建默认postgres用户和数据库之后,它将运行任何*.sql文件,并在该目录中找到任何*.sh脚本,以便在启动服务之前进行进一步初始化
例如,要添加其他用户和数据库,请将以下内容添加到/docker-entrypoint-initdb.d/init-user-db.sh:
#/bin/bash
set-e
psql-v ON_ERROR_STOP=1——用户名“$POSTGRES_USER”您的主机不必在不同的环境中更改。应该按照docker compose.yaml
中的定义为它分配pgsql服务的名称,在本例中,它是postgres
也就是说,如果您希望不必在数据库.json
文件中硬编码任何特定于环境的参数,可以将它们拆分为不同的数据库.json
文件,并使用其他特定于环境的合成文件扩展docker compose.yml
例如,您可以将数据库.json
拆分为db-dev.json
、db staging.json
和db-prod.json
然后定义特定于环境的组合文件,以装载不同的文件。比如说,
# dbconfig-dev.yml
services:
server:
volumes:
- ./config/db-dev.json:/app/
# dbconfig-staging.yml
services:
server:
volumes:
- ./config/db-staging.json:/app/
# dbconfig-prod.yml
services:
server:
volumes:
- ./config/db-prod.json:/app/
请注意,这些合成文件不是完整的合成定义,因为它们只包含相关的卷
片段
然后,您可以通过执行以下操作扩展原始的docker compose.yaml
:
$ docker-compose -f docker-compose.yaml -f dbconfig-dev.yaml up
您可以在撰写中了解更多信息。那么您想要什么解决方案呢?因为将host
更改为postgres
是我需要为docker创建新环境的最佳方式?。只希望使用开发在本地运行服务器。Docker本身在新环境中运行。当容器第一次运行时,您需要配置该环境。
$ docker-compose -f docker-compose.yaml -f dbconfig-dev.yaml up