[英]server and postgresql cannot connect when both running on Docker `getaddrinfo ENOTFOUND`
after creating docker containers with docker compose file (below), I call使用 docker compose 文件(如下)创建 docker 容器后,我调用
$ docker run myApp
However, I get但是,我得到
Error: getaddrinfo ENOTFOUND main_db
this only happens when both server and postgresql are in docker containers (I am able to connect to postgresql on localhost)这仅在服务器和 postgresql 都在 docker 容器中时发生(我能够连接到本地主机上的 postgresql)
I'm running a NestJS app using TypeOrm to connect to a postgresql server我正在使用 TypeOrm 运行 NestJS 应用程序以连接到 postgresql 服务器
inside the app.module.ts
where it boots up the connection my config should match my docker postgresql config.在它启动连接的
app.module.ts
内,我的配置应该与我的 docker postgresql 配置相匹配。 the host
points to the container I created on docker main_db
and I declared this as a dependency of my server, the main
service. host
指向我在main_db
上创建的容器,我将其声明为我的服务器( main
服务)的依赖项。 Everything should be on the same network webnet
.:一切都应该在同一个网络
webnet
。:
TypeOrmModule.forRoot({
type: 'postgres',
host: 'main_db',
port: +process.env.POSTGRES_PORT,
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
autoLoadEntities: true,
synchronize: true,
logging: dbLogging,
}),
docker-compose.yml
version: '3.7'
services:
main:
container_name: main
build:
context: .
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- ${SERVER_PORT}:${SERVER_PORT}
- 9229:9229
command: npm run start:dev
env_file:
- .env
networks:
- webnet
depends_on:
- main_db
main_db:
container_name: main_db
image: postgres:12
restart: always
networks:
- webnet
environment:
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_USER: ${POSTGRES_USER}
PG_DATA: /var/lib/postgresql/data
ports:
- '${POSTGRES_PORT}:${POSTGRES_PORT}'
volumes:
- pgdata:/var/lib/postgresql/data
networks:
webnet:
volumes:
pgdata:
.env
file .env
文件
POSTGRES_PORT=5432
POSTGRES_USER=test
POSTGRES_PASSWORD=test
POSTGRES_DB=test
SERVER_PORT=3001
When creating a Dockerfile
and a docker-compose.yml
file and you call docker run myApp
for the app defined inside of the Dockerfile
instead of calling docker-compose up
, you will see the app running;当创建一个
Dockerfile
和一个Dockerfile
docker-compose.yml
文件并且你为在Dockerfile
定义的应用程序调用docker-compose.yml
docker run myApp
而不是调用Dockerfile
docker-compose up
,你会看到应用程序正在运行; however, it will not start the containers defined in the docker-compose
file.但是,它不会启动
docker-compose
文件中定义的容器。 In the case of the NestJS server, it was running the server, but could not find the container with the database, since this container was not being spun up.在 NestJS 服务器的情况下,它正在运行服务器,但找不到带有数据库的容器,因为这个容器没有被启动。 Although the distinction between the app setup in the
Dockerfile
and the definition of the containers in the docker-compose.yml
was clear, I didn't realize that the docker
command didn't reference the docker-compose.yml
.虽然在应用程序设置之间的区别
Dockerfile
,并在容器的定义docker-compose.yml
是明确的,我不知道该docker
命令并没有提及该docker-compose.yml
。 Thus, posting here in case anyone else has a similar confusion.因此,在这里发布以防其他人有类似的困惑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.