Browse Source

Step 5: persistence using a bound volume.

Frederic G. MARAND 6 years ago
parent
commit
bf8ee1f9f8
5 changed files with 63 additions and 4 deletions
  1. 2 0
      .gitignore
  2. 45 2
      README.md
  3. 1 2
      node/src/server.js
  4. 5 0
      postgres/Dockerfile
  5. 10 0
      postgres/seed.sql

+ 2 - 0
.gitignore

@@ -1 +1,3 @@
 node_modules
+npm-debug.log
+postgres/data/

+ 45 - 2
README.md

@@ -65,11 +65,11 @@ docker run -p 8888:8888             \
 mkdir postgres
 cd postgres
 # edit Dockerfile
-docker build -t node-pg .
+docker build -t voting-pg .
 docker run -p 5432:5432             \
   --name voting-pg                  \
   --rm                              \
-  node-pg
+  voting-pg
 ```
 * Enter the container with a PG client:
 
@@ -142,3 +142,46 @@ curl -d"yourVote=sandwiches" http://localhost:8888/vote
 curl -d"yourVote=tacos"      http://localhost:8888/vote
 curl -d"yourVote=invalid"    http://localhost:8888/vote
 ```
+
+Step 5: persistence
+
+* Initialize PG as described on the image page, by adding a SQL file to the 
+  container from the host into the `docker-entrypoint-initdb.d/` directory.  
+
+      COPY seed.sql /docker-entrypoint-initdb.d/
+* Redefine the image-provided volume pointing to the PG data:
+
+      VOLUME /var/lib/postgresql/data
+* Rebuild container
+
+      cd postgres
+      docker container build -t voting-pg .
+* Run it. Beware: we can't have a `.gitkeep` in the `postgres/data` directory, which will
+  prevent PG from starting in that directory. But if we don't have it, the 
+  directory will have to be created, because git won't save it.
+
+```bash
+cd postgres
+if [ ! -d data ]; then mkdir data fi
+docker container run  \ 
+  --name voting-pg    \
+  -p 9000:5432        \
+  -v $PWD/data:/var/lig/postgresql/data \
+  --rm -d             \
+  voting-pg
+
+```
+* Run the app
+```bash
+cd node
+docker container run  \
+  --name voting-node  \
+  -p 8888:8888        \ 
+  -v $PWD/src:/usr/src/app/src \
+  --rm -d             \
+  voting-node
+```
+* When stopping the PG container, the node app will crash and not restart.
+  Just touch the `server.js` file and `nodemon` in the node container will 
+  restart it. This is only good for a development workflow: a production version
+  should handle disconnects.

+ 1 - 2
node/src/server.js

@@ -52,7 +52,6 @@ app.get('/', function (req, res) {
 
 app.post('/vote', urlencodedParser, function (req, res) {
   const vote = req.body.yourVote;
-
   if (vote in votes) {
     //language=PostgreSQL
     const sql = `
@@ -60,12 +59,12 @@ UPDATE votes
 SET number_of_votes = $1
 WHERE option_name = $2
 `;
+    votes[vote]++;
     const query = {
       text: sql,
       values: [votes[vote], vote]
     };
 
-    votes[vote]++;
     client.query(query, (err, res) => {
       if (err) {
         throw err;

+ 5 - 0
postgres/Dockerfile

@@ -1,3 +1,8 @@
 FROM postgres:9.6
 LABEL maintainer = "aj-foster@pluralsight.com"
 EXPOSE 5432
+
+COPY seed.sql /docker-entrypoint-initdb.d/
+
+# Not needed since it's in the base images, but makes it more visible.
+VOLUME /var/lib/postgresql/data

+ 10 - 0
postgres/seed.sql

@@ -0,0 +1,10 @@
+CREATE TABLE votes (
+  id INTEGER PRIMARY KEY,
+  number_of_votes INTEGER,
+  option_name VARCHAR(20)
+);
+
+INSERT INTO votes (id, number_of_votes, option_name) VALUES (1, 0, 'sandwiches');
+INSERT INTO votes (id, number_of_votes, option_name) VALUES (2, 0, 'tacos');
+
+SELECT * FROM votes;