teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

不要部分・DB関係変更

2017/02/07 00:44

投稿

mosapride
mosapride

スコア1480

answer CHANGED
@@ -1,35 +1,33 @@
1
+ image:postgresの環境変数としてユーザー・DBが作成されていない。また、使用しているポートが違う。
2
+ db接続テストとしてnpm-pgでのサンプルに記載記載されている接続方法に変更。
3
+ package.jsonの内容が不明なので多分こんな感じで作ればいいかなと。
4
+
1
5
  ```bash
2
6
  version: '2'
3
7
  services:
4
8
  web:
5
9
  build: .
6
10
  volumes:
7
- - ./web:/usr/src/app <-?
8
- - ./web:/usr/src/app
11
+ - ./web:/usr/src/app
9
- ports:
10
- - "3000:3000" <- ?
11
- - 3000:3000
12
12
  links:
13
- - db <- ?
14
13
  - db
15
- volumes_from: <- busyboxは直接扱わないからいならない web->db-->dataとリンクしているから web内のデータ(postgres以外)を保存したいなら必要だけど、volume先がホストだからいらないと思う
16
- - data <- いらない
17
14
 
18
15
  db:
19
16
  image: postgres
20
17
  ports:
21
- - "1234:1234" <-?
22
- - 1234:1234
18
+ - 5432
19
+ environment:
20
+ - POSTGRES_USER=pguser
21
+ - POSTGRES_PASSWORD=pgpass
22
+ - POSTGRES_DB=hoge
23
23
  volumes_from:
24
- - data <-?
25
24
  - data
26
25
 
27
26
 
28
27
  data:
29
28
  image: busybox
30
29
  volumes:
31
- - .data:/data <-? 隠しディレクトリ?
30
+ - /var/lib/postgresql/data
32
- - .data:/var/lib/postgresql/data postgres # https://hub.docker.com/_/postgres/ たぶんこうな気がする
33
31
  ```
34
32
 
35
33
  ```bash
@@ -42,26 +40,58 @@
42
40
  WORKDIR /usr/src/app
43
41
  RUN ["npm", "install"]
44
42
 
45
-
46
- ENTRYPOINT ["npm", "start"] <- これCMDにしたいんだとおもう
47
- CMD ["npm","start"] <- たぶんこう
43
+ CMD ["npm","start"]
48
44
  ```
49
45
 
50
46
 
51
47
  ```js
52
48
  var pg = require('pg');
49
+
50
+ // create a config to configure both pooling behavior
51
+ // and client options
52
+ // note: all config is optional and the environment variables
53
+ // will be read if the config is not present
54
+ var config = {
55
+ user: 'pguser', //env var: PGUSER
56
+ database: 'hoge', //env var: PGDATABASE
57
+ password: 'pgpass', //env var: PGPASSWORD
58
+ host: 'db', // Server hosting the postgres database
59
+ port: 5432, //env var: PGPORT
60
+ max: 10, // max number of clients in the pool
61
+ idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
62
+ };
63
+
64
+
65
+ //this initializes a connection pool
66
+ //it will keep idle connections open for a 30 seconds
67
+ //and set a limit of maximum 10 idle clients
53
- var async = require('async');
68
+ var pool = new pg.Pool(config);
54
-
69
+
55
- var connectString = "tcp://postgres@localhost:1234/data"; <--localhostじゃない
70
+ // to run a query we can acquire a client from the pool,
56
- var connectString = "tcp://postgres@db:1234/data"; <-- たしかこっち
71
+ // run a query on the client, and then return the client to the pool
57
-
58
-
59
- pg.connect(connectString, function(err, client) {
72
+ pool.connect(function(err, client, done) {
60
- if (err) {
73
+ if(err) {
74
+ return console.error('error fetching client from pool', err);
75
+ }
76
+ client.query('SELECT $1::int AS number', ['1'], function(err, result) {
77
+ //call `done()` to release the client back to the pool
78
+ done();
79
+
80
+ if(err) {
61
- console.log('Connection Error:', err);
81
+ return console.error('error running query', err);
62
- throw err;
63
- } else {
64
- console.log('Connected');
65
82
  }
83
+ console.log(result.rows[0].number);
84
+ //output: 1
85
+ });
66
86
  });
87
+
88
+ pool.on('error', function (err, client) {
89
+ // if an error is encountered by a client while it sits idle in the pool
90
+ // the pool itself will emit an error event with both the error and
91
+ // the client which emitted the original error
92
+ // this is a rare occurrence but can happen if there is a network partition
93
+ // between your application and the database, the database restarts, etc.
94
+ // and so you might want to handle it and at least log it out
95
+ console.error('idle client error', err.message, err.stack)
96
+ })
67
97
  ```