为NodeJS + PostgreSQL Web应用程序创建Docker镜像

时间:2016-04-27 00:48:15

标签: node.js postgresql docker docker-compose

我一直在阅读Docker的文档,但我无法创建一个可行的图像。

我有一个使用PostgreSQL作为数据库的NodeJS应用程序:

var connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/db';
var pg = require('pg');
var pgp = require('pg-promise')();
var db = pgp(connectionString);
db.func('some_storedProcedure').then(//...)
//...

我首先根据Node's documentation为它创建了一个Dockerfile:

FROM node:argon

# Create app directory
RUN mkdir -p /app
WORKDIR /app

# Install app dependencies
COPY package.json /app
RUN npm install

# Bundle app source
COPY . /app

EXPOSE 5000

CMD [ "npm", "start" ]

然后我跟着this post关于使用docker-compose将数据库连接到它。 docker-compose.yml文件如下所示:

web:


 build: .
  ports:
    - "5000:5000"
  volumes:
    - .:/app
  links:
    - db
  environment:
    DATABASE_URL: postgres://myuser:mypass@db:5432/db
db:
  image: postgres
  environment:
    POSTGRES_USER: myuser
    POSTGRES_PASSWORD: mypass

这是(部分)在创建图像后使用这些文件运行docker-compose up时返回的内容。

npm info ok 
 ---> 87dbbae35721
Removing intermediate container c73f826a0b3d
Step 6 : COPY . /app
 ---> ec56bfc11d3c
Removing intermediate container 745ddf82d742
Step 7 : EXPOSE 5000
 ---> Running in b2be5aecd9d6
 ---> a7d126a7ea5e
Removing intermediate container b2be5aecd9d6
Step 8 : CMD npm start
 ---> Running in 0379d512c688
 ---> 266517f47311
Removing intermediate container 0379d512c688
Successfully built 266517f47311
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Starting imagename_db_1
Creating imagename_web_1
Attaching to imagename_db_1, imagename_web_1
web_1  | npm info it worked if it ends with ok
web_1  | npm info using npm@2.15.1
web_1  | npm info using node@v4.4.3
web_1  | npm info prestart SharedServer@5.8.0
web_1  | npm info start SharedServer@5.8.0
web_1  | 
web_1  | > SharedServer@5.8.0 start /app
web_1  | > node index.js
web_1  | 
web_1  | Wed, 27 Apr 2016 00:41:19 GMT body-parser deprecated bodyParser: use individual json/urlencoded middlewares at index.js:13:9
web_1  | Wed, 27 Apr 2016 00:41:19 GMT body-parser deprecated undefined extended: provide extended option at node_modules/body-parser/index.js:105:29
web_1  | Node app is running on port 5000
db_1   | LOG:  database system was interrupted; last known up at 2016-04-25 00:17:59 UTC
db_1   | LOG:  database system was not properly shut down; automatic recovery in progress
db_1   | LOG:  invalid record length at 0/17076E8
db_1   | LOG:  redo is not required
db_1   | LOG:  MultiXact member wraparound protections are now enabled
db_1   | LOG:  database system is ready to accept connections
db_1   | LOG:  autovacuum launcher started

当我访问http://localhost:5000时,我看到Web应用程序正在运行,但每当我启动尝试访问数据库的内容时,我都会收到以下正文的HTTP 500错误

code: "28P01"
file: "auth.c"
length: 98
line: "285"
name: "error"
routine: "auth_failed"
severity: "FATAL"

我做错了什么?我不确定我是否理解我正在使用Docker做什么,而我对文档的唯一要求是构建特定环境的简单配方(或者至少,这是我所理解的)

感谢。

1 个答案:

答案 0 :(得分:0)

检查$ PGDATA中的pg_hba.conf是否允许来自node.js的连接。

默认情况下,pg_hba.conf是这样的:

$chunk = ['hello old world'];
event(new App\Events\SomeEventName($chunk));
dd($chunk);
DB::table($table)->insert($chunk);

这适用于通过操作系统所有者的标准psql连接,因为它允许本地连接受信任的localhost IP地址127.0.0.1。但是,如果你在服务器上设置了一个IP地址,我猜你做了,那么你需要允许一个条目,因为在你的node.js配置中你指的是一个“DB”主机。所以ping“DB”并添加该IP地址:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

更改该文件后,您需要执行pg_ctl重新加载。