回答編集履歴
1
不要部分・DB関係変更
test
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
image:postgresの環境変数としてユーザー・DBが作成されていない。また、使用しているポートが違う。
|
2
|
+
|
3
|
+
db接続テストとしてnpm-pgでのサンプルに記載記載されている接続方法に変更。
|
4
|
+
|
5
|
+
package.jsonの内容が不明なので多分こんな感じで作ればいいかなと。
|
6
|
+
|
7
|
+
|
8
|
+
|
1
9
|
```bash
|
2
10
|
|
3
11
|
version: '2'
|
@@ -10,25 +18,11 @@
|
|
10
18
|
|
11
19
|
volumes:
|
12
20
|
|
13
|
-
- ./web:/usr/src/app <-?
|
14
|
-
|
15
|
-
- ./web:/usr/src/app
|
21
|
+
- ./web:/usr/src/app
|
16
|
-
|
17
|
-
ports:
|
18
|
-
|
19
|
-
- "3000:3000" <- ?
|
20
|
-
|
21
|
-
- 3000:3000
|
22
22
|
|
23
23
|
links:
|
24
24
|
|
25
|
-
- db <- ?
|
26
|
-
|
27
25
|
- db
|
28
|
-
|
29
|
-
volumes_from: <- busyboxは直接扱わないからいならない web->db-->dataとリンクしているから web内のデータ(postgres以外)を保存したいなら必要だけど、volume先がホストだからいらないと思う
|
30
|
-
|
31
|
-
- data <- いらない
|
32
26
|
|
33
27
|
|
34
28
|
|
@@ -38,13 +32,17 @@
|
|
38
32
|
|
39
33
|
ports:
|
40
34
|
|
41
|
-
-
|
35
|
+
- 5432
|
42
36
|
|
43
|
-
|
37
|
+
environment:
|
38
|
+
|
39
|
+
- POSTGRES_USER=pguser
|
40
|
+
|
41
|
+
- POSTGRES_PASSWORD=pgpass
|
42
|
+
|
43
|
+
- POSTGRES_DB=hoge
|
44
44
|
|
45
45
|
volumes_from:
|
46
|
-
|
47
|
-
- data <-?
|
48
46
|
|
49
47
|
- data
|
50
48
|
|
@@ -58,9 +56,7 @@
|
|
58
56
|
|
59
57
|
volumes:
|
60
58
|
|
61
|
-
-
|
59
|
+
- /var/lib/postgresql/data
|
62
|
-
|
63
|
-
- .data:/var/lib/postgresql/data postgres # https://hub.docker.com/_/postgres/ たぶんこうな気がする
|
64
60
|
|
65
61
|
```
|
66
62
|
|
@@ -86,11 +82,7 @@
|
|
86
82
|
|
87
83
|
|
88
84
|
|
89
|
-
|
90
|
-
|
91
|
-
ENTRYPOINT ["npm", "start"] <- これCMDにしたいんだとおもう
|
92
|
-
|
93
|
-
CMD ["npm","start"]
|
85
|
+
CMD ["npm","start"]
|
94
86
|
|
95
87
|
```
|
96
88
|
|
@@ -102,32 +94,100 @@
|
|
102
94
|
|
103
95
|
var pg = require('pg');
|
104
96
|
|
105
|
-
|
97
|
+
|
106
98
|
|
99
|
+
// create a config to configure both pooling behavior
|
107
100
|
|
101
|
+
// and client options
|
108
102
|
|
109
|
-
|
103
|
+
// note: all config is optional and the environment variables
|
110
104
|
|
111
|
-
|
105
|
+
// will be read if the config is not present
|
112
106
|
|
107
|
+
var config = {
|
113
108
|
|
109
|
+
user: 'pguser', //env var: PGUSER
|
114
110
|
|
111
|
+
database: 'hoge', //env var: PGDATABASE
|
115
112
|
|
113
|
+
password: 'pgpass', //env var: PGPASSWORD
|
116
114
|
|
117
|
-
|
115
|
+
host: 'db', // Server hosting the postgres database
|
118
116
|
|
119
|
-
|
117
|
+
port: 5432, //env var: PGPORT
|
120
118
|
|
121
|
-
c
|
119
|
+
max: 10, // max number of clients in the pool
|
122
120
|
|
123
|
-
t
|
121
|
+
idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
|
124
122
|
|
125
|
-
|
123
|
+
};
|
126
124
|
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
//this initializes a connection pool
|
130
|
+
|
131
|
+
//it will keep idle connections open for a 30 seconds
|
132
|
+
|
133
|
+
//and set a limit of maximum 10 idle clients
|
134
|
+
|
135
|
+
var pool = new pg.Pool(config);
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
// to run a query we can acquire a client from the pool,
|
140
|
+
|
141
|
+
// run a query on the client, and then return the client to the pool
|
142
|
+
|
143
|
+
pool.connect(function(err, client, done) {
|
144
|
+
|
145
|
+
if(err) {
|
146
|
+
|
147
|
+
return console.error('error fetching client from pool', err);
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
client.query('SELECT $1::int AS number', ['1'], function(err, result) {
|
152
|
+
|
153
|
+
//call `done()` to release the client back to the pool
|
154
|
+
|
155
|
+
done();
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
if(err) {
|
160
|
+
|
127
|
-
|
161
|
+
return console.error('error running query', err);
|
128
162
|
|
129
163
|
}
|
130
164
|
|
165
|
+
console.log(result.rows[0].number);
|
166
|
+
|
167
|
+
//output: 1
|
168
|
+
|
169
|
+
});
|
170
|
+
|
131
171
|
});
|
132
172
|
|
173
|
+
|
174
|
+
|
175
|
+
pool.on('error', function (err, client) {
|
176
|
+
|
177
|
+
// if an error is encountered by a client while it sits idle in the pool
|
178
|
+
|
179
|
+
// the pool itself will emit an error event with both the error and
|
180
|
+
|
181
|
+
// the client which emitted the original error
|
182
|
+
|
183
|
+
// this is a rare occurrence but can happen if there is a network partition
|
184
|
+
|
185
|
+
// between your application and the database, the database restarts, etc.
|
186
|
+
|
187
|
+
// and so you might want to handle it and at least log it out
|
188
|
+
|
189
|
+
console.error('idle client error', err.message, err.stack)
|
190
|
+
|
191
|
+
})
|
192
|
+
|
133
193
|
```
|