0

I try to implement my docker-compose file but I have an error with my data base PostgreSQL. If I start my project with npm start I have no issue. For start my project with docker i use : docker-compose up && docker-compose build

docker-compose.yml
version: "3"
services:
  server:
    build: .
    container_name: dev-area-2018
    ports:
      - 8080:8080
    depends_on:
      - db
db:
  image: postgres:latest
  ports:
    - 5432:5432
  environment:
    DATABASE_URL: postgres://postgers@db:5432/API
# volumes:
#   - /var/lib/postgres/data
client:
   build: ./client_web/
   container_name: client_web
   ports:
     - 8081:8081

My output :

Starting client_web         ... done
Starting dev_area_2018_db_1 ... done
Starting dev-area-2018      ... done
Attaching to dev_area_2018_db_1, client_web, dev-area-2018
db_1      | 2019-02-28 17:51:40.679 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1      | 2019-02-28 17:51:40.679 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1      | 2019-02-28 17:51:40.698 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1      | 2019-02-28 17:51:40.741 UTC [23] LOG:  database system was shut down at 2019-02-28 17:46:23 UTC
db_1      | 2019-02-28 17:51:40.759 UTC [1] LOG:  database system is ready to accept connections

client_web |
client_web | > dev-area-2018-client@0.0.0 start /usr/src/app
client_web | > node ./app.js
client_web |
client_web | Your client is currently running on port 8081
dev-area-2018 |
dev-area-2018 | > dev-area-2018@0.0.0 start /usr/src/app
dev-area-2018 | > node ./bin/www
dev-area-2018 |
dev-area-2018 | Server running at http://localhost:8080/
dev-area-2018 | could not connect to postgres { Error: connect ECONNREFUSED 127.0.0.1:5432
dev-area-2018 |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1083:14)
dev-area-2018 |   errno: 'ECONNREFUSED',
dev-area-2018 |   code: 'ECONNREFUSED',
dev-area-2018 |   syscall: 'connect',
dev-area-2018 |   address: '127.0.0.1',
dev-area-2018 |   port: 5432 }

I don't understand why i can't connecte my server to my DataBase PostgreSQL with Docker .

EDIT : Thank you for you help, but with your solutions I have always the same issue and the same output. I have succeeded with this docker-compose :

version: "3"
services:
  server:
    build: .
    container_name: dev-area-2018
    restart: always
    ports:
      - 8080:8080
    links:
      - db
      - db:database
    depends_on:
      - db
    environment:
      DATABASE_URL: postgres://postgres@db
  client:
    build: ./client_web/
    container_name: client_web
    ports:
       - 8081:8081
  db:
    image: postgres:latest
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_DB: API

Thank you, Regards.

  • It appears to be connecting to the localhost. Did you specify a host string for your dev-area-2018 container? – tyhunt99 Feb 28 '19 at 19:05

3 Answers3

0

When using postgres with docker, I would recommend not setting the URL as you have above. The URL you set above is already how you would access it via the internal docker network. If you remove that, then the URL you would need to access the postgres instance is postgres://USER:PWD@postgres/your_db.

Also, you do have a typo in your URL.

Busch
  • 857
  • 10
  • 29
0

I don't understand why I can't connect my server to my DataBase PostgreSQL with Docker.

Because in most the of the cases the server container will be up and try to connect to the db container, however, the db takes time to starts up (normally 10-15 secs).

That's why you are getting could not connect to postgres { Error: connect ECONNREFUSED 127.0.0.1:5432 exception, Because db is not up and ready to accept the connections.

What you can do to is restart your server container after it fails to start.

version: "3"
services:
  server:
    build: .
    container_name: dev-area-2018
    restart: always # will restart the container if fails to start
    ports:
      - 8080:8080
    depends_on:
      - db
...
Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
0

Your project client is trying to access postgress in 127.0.0.1:5432, that address no longer applies under docker architecture.

According to docker documentation:

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

Docker manages all instances in virtual network for you and makes them available by using the name you assigned to the container in the docker-compose file.

So, from your client you have to access postgress with below string:

 postgres://<user>:<password>@db/api.

Notice any request to db will be catch by docker and redirected to the host:port of the container in the virtual network managed by docker.

Cristian Colorado
  • 2,022
  • 3
  • 19
  • 24