Docker Compose无法连接到数据库 [英] Docker Compose cannot connect to database

查看:107
本文介绍了Docker Compose无法连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将nestjs用于后端,并将typeorm用作ORM.我试图在docker-compose文件中定义数据库和应用程序.

I'm using nestjs for my backend and using typeorm as ORM. I tried to define my database and my application in an docker-compose file.

如果我将数据库作为容器运行,而我的应用程序是从本地计算机运行的,则它运行良好.我的程序连接并创建表等.

If I'm running my database as a container and my application from my local machine it works well. My program connects and creates the tables etc.

但是,如果我尝试从容器内部连接数据库或使用docker-compose up来启动容器,则会失败.

But if I try to connect the database from within my container or to start the container with docker-compose up it fails.

始终会收到ECONNREFUSED错误.

Always get an ECONNREFUSED Error.

我的错误在哪里?

docker-compose.yml

docker-compose.yml

version: '3.1'
volumes:
 dbdata:

services:
  db:
    image: postgres:10
    volumes:
      - ./dbData/:/var/lib/postgresql/data
    restart: always
    environment:
      - POSTGRES_PASSWORD=${TYPEORM_PASSWORD}
      - POSTGRES_USER=${TYPEORM_USERNAME}
      - POSTGRES_DB=${TYPEORM_DATABASE}
    ports:
      - ${TYPEORM_PORT}:5432

  backend:
    build: .
    ports:
      - "3001:3000"
    command: npm run start
    volumes:
      - .:/src

Dockerfile

Dockerfile

FROM node:10.5

WORKDIR /home

# Bundle app source
COPY . /home

# Install app dependencies
#RUN npm install -g nodemon
# If you are building your code for production
# RUN npm install --only=production
RUN npm i -g @nestjs/cli
RUN npm install

EXPOSE 3000

.env

# .env
HOST=localhost
PORT=3000
NODE_ENV=development
LOG_LEVEL=debug

TYPEORM_CONNECTION=postgres
TYPEORM_HOST=localhost
TYPEORM_USERNAME=postgres
TYPEORM_PASSWORD=postgres
TYPEORM_DATABASE=mariokart
TYPEORM_PORT=5432
TYPEORM_SYNCHRONIZE=true
TYPEORM_DROP_SCHEMA=true
TYPEORM_LOGGING=all
TYPEORM_ENTITIES=src/database/entity/*.ts
TYPEORM_MIGRATIONS=src/database/migrations/**/*.ts
TYPEORM_SUBSCRIBERS=src/database/subscribers/**/*.ts

我尝试使用链接,但在容器中不起作用.

I tried to use links but it don't work in the container.

推荐答案

看看后端容器内的/etc/hosts .您会看到

Take a look at your /etc/hosts inside the backend container. You will see

192.0.18.1    dir_db_1

或类似的东西.IP将不同,并且 dir 将代表您所在的目录.因此,必须将 TYPEORM_HOST = localhost 更改为 TYPEORM_HOST = dir_db_1 .

or something like that. The IP will be different and dir will represent the dir you're in. Therefore, you must change TYPEORM_HOST=localhost to TYPEORM_HOST=dir_db_1.

尽管如此,我建议您为容器设置静态名称.

Although, I suggest you set static names to your containers.

services:
  db:
    container_name: project_db
    ...
  backend:
    container_name: project_backend

在这种情况下,您始终可以确定容器将具有静态名称,并且可以设置 TYPEORM_HOST = project_db ,而不必再担心该名称了.

In this case you can always be sure, that your container will have a static name and you can set TYPEORM_HOST=project_db and never worry about the name ever again.

这篇关于Docker Compose无法连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆