Running a nodejs application with mysql database as microservices using docker using microservice architecture
- Launch mysql server in a docker container.
- Launch our simple node app in a separate container.
- Link these two containers and test our integrated mysql-nodejs app.
- must have docker set up and running on your system
-
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 <name@email.com> # 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 wile running our mysql container. On Linux default storage directory is/var/lib/mysqlbut 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-mysqlwould 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
-dand 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/datapath 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 testuse password=password when prompt and hit enter -
if connected successfully you can see a sample table students
show tablesexitwhen done.