Dockerizing an AdonisJs App
Tags: docker, node, adonisjs, programming
Published: 2018-08-11 18:47:12

---
title: Dockerizing an AdonisJs App
published: true
description: Dockerizing An AdonisJs App
tags: docker, nodejs, adonisjs, programming
---

![Dockerizing An AdonisJs App](https://thepracticaldev.s3.amazonaws.com/i/0uky2oh007zs0tkqz7c9.png)

Hi, in this post I will talk about Dockerizing An AdonisJs App. I will use docker-compose to be it easy.

# Dockerizing An AdonisJs App

Before starting I should say, this post will not give any deep information about adonisjs. For example, I will not teach its controller system. If everything is okay with you, let's start.

# Installing Docker to Ubuntu

Docker requires 64-bit Ubuntu system. So if you have an intention to set up your server or your personal computer, you need sure about you have 64-bit Ubuntu system.

### 1-) Add GPG Key to System for Official Docker Repository

```shell
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
```

### 2-) Add Docker Repositories to APT resources

```shell
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
```

In the above code, we used **lsb_release** command with its flags as a variable. It gives your system's codename in short form. [via](https://refspecs.linuxfoundation.org/LSB_2.0.1/LSB-Core/LSB-Core/lsbrelease.html)

### 3-) Let's update our system for newly added repositories.

```shell
sudo apt-get update
```

### 4-) To make sure that the docker repo is received we will use apt-cache.

```shell
apt-cache policy docker-ce
```

We should see an output like below on the command line.

```shell
Version table:
 *** 18.06.0~ce~3-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu artful/stable amd64 Packages
        100 /var/lib/dpkg/status
     18.03.1~ce-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu artful/stable amd64 Packages
     18.03.0~ce-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu artful/stable amd64 Packages
     17.12.1~ce-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu artful/stable amd64 Packages
     17.12.0~ce-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu artful/stable amd64 Packages
```

### 5-) Docker Installation

We can install Docker now with this command

```shell
sudo apt-get install -y docker-ce
```

### 6-) Checking the Docker Running Status

We need to sure about Docker is running successfully. To make this, we will use this command

```shell
sudo systemctl status docker
```

The output should be like this;

```shell
docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: e
   Active: active (running) since Sat 2018-08-11 15:24:13 +03; 1h 59min ago
     Docs: https://docs.docker.com
```

Dear devs, you should know that Docker requires superuser account. So, you always need to use the **sudo** command. If you don't want to do that all the time, you can change user mode for the Docker. But the Docker, don't recommend this.

```shell
sudo usermod -aG docker ${USER}
 
su - ${USER}
```

With this action, we completed the Docker installation. Now, we will install **docker-compose** to easily manage the Docker.

# Installation of Docker Compose

The installation should be like this on Ubuntu

```shell
sudo apt install docker-compose
```

You can access more information for other operating systems with this [link](https://docs.docker.com/compose/install/#install-compose).

# Installation of AdonisJs

With the below command, we will install Adonis CLI as global

```shell
npm i -g @adonisjs/cli
```

After installation, you should check the adonis command on your command line with below command.

```shell
adonis –help
```

# Creating New Adonis Project

I have created a directory called **dockerize**. I entered this directory with the **cd dockerize** command. If you did like that, we will use this command to create a new adonis project;

```shell
adonis new .
```

The dot means current directory. After the project is created, I will open the **routes.js** file. This file under the start folder.

Then I will add a new endpoint called **hello**. So, our routes file should look like this;

```javascript
const Route = use('Route')
 
Route.on('/').render('welcome')
 
Route.get('/hello', async () => {
    return "Hello World"
})
```

Before using docker, I will check our project with the Adonis CLI.

```shell
adonis serve --dev
```

Currently, our project serving on 3333 port. I will change it with 8080 port. I will open the **.env** file under the root folder. Then our **HOST** and **PORT** values will change.

```env
HOST=0.0.0.0
PORT=8080
```

Now our application will use port 8080.

# The Dockerfile

To create a Dockerfile we will use this command;

```shell
touch Dockerfile
```

The Dockerfile will be like this

```dockerfile
FROM node:8
 
WORKDIR . /app
 
COPY package*.json ./
 
RUN npm install
 
COPY . .
 
EXPOSE 8080
 
CMD [ "npm", "start" ]
```

I have chosen the version of Node JS 8. I have set the port with the EXPOSE. Then command parameters should be set.

After that, we will prepare the **docker-compose.yml** file. To create a **docker-compose.yml** file we will use this command;

```shell
touch docker-compose.yml
```

The content of the **docker-compose.yml** file should be like this

```yaml
web:
  build: .
  ports:
   - "8080:8080"
  volumes:
   - .:/code
```

Now let's run the following commands in order to publish the project:

```shell
sudo docker-compose build
```

This command is building the project. At this time the project is not deployed yet. Then with below command, we will up the project in the detached mode with -d flag.

```shell
sudo docker-compose up -d
```

The **-d** flag runs processes in the background and terminates them.

Now, I will start the project with the below command;

```shell
sudo docker-compose start
```

Your project running on port 8080 now. If your project codes change, you need stop docker container

```shell
sudo docker-compose stop
```

then repeat the first three steps again. Wait! "If you're saying I'm lazy people and I have a very very basic project", you can create a bash script like below;

```shell
# dock.sh
sudo docker-compose stop
 
sudo docker-compose build
 
sudo docker-compose up -d
 
sudo docker-compose start
```

you will use this;

```shell
bash dock.sh
```

That's all. Thanks for reading. I hope this will help you.