docker 容器中的服务器无法连接到另一个 docker 容器中的 postgres 数据库
Server in docker container unable to connect to postgres database in another docker container
我有一个节点服务器正在尝试使用 knex. Both are in docker containers with the same custom network. I keep getting ECONNREFUSED errors. I've poked around in the database container and I see that my DB (test_db
) has been created by psql but it has no permissions. After giving root
permissions, I'm still getting the same issues. I've tried removing the volumes with docker-compose down -v
but still no luck. I've also tried removing knex and just using node-postgres 连接到 postgres 数据库,但出现相同的错误。我也无法使用 pgadmin 从主机连接到 Db。将不胜感激任何帮助!
这是我的 docker-compose.yml
version: "3"
services:
server:
build:
context: .
dockerfile: development.Dockerfile
ports:
- "8081:8081"
volumes:
- .:/src
- /src/node_modules
networks:
- dev-network
environment:
DB_HOSTNAME: pg-development
DB_USER: root
DB_PASSWORD: helloworld
DB_PORT: 3306
DB_NAME: test_dev
depends_on:
- pg-development
pg-development:
image: postgres
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: helloworld
POSTGRES_DB: test_dev
ports:
- "3308:3306"
volumes:
- dbdata:/data/db
networks:
- dev-network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U root -d test_dev"]
interval: 10s
timeout: 2s
retries: 10
networks:
dev-network:
driver: bridge
volumes:
dbdata:
这是我的数据库连接
import knex from "knex";
const connection = {
host: process.env.DB_HOSTNAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
port: Number(process.env.DB_PORT),
database: process.env.DB_USER,
};
const db = knex({
client: "pg",
connection,
debug: true,
pool: {
min: 0,
max: 50,
afterCreate: function (conn, done) {
conn.query('SET timezone="UTC";', function (err) {
if (err) {
done(err, conn);
}
});
},
},
});
这是因为您在 pg-development
容器中公开了错误的端口:
https://docs.docker.com/compose/reference/port/
port [options] SERVICE PRIVATE_PORT
你在数据库中暴露的端口是端口3308
,正确的配置应该是:
...
ports:
- "3306:3306"
...
这不起作用的原因是因为数据库实际上是在默认的 postgres 端口 5432 上启动的。结果是 docker-compose.yml 你需要添加一个命令到更改默认端口。
pg-development:
image: postgres
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: helloworld
POSTGRES_DB: test_dev
ports:
- "3308:3306"
volumes:
- dbdata:/data/db
networks:
- dev-network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U root -d test_dev"]
interval: 10s
timeout: 2s
retries: 10
command: -p 3306 // this fixes the issue
这在 docker-compose 中对我有用,其中一个容器是 postgresqldb,另一个是 .net core 3.1 容器。
#docker-compose.yml
version: '3.4'
services:
customerdb:
image: postgres
customer.api:
image: ${DOCKER_REGISTRY-}customerapi
build:
context: .
dockerfile: Customer/Customer.API/Dockerfile
docker-compose.override.yml
customerdb:
container_name: customerdb
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
POSTGRES_USER: postgres
ports:
- 5100:5432
volumes:
- ./postgres-data1:/var/lib/postgresql/data
customer.api:
container_name: customerapi
environment:
- ASPNETCORE_ENVIRONMENT=Development
- "ConnectionStrings:CustomerConnection=host=host.docker.internal;port=5100;database=CustomerDB;username=postgres;password=postgres"
depends_on:
- customerdb
volumes:
- ${HOME}/.microsoft/usersecrets/:/root/.microsoft/usersecrets
- ${HOME}/.aspnet/https:/root/.aspnet/https
ports:
- "8000:80"
我使用 ConnectionStrings:CustomerConnection=host=... 因为我曾经 app.config 作为连接字符串。
我有一个节点服务器正在尝试使用 knex. Both are in docker containers with the same custom network. I keep getting ECONNREFUSED errors. I've poked around in the database container and I see that my DB (test_db
) has been created by psql but it has no permissions. After giving root
permissions, I'm still getting the same issues. I've tried removing the volumes with docker-compose down -v
but still no luck. I've also tried removing knex and just using node-postgres 连接到 postgres 数据库,但出现相同的错误。我也无法使用 pgadmin 从主机连接到 Db。将不胜感激任何帮助!
这是我的 docker-compose.yml
version: "3"
services:
server:
build:
context: .
dockerfile: development.Dockerfile
ports:
- "8081:8081"
volumes:
- .:/src
- /src/node_modules
networks:
- dev-network
environment:
DB_HOSTNAME: pg-development
DB_USER: root
DB_PASSWORD: helloworld
DB_PORT: 3306
DB_NAME: test_dev
depends_on:
- pg-development
pg-development:
image: postgres
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: helloworld
POSTGRES_DB: test_dev
ports:
- "3308:3306"
volumes:
- dbdata:/data/db
networks:
- dev-network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U root -d test_dev"]
interval: 10s
timeout: 2s
retries: 10
networks:
dev-network:
driver: bridge
volumes:
dbdata:
这是我的数据库连接
import knex from "knex";
const connection = {
host: process.env.DB_HOSTNAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
port: Number(process.env.DB_PORT),
database: process.env.DB_USER,
};
const db = knex({
client: "pg",
connection,
debug: true,
pool: {
min: 0,
max: 50,
afterCreate: function (conn, done) {
conn.query('SET timezone="UTC";', function (err) {
if (err) {
done(err, conn);
}
});
},
},
});
这是因为您在 pg-development
容器中公开了错误的端口:
https://docs.docker.com/compose/reference/port/
port [options] SERVICE PRIVATE_PORT
你在数据库中暴露的端口是端口3308
,正确的配置应该是:
...
ports:
- "3306:3306"
...
这不起作用的原因是因为数据库实际上是在默认的 postgres 端口 5432 上启动的。结果是 docker-compose.yml 你需要添加一个命令到更改默认端口。
pg-development:
image: postgres
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: helloworld
POSTGRES_DB: test_dev
ports:
- "3308:3306"
volumes:
- dbdata:/data/db
networks:
- dev-network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U root -d test_dev"]
interval: 10s
timeout: 2s
retries: 10
command: -p 3306 // this fixes the issue
这在 docker-compose 中对我有用,其中一个容器是 postgresqldb,另一个是 .net core 3.1 容器。
#docker-compose.yml
version: '3.4'
services:
customerdb:
image: postgres
customer.api:
image: ${DOCKER_REGISTRY-}customerapi
build:
context: .
dockerfile: Customer/Customer.API/Dockerfile
docker-compose.override.yml
customerdb:
container_name: customerdb
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
POSTGRES_USER: postgres
ports:
- 5100:5432
volumes:
- ./postgres-data1:/var/lib/postgresql/data
customer.api:
container_name: customerapi
environment:
- ASPNETCORE_ENVIRONMENT=Development
- "ConnectionStrings:CustomerConnection=host=host.docker.internal;port=5100;database=CustomerDB;username=postgres;password=postgres"
depends_on:
- customerdb
volumes:
- ${HOME}/.microsoft/usersecrets/:/root/.microsoft/usersecrets
- ${HOME}/.aspnet/https:/root/.aspnet/https
ports:
- "8000:80"
我使用 ConnectionStrings:CustomerConnection=host=... 因为我曾经 app.config 作为连接字符串。