Skip to content

Commit 587abb9

Browse files
authored
Fixes docker-compose & pg initialization (#10)
* Fixes docker-compose & pg initialization - ./scripts/init_db.sh is not executable (/bin/bash: bad interpreter: Permission denied) - same script fails because user is already created, only table creation is necessary. * Actually fix docker-compose development workflow * Incorporate PR feedback * Remove credentials.json
1 parent 899a037 commit 587abb9

File tree

8 files changed

+30
-31
lines changed

8 files changed

+30
-31
lines changed

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
FROM node:14.15.3
22

3-
RUN mkdir -p /opt/notes-app
43
WORKDIR /opt/notes-app
54

65
COPY package.json package-lock.json ./
76

87
RUN npm install
98

10-
CMD ["npm", "run", "start"]
9+
COPY . .
10+
11+
ENTRYPOINT [ "npm", "run" ]
12+
CMD [ "start" ]

credentials.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
host: process.env.DB_HOST || 'localhost',
3+
database: 'notesapi',
4+
user: 'notesadmin',
5+
password: 'password',
6+
port: '5432',
7+
};

credentials.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

docker-compose.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ services:
1919
- postgres
2020
ports:
2121
- '4000:4000'
22-
network_mode: host
22+
environment:
23+
DB_HOST: postgres
2324
volumes:
24-
- ./notes:/opt/notes-app/notes
25-
- ./public:/opt/notes-app/public
26-
- ./scripts:/opt/notes-app/scripts
27-
- ./server:/opt/notes-app/server
28-
- ./src:/opt/notes-app/src
29-
- ./credentials.json:/opt/notes-app/credentials.json
25+
- ./notes:/opt/notes-app/notes
26+
- ./public:/opt/notes-app/public
27+
- ./scripts:/opt/notes-app/scripts
28+
- ./server:/opt/notes-app/server
29+
- ./src:/opt/notes-app/src
30+
- ./credentials.js:/opt/notes-app/credentials.js
3031

3132
volumes:
3233
db:

scripts/init_db.sh

100644100755
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22
set -e
33

44
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
5-
CREATE ROLE notesadmin WITH LOGIN PASSWORD 'password';
6-
ALTER ROLE notesadmin WITH SUPERUSER;
7-
ALTER DATABASE notesapi OWNER TO notesadmin;
8-
9-
DROP TABLE IF EXISTS notes;
10-
CREATE TABLE notes (
11-
id SERIAL PRIMARY KEY,
12-
created_at TIMESTAMP NOT NULL,
13-
updated_at TIMESTAMP NOT NULL,
14-
title TEXT,
15-
body TEXT
16-
);
5+
DROP TABLE IF EXISTS notes;
6+
CREATE TABLE notes (
7+
id SERIAL PRIMARY KEY,
8+
created_at TIMESTAMP NOT NULL,
9+
updated_at TIMESTAMP NOT NULL,
10+
title TEXT,
11+
body TEXT
12+
);
1713
EOSQL

scripts/seed.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const path = require('path');
1313
const {Pool} = require('pg');
1414
const {readdir, unlink, writeFile} = require('fs/promises');
1515
const startOfYear = require('date-fns/startOfYear');
16-
const credentials = require('../credentials.json');
16+
const credentials = require('../credentials');
1717

1818
const NOTES_PATH = './notes';
1919
const pool = new Pool(credentials);

server/api.server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const React = require('react');
2929
const ReactApp = require('../src/App.server').default;
3030

3131
// Don't keep credentials in the source tree in a real app!
32-
const pool = new Pool(require('../credentials.json'));
32+
const pool = new Pool(require('../credentials'));
3333

3434
const PORT = 4000;
3535
const app = express();

src/db.server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {Pool} from 'react-pg';
10-
import credentials from '../credentials.json';
10+
import credentials from '../credentials';
1111

1212
// Don't keep credentials in the source tree in a real app!
1313
export const db = new Pool(credentials);

0 commit comments

Comments
 (0)