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

查看:88
本文介绍了docker-compose up&&docker-compose build:PostgreSQL错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实现docker-compose文件,但是数据库PostgreSQL出现错误.如果我使用npm start启动项目,则没有问题.要使用docker启动我的项目,请使用:docker-compose up&&docker-compose build

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

我的输出:

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 }

我不明白为什么我无法使用Docker将服务器连接到我的数据库PostgreSQL.

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

谢谢您的帮助,但是对于您的解决方案,我始终有同样的问题和相同的输出.我已经成功完成了docker-compose

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

谢谢你,问候.

推荐答案

您的项目客户端正在尝试访问127.0.0.1:5432中的postgress,该地址不再适用于docker体系结构.

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

根据 docker文档:

默认情况下,Compose为您的应用设置一个网络.每个服务的容器加入了默认网络,并且两者都是可以被该网络上的其他容器访问,并且可以被发现它们的主机名与容器名称相同.

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为您管理虚拟网络中的所有实例,并通过使用您在docker-compose文件中分配给容器的名称来使它们可用.

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.

因此,您必须从客户端使用以下字符串访问postgress:

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

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

注意,对 db 的任何请求将被docker捕获,并重定向到docker管理的虚拟网络中容器的 host:port .

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.

这篇关于docker-compose up&amp;&amp;docker-compose build:PostgreSQL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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