You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+98-18Lines changed: 98 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,11 +15,11 @@ using microservice architecture
15
15
16
16
### Launching mysql in a container
17
17
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`)
23
23
```
24
24
## Pull the mysql:5.7 image
25
25
FROM mysql:5.7
@@ -35,55 +35,135 @@ using microservice architecture
35
35
COPY ./test-dump.sql /docker-entrypoint-initdb.d/
36
36
37
37
```
38
-
6. we'll initialize our test database with a sample schema.
38
+
6. We'll initialize our test database with a sample schema.
39
39
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
40
40
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`.
42
42
We will specify this directory wile running our mysql container.
43
43
On Linux default storage directory is `/var/lib/mysql` but in this tutorial we'll use a custom storage directory.
44
44
45
-
8. build the image with Dockerfile `docker build -t test-mysql .`
45
+
8. Build the image with Dockerfile `docker build -t test-mysql .`
46
46
Note that we are inside mysql-microservice directory. `test-mysql` would be name of our image
47
47
48
-
9. you can check your newly built image using `docker images`
48
+
9. You can check your newly built image using `docker images`
49
49

50
50
51
-
10. run the newly created docker image as container
51
+
10. Run the newly created docker image as container
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`.
60
60
We are also using our custom data storage directory by specifying host path volume `--volume`.
61
61
Replace `/home/varunkumar/getting-started-docker-mysql-nodejs/mysql-microservice/data` path to absolute path of data directory which you created on your system.
62
62
We are also naming our container as test-mysql-microservice `--name`
63
63
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`
65
65
66
-
13. check your container state `docker ps`
66
+
13. Check your container state `docker ps`
67
67

68
68
69
-
14. we have successfully launched a mysql container
69
+
14. We have successfully launched a mysql container
70
70
71
71
72
72
### connecting to newly launched mysql container from host (optional)
73
73
74
74
To verify that our test-mysql-microservice container is up and running, we'll connect to it.
75
75
Follow below steps if you have mysql (mysql-client) installed on your system.
76
76
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-
79
79
user-root, host=192.168.43.147, port=6603, database=test and password=password.
80
80
Remember that we have specified root username and password in Dockerfile.
81
81
Also our container is initialized with test-dump.sql (a schema with database name test)
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`
87
87
`exit` when done.
88
88

89
89
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
+

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
+

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.
0 commit comments