In this tutorial I am going to explain you how to use docker compose to dockerize your Nodejs MySQL REST API for CRUD operations. I am not going to tell you here how to build the REST CRUD application but you can always go back and check the detail tutorial on this here. I am only going to show you how to dockerize your app using docker compose in Linux environment.
It is a best practice for a container to have only one process with single responsibility. But as I am going to dockerize Nodejs app and MySQL server for storing data for the app, so I need two containers – one for running the Nodejs app and another one for running the MySQL database server.
So these two containers are running independently and to establish communication between these two containers you need docker compose. I am also going to show you how to install docker compose under Linux environment on CentOS operating system.
Using docker compose is basically a three-step process:
- Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
- Define services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
- Run docker-compose up and compose starts and runs your entire app.
Docker Compose Installation
Use the following command to install Docker Compose in your system. I am installing it under Linux environment CentOS operating system.
I am using latest release version 1.26.2, you can check for the latest release version and also use.
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
It may ask for password and you can enter for your user’s password.
Next set the permission to make the binary executable using the following command:
$ sudo chmod +x /usr/local/bin/docker-compose
Finally verify that the installation was successful by checking the version using the following command:
$ docker-compose --version
It will show you the following information:
docker-compose version 1.26.2, build eefe0d31
Please make sure you follow the directory structure as given in the source code.
Project Directory Setup
The first thing is to make sure that you have setup the required folders for your app. I am going to create a root folder and inside this root folder I will create two more folders – one for app’s root directory where all your project files will be kept and another one for putting database script.
The top level root folder is nodejs_mysql_rest_api_crud_docker which you can create using the command on your Unix terminal:
$ sudo mkdir nodejs_mysql_rest_api_crud_docker
Inside the above directory I have two more directories – app and db.
Inside the app directory you need to src, config, app.js and package.js files/folders. Inside your db folder you need to have the items.sql script.
I am not going to show you the source code here but you can always download it from here.
Next you need to create a Dockerfile under your app directory with the following content.
FROM node:12 COPY . /app WORKDIR /app RUN npm install RUN npm install express RUN npm install mysql COPY . . RUN apt-get update && apt-get install -y netcat COPY wait.sh /wait.sh RUN chmod +x /wait.sh
I am using Nodejs version 12, so I am using Nodejs version 12 image using
I am copying all files/folders from the current directory to app directory. Setting working directory as app directory using WORKDIR command.
Next I am installing required packages for my application. Note I am using mysql which is old client and it will throw the following error:
MySQL 8.0.21 - Client does not support authentication protocol requested by server; consider upgrading MySQL client
For the above error you can use any one of the two solutions. The first solution is to use mysql2 instead of mysql. Then you need to use the line
const mysql = require('mysql2'); instead of
const mysql = require('mysql'); for connecting to your database.
The second solution is to use the below line in your docker-compose.yml/yaml file.
I have used the above line in my docker compose file, but you can try to implement the first solution in your app.
Next I have written
COPY . . to build the app source.
After that I have installed nc (netcat) package to execute the nc command for checking a particular host/port is up or not. For my case I am checking whether MySQL server is up or not. If MySQL server is not up and by this time the Nodejs application is up then it will throw error because it will try to connect to MySQL server.
So I have written one shell script for this and it is written into wait.sh file which is kept under app directory.
#!/bin/sh while ! nc -z db 3306 ; do echo "Waiting for the MySQL Server" sleep 3 done node app.js
In the above shell script you can see that I am waiting for the MySQL server to be startup before starting the Nodejs app. I am also sleeping 3 seconds before checking again whether MySQL server is up or not.
Create a .dockerignore file in the same directory as your Dockerfile with following content:
This will prevent your local modules and debug logs from being copied onto your Docker image and possibly overwriting modules installed within your image.
I already explained why do you need docker compose. So here I am going to show you the whole content for docker-compose.yml/yaml file.
I have used depend-on to arrange the services in order but it does not guarantee that the services kept in order will be executing first and here the situation where you need to control the execution of your services using shell script.
version: "3" services: nodejs: image: rest-app restart: on-failure build: context: ./app dockerfile: Dockerfile depends_on: - db ports: - "4000:4000" entrypoint: ["./wait.sh"] db: image: mysql:8.0.21 command: --default-authentication-plugin=mysql_native_password restart: on-failure ports: - "33000:3306" environment: MYSQL_ROOT_PASSWORD: root volumes: - ./db:/docker-entrypoint-initdb.d/:ro
The entrypoint keyword is used to here to execute the shell script file, i.e., wait.sh.
The restart I have used here if app fails to start then only restart the app.
I have mentioned which directory to look for Dockerfile in the app for building the image.
Testing the Application
Well, I am finished with my coding and the required docker configurations.
Execute the command
$ docker-compose up to bring up all services. Wait for few moments, the docker will pull the required images and build the apps and starts up all apps.
If you think that something went wrong in your apps then you can remove all images using the command
$ docker-compose down --rmi all.
You can also execute command
$ docker-compose up --build to re-build your images.
When your apps are up and running you can use
links command to access your API endpoints. Even you can access using REST client.
Once your app and MySQL server are up and running, you will see the output similar to the following image:
The app can be access via your server IP as shown in the following image. The below image displays the root path or home page of the app.
Next you can use the Postman tool or any REST client to test your application, only thing you need to use your server IP instead of localhost for the host.
I am not going to show you how to test the REST CRUD operations but it is similar to the example given in Nodejs MySQL REST API for CRUD operations.
You can also access your MySQL server which is running inside the docker container using the following command:
$ mysql --host=127.0.0.1 --port=33000 -u root -p
When it prompts for password, enter your password to get the MySQL client terminal. The port which you see here 33000 is the external port but the port 3306 is used inside the container.
Why I am using different port because 3306 port is already used by MySQL server which is accessible publicly and will be a conflict with the container’s port if I use the same port outside the container.
Thanks for reading.