Browse Source

Step 3: working PostgreSQL.

Frederic G. MARAND 6 years ago
parent
commit
1d504928ce
2 changed files with 92 additions and 24 deletions
  1. 89 24
      README.md
  2. 3 0
      postgres/Dockerfile

+ 89 - 24
README.md

@@ -1,5 +1,7 @@
 From https://www.codeschool.com/screencasts/build-a-node-app-with-postgres-and-docker
 
+# Step 1: Nginx
+
 * Run nginx 
     * in foreground
         * `docker run -p 81:80 --name web nginx`
@@ -7,42 +9,105 @@ From https://www.codeschool.com/screencasts/build-a-node-app-with-postgres-and-d
         * `docker run -p 81:80 --name web --rm nginx`
     * in background
         * `docker run -p 81:80 --name web -d nginx`
-    * foreground, mounting the nginx/html directory:
+    * foreground, mounting the `nginx/html` directory:
+```bash
+docker run --rm \
+  -p 81:80      \
+  --name web    \
+  --mount type=bind,source="$PWD"/nginx/html,target=/usr/share/nginx/html \
+  nginx
 
-            docker run --rm \
-            -p 81:80  \
-            --name web \
-            --mount type=bind,source=$PWD/nginx/html,target=/usr/share/nginx/html \
-            nginx
-          
-* Enter the container:
+# or...
 
-        docker exec -it web /bin/bash
-      
+docker run --rm                              \
+  -p 81:80                                   \
+  --name web                                 \
+  -v "$PWD"/nginx/html,/usr/share/nginx/html \
+  nginx
+```
+          
+* Enter the container: `docker exec -it web /bin/bash`
 * Build a local image from the `Dockerfile`
-
-        docker build -t osinet/nginx:latest .
-      
+```bash
+docker build -t osinet/nginx:latest .
+```
 * Run the newly built image:
-
-        docker run --rm \
-        -p 81:80 \
-        --name web \
-        --mount type=bind,source=$PWD/nginx/html,target=/usr/share/nginx/html \ 
-        osinet/nginx
-        
+```bash
+docker run --rm \
+  -p 81:80 \
+  --name web \
+  --mount type=bind,source=$PWD/nginx/html,target=/usr/share/nginx/html \ 
+  osinet/nginx
+``` 
 * List images
     * All except intermediates: `docker images`
     * All including intermediates: `docker images -a`
     * Images with this name: `docker images osinet/nginx`
 
-# Step 2: node
+# Step 2: Node.js app
 
-````bash
-cd nginx/node
+```bash
+mkdir node
+cd node
+# edit Dockerfile
 docker build -t node-app .
 docker run -p 8888:8888             \
   -v $PWD/node/src:/usr/src/app/src \ 
   --name voting-node                \
+  --rm                              \
   node-app 
-````
+```
+
+# Step 3: PostgreSQL
+
+* Build and run the container
+```bash
+mkdir postgres
+cd postgres
+# edit Dockerfile
+docker build -t node-pg .
+docker run -p 5432:5432             \
+  --name voting-pg                  \
+  --rm                              \
+  node-pg
+```
+* Enter the container with a PG client:
+
+```bash
+# Without the -U, PG will try to use undeclared used root.  
+docker container exec -it psql -U postgres
+```
+* In `psql` in the container, list databases, create table, list tables:
+```postgresql
+\l
+
+CREATE TABLE votes (
+  id INTEGER PRIMARY KEY,
+  number_of_votes INTEGER,
+  option_name VARCHAR(20)
+);
+
+\dt
+```
+  * Now we have a table:
+```
+         List of relations
+ Schema | Name  | Type  |  Owner
+--------+-------+-------+----------
+ public | votes | table | postgres
+(1 row)
+```
+  * Insert initial data
+```postgresql
+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;
+```
+  * Results:
+```
+ id | number_of_votes | option_name
+----+-----------------+-------------
+  1 |               0 | sandwiches
+  2 |               0 | tacos
+(2 rows)
+```

+ 3 - 0
postgres/Dockerfile

@@ -0,0 +1,3 @@
+FROM postgres:9.6
+LABEL maintainer = "aj-foster@pluralsight.com"
+EXPOSE 5432