@@ -57,3 +57,88 @@ docker run -p 80:80 -d amirhossein21/nginx:v0.1.0
57
57
```
58
58
59
59
Now make a curl to ``` localhost:8080 ``` .
60
+
61
+ ## Node App
62
+
63
+ In the next example we are going to deploy a full ** Node.js** application with ** Redis** database on ** Docker** .
64
+
65
+ First we need to build our network and volume:
66
+
67
+ #### Network
68
+
69
+ ``` shell
70
+ docker network create node-app
71
+ ```
72
+
73
+ #### Volume
74
+
75
+ ``` shell
76
+ docker volume create redisvolume
77
+ ```
78
+
79
+ Now let's start ** Redis** cluster in ** node-app** network, binded to ``` redisvolume ``` .
80
+
81
+ ``` shell
82
+ docker run --network=node-app -v redisvolume:/data --name node-app-redis-cluster -d redis:latest
83
+ ```
84
+
85
+ Now that we have Redis cluster up and running, we are
86
+ going to deploy our node app.
87
+
88
+ First we are going to write a Dockerfile.
89
+
90
+ ``` Dockerfile
91
+ FROM node:16
92
+
93
+ # Create app directory
94
+ WORKDIR /usr/src/app
95
+
96
+ # set environment variables
97
+ ENV HTTP_PORT=8080
98
+ ENV REDIS_URL=redis://node-app-redis-cluster:6379
99
+
100
+ # Install app dependencies
101
+ # A wildcard is used to ensure both package.json AND package-lock.json are copied
102
+ # where available (npm@5+)
103
+ COPY ./app/package*.json ./
104
+
105
+ # Run npm i to create node_modules directory.
106
+ RUN npm install
107
+
108
+ # If you are building your code for production
109
+ # RUN npm ci --only=production
110
+
111
+ # Bundle app source
112
+ COPY ./app .
113
+
114
+ # cmd allows us to execute the node app starting command
115
+ CMD [ "node" , "index.js" ]
116
+ ```
117
+
118
+ Pay attention to the redis cluster address:
119
+
120
+ ``` Dockerfile
121
+ ENV REDIS_URL=redis://node-app-redis-cluster:6379
122
+ ```
123
+
124
+ Since our node app container and redis cluster are in
125
+ one network, we can use the redis container name insted of host ip.
126
+
127
+ #### Build
128
+
129
+ ``` shell
130
+ docker build . -f build/app/Dockerfile -t amirhossein21/node-app:v0.1.0
131
+ ```
132
+
133
+ #### Run
134
+
135
+ ``` shell
136
+ docker run --network=node-app --name node-app-container -d -p 8080:8080 amirhossein21/node-app:v0.1.0
137
+ ```
138
+
139
+ Now make the following http requests:
140
+
141
+ ``` shell
142
+ curl -X POST " localhost:8080/api?key=amir&value=hossein"
143
+ curl -X GET " localhost:8080/api?key=amir"
144
+ ```
0 commit comments