Home>

Hello everyone. I am writing a server on nest, using postgres and trying to connect elasticsearch. In order to test the latter, as I understand it, you need to start a docker container. I sort of did everything, but when starting the container, an error occursERROR [ExceptionHandler] connect ECONNREFUSED 127.0.0.1:5432... I understand that there are many questions with a similar error, but I have not yet found anything suitable for myself + before that everything worked fine. The only thing I thought about db migrations. Can they somehow interfere with connecting to the database?

.env

PORT= 5000
POSTGRES_HOST= localhost
POSTGRES_USER= postgres
POSTGRES_DB= cp-itr
POSTGRES_PASSWORD= 12345
POSTGRES_PORT= 5432
SECRET= SECRET3
ELASTICSEARCH_NODE= http: //localhost: 9200
ELASTICSEARCH_INDEX= tasks

Dockerfile

FROM node: 12.13-alpine
WORKDIR /app
COPY package * .json ./
RUN npm install
COPY. ...
COPY ./dist ./dist
CMD ["npm", "run", "start: dev"]

docker-compose

version: '3.0'
services:
    main:
        container_name: main_nest
        build:
            context:.
        env_file:
            -.development.env
        volumes:
            -.: /app
            -/app /node_modules
        ports:
            -5000: 5000
            -9229: 9229
        command: npm run start: dev
        depends_on:
            -db_postgres
        restart: always
    db_postgres:
        container_name: db_postgres
        image: postgres: 13
        env_file:
            -.development.env
        environment:
            PG_DATA: /var /lib /postgresql /data
        ports:
            -5432: 5432
        volumes:
            -pgdata: /var /lib /postgresql /data
        restart: always
    nest-elasticsearch:
        container_name: nest-elasticsearch
        image: docker.elastic.co/elasticsearch/elasticsearch:7.0.1
        healthcheck:
            test: ["CMD-SHELL", "curl --silent --fail localhost: 9200 /_cat /health? h= st || exit 1"]
            interval: 50s
            timeout: 30s
            retries: 5
        environment:
            -cluster.name= cp-itr
            -bootstrap.memory_lock= true
            -"ES_JAVA_OPTS= -Xms512m -Xmx512m"
            -discovery.type= single-node
        ports:
            -9300: 9300
            -9200: 9200
volumes:
    pgdata:
  • Answer # 1

    And why POSTGRES_HOST= localhost isn't POSTGRES_HOST= db_postgres the connection takes place on the docker subnet

  • Answer # 2

    And why POSTGRES_HOST= localhost isn't POSTGRES_HOST= db_postgres the connection takes place on the docker subnet