简体   繁体   English

docker-compose up && docker-compose build:PostgreSQL错误

[英]docker-compose up && docker-compose build : error with PostgreSQL

I try to implement my docker-compose file but I have an error with my data base PostgreSQL. 我尝试实现我的docker-compose文件,但是数据库PostgreSQL出现错误。 If I start my project with npm start I have no issue. 如果我使用npm start启动项目,则没有问题。 For start my project with docker i use : docker-compose up && docker-compose build 为了从docker启动我的项目,我使用: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 . 我不明白为什么我无法使用Docker将服务器连接到数据库PostgreSQL。

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 : 我已经成功完成了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. 谢谢,问候。

When using postgres with docker, I would recommend not setting the URL as you have above. 与docker一起使用postgres时,建议不要像上面一样设置URL。 The URL you set above is already how you would access it via the internal docker network. 您在上面设置的URL已经是您可以通过内部docker网络访问它的方式。 If you remove that, then the URL you would need to access the postgres instance is postgres://USER:PWD@postgres/your_db . 如果删除它,则访问postgres实例所需的URL是postgres://USER:PWD@postgres/your_db

Also, you do have a typo in your URL. 另外,您的网址中确实有错字。

I don't understand why I can't connect my server to my DataBase PostgreSQL with Docker. 我不明白为什么我无法使用Docker将服务器连接到我的数据库PostgreSQL。

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). 因为在大多数情况下, server容器将启动并尝试连接到db容器,但是db需要花费一些时间来启动(通常需要10到15秒)。

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. 这就是为什么您could not connect to postgres { Error: connect ECONNREFUSED 127.0.0.1:5432异常,因为db尚未启动并准备接受连接。

What you can do to is restart your server container after it fails to start. 您可以做的是在server容器启动失败后重新启动它。

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
...

Your project client is trying to access postgress in 127.0.0.1:5432, that address no longer applies under docker architecture. 您的项目客户端正在尝试访问127.0.0.1:5432中的postgress,该地址在Docker体系结构下不再适用。

According to docker documentation : 根据docker文档

By default Compose sets up a single network for your app. 默认情况下,Compose为您的应用设置单个网络。 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. Docker为您管理虚拟网络中的所有实例,并通过使用您在docker-compose文件中分配给容器的名称来使它们可用。

So, from your client you have to access postgress with below string: 因此,从您的客户端,您必须使用以下字符串访问postgress:

 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. 请注意,对db任何请求都将被docker捕获,并重定向到docker管理的虚拟网络中容器的host:port

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM