Eddie is already almost 2 years old. Eddie is the name of my server where I run all the backends for my applications. I always just setup the applications and databases right on the server mostly with pm2. Since I develope more and more backend-applications I decided to dockerize all my current ones. So here is the way I went with my backend for the All-Time-Table App.

The current situation is, that I have a node-application on port 8055 and MySQL-Database on port 3306 running with pm2.

Let’s start:

1. Node Dockerfile

Create a file called Dockerfile in the root-folder of your node application with the following content

1
2
3
4
5
6
7
FROM node:8
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD npm start
EXPOSE 8055

For npm start (line 6) to work you need to define it in your package.json like this:

1
2
3
4
"scripts": {
    "dev": "nodemon server.js",
    "start": "node server.js"
  }

Or just use node server.js instead of npm start in your Dockerfile.

2. MySQL Dockerfile and SQL dump

Create anfolder in your node-root-folder called mysql an in there a file called Dockerfile

1
2
3
4
5
6
FROM mysql

ENV MYSQL_DATABASE your_database_name
ENV MYSQL_ROOT_PASSWORD your_root_password

COPY init_db.sql /docker-entrypoint-initdb.d/

If you don’t want to fill your database with a dump you don’t need the last line. In case you wat to fill it with a dump you have to put your sql-file in the mysql-folder with the name init_db.sql. Be sure to check your sql-syntax to avoid errors.

3. Docker-Compose File

Back in the node-root-folder create a file calles docker-compose.yml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
version: '3.3'

services:
    node-app:
        build: .
        environment:
            - DATABASE_HOST=db
        depends_on:
            - db
        restart: always
        ports:
            - 8055:8055
    db:
        build: ./mysql
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: your_root_password
            MYSQL_DATABASE: your_database_name

4. Connection Settings

Update the mysql-connection in your node-application:

1
2
3
4
5
6
7
8
mysql.createConnection({
	host     : process.env.DATABASE_HOST || '127.0.0.1',
	user     : 'root',
	password : 'your_root_password',
	database : 'your_database_name',
	port: 3306,
	insecureAuth : true
});

5. Docker-Ignore File

For docker to ignore some files add a file called .dockerignore in the root-folder. Add every file, you dont neet in the docker-container. Mine looks like this:

1
2
3
4
5
6
node_modules
npm-debug.log
docs
mysql.txt
test.js
mysql

6. Final folder-structure

Here is an overfiew of the root-folder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.
├── app
│   ├── routes
│   └── scraper
├── docker-compose.yml
├── Dockerfile
├── .dockerignore
├── mysql
│   ├── Dockerfile
│   └── init_db.sql
├── node_modules
├── npm-debug.log
├── package.json
├── package-lock.json
└── server.js

7. Docker Compose

  1. Use the command sudo docker-compose build --no-cache --force-rm to build the images.
  2. To start the containers together just type sudo docker-compose up and voilà, everything shoud work.