Skip to content

Commit 724c814

Browse files
committed
added full tutorial
1 parent 470cbf7 commit 724c814

File tree

10 files changed

+724
-18
lines changed

10 files changed

+724
-18
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ mysql-microservice/data/*
33

44
# exception to the rule
55
!mysql-microservice/data/.gitkeep
6+
7+
nodejs-microservice/node_modules

README.md

Lines changed: 98 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ using microservice architecture
1515

1616
### Launching mysql in a container
1717

18-
1. create a directory for our tutorial `mkdir getting-started-docker-mysql-nodejs`
19-
2. move to this directory `cd getting-started-docker-mysql-nodejs/`
20-
3. create a directory for our mysql microservice `mkdir mysql-microservice`
21-
4. move to this directory `cd mysql-microservice/`
22-
5. create a Dockerfile with following content (name of file will be `Dockerfile`)
18+
1. Create a directory for our tutorial `mkdir getting-started-docker-mysql-nodejs`
19+
2. Move to this directory `cd getting-started-docker-mysql-nodejs/`
20+
3. Create a directory for our mysql microservice `mkdir mysql-microservice`
21+
4. Move to this directory `cd mysql-microservice/`
22+
5. Create a Dockerfile with following content (name of file will be `Dockerfile`)
2323
```
2424
## Pull the mysql:5.7 image
2525
FROM mysql:5.7
@@ -35,55 +35,135 @@ using microservice architecture
3535
COPY ./test-dump.sql /docker-entrypoint-initdb.d/
3636
3737
```
38-
6. we'll initialize our test database with a sample schema.
38+
6. We'll initialize our test database with a sample schema.
3939
Download [test-dump.sql](https://github.com/varunon9/getting-started-docker-mysql-nodejs/blob/master/mysql-microservice/test-dump.sql) and put it inside mysql-microservice folder along with Dockerfile
4040
41-
7. create a data directory where mysql will store its content `mkdir data`.
41+
7. Create a data directory where mysql will store its content `mkdir data`.
4242
We will specify this directory wile running our mysql container.
4343
On Linux default storage directory is `/var/lib/mysql` but in this tutorial we'll use a custom storage directory.
4444
45-
8. build the image with Dockerfile `docker build -t test-mysql .`
45+
8. Build the image with Dockerfile `docker build -t test-mysql .`
4646
Note that we are inside mysql-microservice directory. `test-mysql` would be name of our image
4747
48-
9. you can check your newly built image using `docker images`
48+
9. You can check your newly built image using `docker images`
4949
![Building the image using Dockerfile](./screenshots/building-test-mysql-image.png)
5050
51-
10. run the newly created docker image as container
51+
10. Run the newly created docker image as container
5252
```
5353
docker run -d \
5454
--publish 6603:3306 \
5555
--volume=/home/varunkumar/getting-started-docker-mysql-nodejs/mysql-microservice/data:/var/lib/mysql \
5656
--name=test-mysql-microservice test-mysql
5757
```
5858
59-
11. with above command we started our container in detach mode `-d` and mapped host(your machine) port 6603 with container port 3306 (mysql server) `--publish 6603:3306`.
59+
11. With above command we started our container in detach mode `-d` and mapped host(your machine) port 6603 with container port 3306 (mysql server) `--publish 6603:3306`.
6060
We are also using our custom data storage directory by specifying host path volume `--volume`.
6161
Replace `/home/varunkumar/getting-started-docker-mysql-nodejs/mysql-microservice/data` path to absolute path of data directory which you created on your system.
6262
We are also naming our container as test-mysql-microservice `--name`
6363
64-
12. check logs to see if everything went smooth `docker logs test-mysql-microservice`
64+
12. Check logs to see if everything went smooth `docker logs test-mysql-microservice`
6565
66-
13. check your container state `docker ps`
66+
13. Check your container state `docker ps`
6767
![Running the docker image](./screenshots/running-test-mysql-microservice-container.png)
6868
69-
14. we have successfully launched a mysql container
69+
14. We have successfully launched a mysql container
7070
7171
7272
### connecting to newly launched mysql container from host (optional)
7373
7474
To verify that our test-mysql-microservice container is up and running, we'll connect to it.
7575
Follow below steps if you have mysql (mysql-client) installed on your system.
7676
77-
1. check the ip of your system. On Linux use `ifconfig`. Lets say that ip is 192.168.43.147
78-
2. connect to test-mysql-microservice container with following params-
77+
1. Check the ip of your system. On Linux use `ifconfig`. Lets say that ip is 192.168.43.147
78+
2. Connect to test-mysql-microservice container with following params-
7979
user-root, host=192.168.43.147, port=6603, database=test and password=password.
8080
Remember that we have specified root username and password in Dockerfile.
8181
Also our container is initialized with test-dump.sql (a schema with database name test)
8282
8383
3. `mysql -u root -p -h 192.168.43.147 -P 6603 -D test`
84-
use password=password when prompt and hit enter
84+
Use password=password when prompt and hit enter
8585
86-
4. if connected successfully you can see a sample table students `show tables`
86+
4. If connected successfully you can see a sample table students `show tables`
8787
`exit` when done.
8888
![Connecting to mysql container from host](./screenshots/connecting-to-test-mysql-microservice.png)
8989
90+
### Launching nodejs app in a container
91+
92+
1. Right now we are in mysql-microservice directory. We go to project root directory `cd ..`
93+
2. create directory for node microservice `mkdir nodejs-microservice`
94+
3. Move to this directory `cd nodejs-microservice/`
95+
4. Create a Dockerfile with following content (name of file will be `Dockerfile`)
96+
```
97+
# Use Node v8 as the base image.
98+
FROM node:8
99+
100+
# create and set app directory
101+
RUN mkdir -p /usr/src/app
102+
WORKDIR /usr/src/app
103+
104+
# Install app dependencies
105+
# A wildcard is used to ensure both package.json AND package-lock.json are copied
106+
# where available (npm@5+)
107+
COPY package*.json ./
108+
RUN npm install
109+
110+
# Copy app source from current host directory to container working directory
111+
COPY . .
112+
113+
# Run app
114+
CMD ["npm", "start"]
115+
116+
```
117+
5. We need a package.json file for our node-microservice app as well as source code.
118+
For this tutorial, I've already created one.
119+
Download [package.json](https://github.com/varunon9/getting-started-docker-mysql-nodejs/blob/master/nodejs-microservice/package.json) as well as [index.js](https://github.com/varunon9/getting-started-docker-mysql-nodejs/blob/master/nodejs-microservice/index.js) and put it inside nodejs-microservice folder along with Dockerfile.
120+
121+
6. Build the image with Dockerfile `docker build -t test-nodejs .`
122+
Note that we are inside nodejs-microservice directory. `test-nodejs` would be name of our image
123+
124+
7. You can check your newly built image using `docker images`
125+
![Building the image using Dockerfile](./screenshots/building-test-nodejs-image.png)
126+
127+
8. Run the newly created docker image as container
128+
```
129+
docker run -d \
130+
--publish 4000:4000 \
131+
-e MYSQL_USER='root' \
132+
-e MYSQL_PASSWORD='password' \
133+
-e MYSQL_DATABASE='test' \
134+
-e MYSQL_HOST='172.17.0.2' \
135+
--link test-mysql-microservice:db \
136+
--name=test-nodejs-microservice test-nodejs
137+
```
138+
![Running the image using Dockerfile](./screenshots/running-test-nodejs-image.png)
139+
140+
9. Explaination of above command-
141+
`-d` run in detach mode
142+
`--publish` map the host port 4000 to the container port 4000
143+
`-e` pass environment variables to nodejs app necessary to make mysql connection (check index.js file)
144+
`--link test-mysql-microservice:db` link to the container named test-mysql-microservice and refer to it as db
145+
`--name` naming our container as test-nodejs-microservice
146+
147+
10. How to know your MYSQL_HOST-
148+
Note that I am using `172.17.0.2` ip-address as MYSQL_HOST. This is the IpAddress of our test-mysql-microservice container.
149+
You must replace this value to your container's ipAddress. Use `docker inspect test-mysql-microservice | grep IPAddress`
150+
151+
152+
### testing our complete app
153+
154+
If everything is good so far then congratulations :smile: You have a complete app running with two microservices. To test this you can use CURL command from your host machine
155+
156+
1. Get homepage of your app `curl -X GET localhost:4000`
157+
158+
2. Get list of all students from test database `curl -X POST 192.168.43.147:4000/get-students`
159+
Here 192.168.43.147 is my host IpAddress `ifconfig | grep inet`
160+
161+
3. Add a new student to your test db `curl --header "Content-Type: application/json" -d '{"rollNo": 1130360, "name": "Abhishek Goswami"}' -X POST localhost:4000/add-student`
162+
163+
4. Again fetch all students to see updated results `curl -X POST 192.168.43.147:4000/get-students`
164+
165+
5. Modify sorce code of nodejs app, build image, run container and test again.
166+
167+
### Queries/Comments
168+
169+
You can contact me at varunon9@gmail.com or create github issues.

nodejs-microservice/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

nodejs-microservice/Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Use Node v8 as the base image.
2+
FROM node:8
3+
4+
# create and set app directory
5+
RUN mkdir -p /usr/src/app
6+
WORKDIR /usr/src/app
7+
8+
# Install app dependencies
9+
# A wildcard is used to ensure both package.json AND package-lock.json are copied
10+
# where available (npm@5+)
11+
COPY package*.json ./
12+
RUN npm install
13+
14+
# Bundle app source
15+
COPY . .
16+
17+
# Run app
18+
CMD ["npm", "start"]

nodejs-microservice/index.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @author Varun Kumar <varunon9@gmail.com>
3+
*/
4+
5+
const app = require('express')();
6+
const mysql = require('mysql');
7+
8+
const bodyParser = require('body-parser');
9+
10+
app.use(bodyParser.json({
11+
limit: '8mb'
12+
})); // support json encoded bodies
13+
14+
// environment variables
15+
const PORT = process.env.PORT || 4000;
16+
const HOST = process.env.HOST || '0.0.0.0';
17+
18+
// mysql credentials
19+
const connection = mysql.createConnection({
20+
host: process.env.MYSQL_HOST || '172.17.0.2',
21+
user: process.env.MYSQL_USER || 'root',
22+
password: process.env.MYSQL_PASSWORD || 'password',
23+
database: process.env.MYSQL_DATABASE || 'test'
24+
});
25+
26+
connection.connect((err) => {
27+
if (err) {
28+
console.error('error connecting mysql: ', err);
29+
} else {
30+
console.log('mysql connection successful');
31+
app.listen(PORT, HOST, (err) => {
32+
if (err) {
33+
console.error('Error starting server', err);
34+
} else {
35+
console.log('server listening at port ' + PORT);
36+
}
37+
});
38+
}
39+
});
40+
41+
// home page
42+
app.get('/', (req, res) => {
43+
res.json({
44+
success: true,
45+
message: 'Hello world'
46+
});
47+
});
48+
49+
// insert a student into database
50+
app.post('/add-student', (req, res) => {
51+
const student = req.body;
52+
const query = 'INSERT INTO students values(?, ?)';
53+
54+
connection.query(query, [student.rollNo, student.name], (err, results, fields) => {
55+
if (err) {
56+
console.error(err);
57+
res.json({
58+
success: false,
59+
message: 'Error occured'
60+
});
61+
} else {
62+
res.json({
63+
success: true,
64+
message: 'Successfully added student'
65+
});
66+
}
67+
});
68+
});
69+
70+
// fetch all students
71+
app.post('/get-students', (req, res) => {
72+
const query = 'SELECT * FROM students';
73+
connection.query(query, (err, results, fields) => {
74+
if (err) {
75+
console.error(err);
76+
res.json({
77+
success: false,
78+
message: 'Error occured'
79+
});
80+
} else {
81+
res.json({
82+
success: true,
83+
result: results
84+
});
85+
}
86+
});
87+
});
88+

0 commit comments

Comments
 (0)