A guide on how to Deploy Node.js and MySql in a Docker container:
Node.js being so popular and MySQL being one of the most sought after DB management system, it is required for an application to combine these two and get the optimal results.
Before starting we can have a brief idea about Docker, and Node.js
Wikipedia defines Docker as an open-source project that automates the deployment of software applications inside containers by providing an additional layer of abstraction and automation of OS-level virtualization on Linux.
Node.js is a open source cross-platform for server end programming that allows the user to create application in ease.
The definition of Node.js as supplied by its official documentation is as follows −
Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
This tutorial is divided into 3 sections.
1.Installing and configuring Docker
2.Deploying Node.js container
3.Deploying MySQL container

Installing and configuring Docker

To follow this tutorial, we will need the following:

  • 64-bit Ubuntu 16.04 server

Installing Docker
The Docker installation package available in the official Ubuntu 16.04 repository may not be the latest version.
Download and install Docker from the official Docker repository itself.
1. First, add the GPG key for the official Docker repository to the system:
# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
2. Add the Docker repository to APT sources:
# add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
3. Update the package database with the Docker packages from the newly added repo:
# apt-get update
4. Make sure the installation is from the Docker repo instead of the default Ubuntu16.04 repo:
# apt-cache policy docker-ce

5. Finally, install Docker:
# apt-get install -y docker-ce

6. Docker daemon will be started automatically once installation is completed. To check the status of the docker, use the command.
# systemctl status docker

Docker commands
The syntax for docker is
# docker [option] [command] [arguments]
To see all the available command on docker type the following.
To see the switches available to a specific command, type:
# docker docker-subcommand --help
→> If you wish to know more about docker commands and its usage you can refer our blog.

Deploying Containers

All Docker containers are started based on a Docker Image. These images contain everything required to launch the process; the host doesn’t require any configuration or dependencies.
You can deploy a container that is already configured with node.js application.
Use the below command to see available images for node.js application.
# docker search nodejs

In the OFFICIAL column, OK indicates an image built and supported by the company behind the project. We are downloading the centos/nodejs-6-centos7 from the repository. To download an image use the command
# docker pull Image name ## Replace image name with the image that need to downloaded
The pull command fetches the centos from the Docker registry and saves it to our system. You can use the Docker images command to see a list of all images on your system.
Another method to deploy a node.js container is to manually install node.js on a base image like CentOS, Ubuntu. In this session I am using the second method.
1. Download base image (I am using CentOS 7 as the base image).
# docker pull centos
More information about CentOS container is available on https://hub.docker.com/_/centos/
2. Use the ‘images’ command to check images exist on your system
# docker images

3. Run the container, Use the command ‘docker run centos’ to start the container. However, since it doesn’t have anything to do, and it shuts down immediately. Instead, use the switch -i and -t to run interactively.
#docker run -i -t centos
2. install Node.js and npm from NodeSource
1. Add NodeSource yum repository to our system. The current LTS version of Node.js is version 8.x.
#curl -sL https://rpm.nodesource.com/setup_8.x | bash -
Once the NodeSource repository is enabled install Node.js and npm
#yum install nodejs
To verify that the installation was successful, we can run the following commands which will display the Node.js and npm versions.
#node --version

#npm --version

To access a MySQL database with Node.js, you need a MySQL driver.
We can install the MySQL driver with npm installer.
#npm install mysql
You should now be able see MySQL module under /node_modules directory.

Before connecting to MySQL we will deploy the MySQL container first. Here I will be using a container which already has MySQL installation. For that, we can use the command.
#docker pull centos/mysql-57-centos7
Now the MySQL container will be downloaded to our local environment which can be verified with the command;
#docker images
Now we need to start running the container. Since we are setting only the mandatory variables we can run the container with the following command.
#docker run -d --name mysql_database -e MYSQL_USER=user -e MYSQL_PASSWORD=pass -e MYSQL_DATABASE=db -p 3306:3306 centos/mysql-57-centos7
This will create a container named mysql_database running MySQL with database
db and user with credentials user, pass. Port 3306 will be exposed and mapped
to the host with the user as ‘user’ and database ‘db’.
The below documentation explains more about the variables that are used here.
The problem here is that the IP of the container will be changed on container restart. After every restart we will need to edit the node.js code to replace the IP of the MySQL container.
The solution for this is to assign a static IP for the container during deployment so that the IP remains the same even if we restart the container.
First, we need to create our own docker network (mynet123)
#docker network create --subnet= mynet123
##mynet123 is the name given for the network that I have created.
Then run the image
# docker run --net mynet123 --ip --name mysql_database -e MYSQL_USER=user -e MYSQL_PASSWORD=pass -e MYSQL_DATABASE=db -p 3306:3306 centos/mysql-57-centos7
(Change the subnet and the IP address according to your need).
Next, we can verify the IP address of this container.
#docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' c19b629c3210 ## c19b629c3210 is the container ID

Now we will access the node.js server and add the DB connect code.

# docker ps
# docker exec -it 877c819cfa50 /bin/bash
# mkdir mysql ## creating directory to add the file
# vi mysql_connect.js ##

You can use any file name however, the extension must be .js

Paste the code below

var mysql = require(‘mysql’);
var con = mysql.createConnection({
  host: “xxx:xxx:xxx:xxx”,
  user: “yourusername”,
  password: “yourpassword”
con.connect(function(err) {
  if (err) throw err;
  console.log(“##Successfully connected to MySQL container##”);

Note that you will need replace the following,

Host – IP address of the MySQL container
User – MySQL username
Password – MySQL password
The MySQL username and password are the same which we used while starting the MySQL instance. In my case, I have used the username as ‘user’ and password as ‘pass’
Finally, save the file.

New to Docker?

We have an expert team to guide you on your journey to containerization

We can now run the code by executing the command
# node mysql_connect.js
If the connection to the container is successful you will see output similar to this.

That is it. Happy containerization!
Thanks for dropping by. Ready for the next blog?

Get new insights right to your inbox

How can our experts help you?

Schedule your consultation

You may also like

  • By admin
  • in DevOps

Agile vs DevOps: What’s the difference

  • Nov 18, 2022 .
  • 9 min min
Read More
  • By admin
  • in DevOps

DevOps as a Service: All You Should Know

  • Aug 9, 2022 .
  • 9 min min
Read More
  • By admin
  • in Containerization

Containerization VS Virtualization: Understanding the Differences

  • Aug 4, 2022 .
  • 8 min min
Read More

Be in the know

Techno tips served hot! Subscribe now and stay atop.