Search code examples
vaporvapor-fluent

Vapor + PostgreSQL + Nginx build on Docker not operation properly


I use docker to compose Vapor, PostgreSQL and Nginx for a project, my docker-compose.yml like this:

version: "3.6"

services:
    vapor:
        build:
            context: ./vapor
        image: ${CURRENT_VAPOR_IMG}
        ports:
            - 8080:8080
        volumes:
            - ${HOST_ROOT}:${CONTAINER_ROOT}
        working_dir: ${CONTAINER_ROOT}
        tty: true
        entrypoint: bash
        networks:
            - x-net

    nginx:
        build:
            context: ./nginx
        image: ${CURRENT_NGINX_IMG}
        ports:
            - ${HOST_HTTP_PORT}:80
        volumes:
            - ${HOST_ROOT}:${CONTAINER_ROOT}
        networks:
            - x-net

    psql:
        image: ${CURRENT_DB_IMG}
        ports:
            - 5432:5432
        environment:
            - POSTGRES_DB=xxx
            - POSTGRES_USER=xxx
            - POSTGRES_PASSWORD=pass
        volumes:
            - ~/x/x-db:/var/lib/postgresql/data
    networks:
        - x-net

networks:
    x-net:
         driver: bridge

After I start all the container by running docker-compose up, then enter to vapor's container to build && run the project, it will prompt an error to the console:

NIO.ChannelError.connectFailed(NIO.NIOConnectionError(host: "localhost", port: 5432, dnsAError: nil, dnsAAAAError: nil, connectionErrors: [NIO.SingleConnectionFailure(target: [IPv6]localhost/::1:5432, error: connection reset (error set): Connection refused (errno: 61)), NIO.SingleConnectionFailure(target: [IPv4]localhost/127.0.0.1:5432, error: connection reset (error set): Connection refused (errno: 61))]))

Then I run the vapor project on local machine and keep the psql container running, it works normally, such as finished the first migration with models.

Is there any mistakes on my configuration of docker or any others?


Solution

  • To connect to database inside container dont use localhost as a db host but your db container name. So in your case host is psql. Here your docker compose is not well formatted psql and nginx must have one tab more. But maybe its just SO formatting wrong.