Nodejs + MYSQL + docker-compose

Shubham Verma
4 min readMar 26, 2021

--

In this article we will create a docker-compose.yaml file for nodejs and MYSQL application.
If you want to look at the code then go to my github repo

Dockerize nodejs and mysql

Content

  • What is Docker ?
  • Dockerizing nodejs backend
  • Dockerizing MYSQL
  • Creating docker-compose file
  • Running the app

What is Docker?

Docker is an open platform for developing, shipping, and running applications.
It provides the ability to package and run an application in a loosely isolated environment called a container.
Containers are lightweight and contain everything needed to run the application, so you do not need to rely on what is currently installed on the host. You can easily share containers while you work, and be sure that everyone you share with gets the same container that works in the same way.

If you want to play around with docker go to docker-playground and create a new instance.
Make sure node is not installed in that instance by running.

node --version

Even with node not installed in a machine, we can run a node application(that is the beauty of docker).
Run the below command to start a simple nodejs application

docker run -dp 5000:5000 varsubham/sample_node

The above command will pull the sample_node image from docker-hub, create a docker container from that image, and run it.
Although its a very simple example but you get the idea 😃

Dockerizing nodejs backend

Before creating the application, make sure docker and docker-compose are installed in your machine.

Our final goal is to run two docker container-
1. Nodejs container
2. MYSQL container

And nodejs container should be able to communicate to MYSQL container.

Lets create a simple nodejs backend:

mkdir web && cd web
npm init -y
npm install express mysql
touch index.js
  • First we create a web(for backend)folder (because we will create separate Dockerfile for backend)
  • Then initialize it with npm and install express for creating a route and mysql for communicating with MYSQL Database
  • Finally we create index.js where we will write the following code:

We are connecting to MYSQL database named test and in the / GET route we are displaying all the content stored in the Student table.

Now we need to dockerize our backend.
So lets create a Dockerfile(without any extension) in the same web folder.

FROM node:15-alpineWORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 5000 CMD ["npm", "start"]
  • We are using node:15 as a base image (alpine because it is very small in size).
  • Then we create a working directory /app inside the container
  • Then copy package.json and package-lock.json to the /app folder
  • Run npm install to install dependencies (in our case express and mysql)
  • Copy the remaining files (make sure to create .dockerignore file and add node_modules to ignore node_modues being copied)
  • Expose port 5000, so that we can access the app outside the container
  • Finally run npm start to start the backend (make sure to add npm start script in package.json)

Dockerizing MYSQL

We have already created the nodejs backend and the Dockerfile.

Now lets create MYSQL docker image.
First go to the root folder and in the root folder create a new folder named db and cd into it

mkdir db && cd db
touch testdump.sql
touch Dockerfile
  • In testdump.sql we will be write the starting SQL queries which will run when the MYSQL container starts.
    We need to create a Student table and insert few values inside it so that we can query it from our nodejs application.

Write the following in testdump.sql —

CREATE TABLE Student(
student_id INT PRIMARY KEY AUTO_INCREMENT,
student_name VARCHAR(60),
student_age INT
);
INSERT INTO Student(
student_name,
student_age)
VALUES(
"Shubham verma",
21
);
INSERT INTO Student(
student_name,
student_age)
VALUES(
"Utkarsh verma",
23
);
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'password'; flush privileges;

Here we have created a table Student and inserted 2 entries.

Also you need to alter the user root because mysql in Node (the package we installed with npm install mysql ) does not support the new default authentication method of MySQL 8
That is why we need to run a query saying root is fine using old mysql_native_password method for authentication.

Now lets create Dockerfile for MYSQL image

FROM mysql EXPOSE 3306 COPY ./testdump.sql /docker-entrypoint-initdb.d/

We used the latest MYSQL as root image, exposed the default port(3306) and copied the testdump.sql to docker-entrypoint-initdb.d (all the sql script inside this folder will execute when the container start)

Creating docker-compose file

We are almost done. We can build and run both the docker container seperately, but the effective way is to create a docker-compose.yaml file(describing all the configurations) to start our entire application with a single command.

Move to the root folder and there create docker-compose.yaml file

touch docker-compose.yaml

Copy the following into docker-compose file:

Here we are creating 2 services:

  • db (MYSQL service)
  • web (Nodejs backend service)

Also we are specifying build folder for both the services

Adding the environment variable so that our nodejs can connect to mysql

And finally depends_on: db will make sure that db container starts before web container

Running the app

And we are done. Now to run the app all we have to do is:

docker-compose up

This will first create the docker images and then run both the containers.
To view the application go to http://localhost:5000/

And voila!!
We have successfully dockerized our application 🙂

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Shubham Verma
Shubham Verma

Responses (3)

Write a response