Running a nodejs application with mysql database as microservices using docker
MIT License
Running a nodejs application with mysql database using docker and microservice architecture
Watch this tutorial at https://youtu.be/tIbMSqTEpfY
Create a directory for our tutorial mkdir getting-started-docker-mysql-nodejs
Move to this directory cd getting-started-docker-mysql-nodejs/
Create a directory for our mysql microservice mkdir mysql-microservice
Move to this directory cd mysql-microservice/
Create a Dockerfile with following content (name of file will be Dockerfile
)
## Pull the mysql:5.7 image
FROM mysql:5.7
## The maintainer name and email
MAINTAINER Your Name <[email protected]>
# database = test and password for root = password
ENV MYSQL_DATABASE=test \
MYSQL_ROOT_PASSWORD=password
# when container will be started, we'll have `test` database created with this schema
COPY ./test-dump.sql /docker-entrypoint-initdb.d/
We'll initialize our test database with a sample schema. Download test-dump.sql and put it inside mysql-microservice folder along with Dockerfile
Create a data directory where mysql will store its content mkdir data
.
We will specify this directory while running our mysql container.
On Linux default storage directory is /var/lib/mysql
but in this tutorial we'll use a custom storage directory.
Build the image with Dockerfile docker build -t test-mysql .
Note that we are inside mysql-microservice directory. test-mysql
would be name of our image
You can check your newly built image using docker images
Run the newly created docker image as container
docker run -d \
--publish 6603:3306 \
--volume=/home/varunkumar/getting-started-docker-mysql-nodejs/mysql-microservice/data:/var/lib/mysql \
--name=test-mysql-microservice test-mysql
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
.
We are also using our custom data storage directory by specifying host path volume --volume
.
Replace /home/varunkumar/getting-started-docker-mysql-nodejs/mysql-microservice/data
path to absolute path of data directory which you created on your system.
We are also naming our container as test-mysql-microservice --name
Check logs to see if everything went smooth docker logs test-mysql-microservice
Check your container state docker ps
We have successfully launched a mysql container
To verify that our test-mysql-microservice container is up and running, we'll connect to it. Follow below steps if you have mysql (mysql-client) installed on your system.
Check the ip of your system. On Linux use ifconfig
. Lets say that ip is 192.168.43.147
Connect to test-mysql-microservice container with following params- user-root, host=192.168.43.147, port=6603, database=test and password=password. Remember that we have specified root username and password in Dockerfile. Also our container is initialized with test-dump.sql (a schema with database name test)
mysql -u root -p -h 192.168.43.147 -P 6603 -D test
Use password=password when prompt and hit enter
If connected successfully you can see a sample table students show tables
exit
when done.
Right now we are in mysql-microservice directory. We go to project root directory cd ..
create directory for node microservice mkdir nodejs-microservice
Move to this directory cd nodejs-microservice/
Create a Dockerfile with following content (name of file will be Dockerfile
)
# Use Node v8 as the base image.
FROM node:8
# create and set app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
# Copy app source from current host directory to container working directory
COPY . .
# Run app
CMD ["npm", "start"]
We need a package.json file for our node-microservice app as well as source code. For this tutorial, I've already created one. Download package.json as well as index.js and put it inside nodejs-microservice folder along with Dockerfile.
Build the image with Dockerfile docker build -t test-nodejs .
Note that we are inside nodejs-microservice directory. test-nodejs
would be name of our image
You can check your newly built image using docker images
Run the newly created docker image as container
docker run -d \
--publish 4000:4000 \
-e MYSQL_USER='root' \
-e MYSQL_PASSWORD='password' \
-e MYSQL_DATABASE='test' \
-e MYSQL_HOST='172.17.0.2' \
--link test-mysql-microservice:db \
--name=test-nodejs-microservice test-nodejs
-d
run in detach mode--publish
map the host port 4000 to the container port 4000-e
pass environment variables to nodejs app necessary to make mysql connection (check index.js file)--link test-mysql-microservice:db
link to the container named test-mysql-microservice and refer to it as db--name
naming our container as test-nodejs-microservice172.17.0.2
ip-address as MYSQL_HOST. This is the IpAddress of our test-mysql-microservice container.docker inspect test-mysql-microservice | grep IPAddress
If everything is good so far then congratulations 😄 You have a complete app running with two microservices. To test this you can use CURL command from your host machine
Get homepage of your app curl -X GET localhost:4000
Get list of all students from test database curl -X POST 192.168.43.147:4000/get-students
Here 192.168.43.147 is my host IpAddress ifconfig | grep inet
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
Again fetch all students to see updated results curl -X POST 192.168.43.147:4000/get-students
Modify source code of nodejs app, build image, run container and test again.
You can contact me at [email protected] or create github issues.