loading...

Dockerizing an AdonisJs App

aligoren profile image Ali GOREN ・4 min read

Dockerizing An AdonisJs App

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

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

2-) Add Docker Repositories to APT resources

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

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

sudo apt-get update

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

apt-cache policy docker-ce

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

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

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

sudo systemctl status docker

The output should be like this;

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.

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

sudo apt install docker-compose

You can access more information for other operating systems with this link.

Installation of AdonisJs

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

npm i -g @adonisjs/cli

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

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;

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;

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.

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.

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;

touch Dockerfile

The Dockerfile will be like this

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;

touch docker-compose.yml

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

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

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

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.

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;

sudo docker-compose start

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

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;

# dock.sh
sudo docker-compose stop

sudo docker-compose build

sudo docker-compose up -d

sudo docker-compose start

you will use this;

bash dock.sh

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

Posted on by:

aligoren profile

Ali GOREN

@aligoren

I'm a front-end developer. I'm living in Turkey. I started my professional career in 2016. I also interest in Backend, SQL technologies.

Discussion

pic
Editor guide
 

Sudo is a bad practice here.
Regarding to base layer, better to fetch alpine version for demonstration.
FROM node:8-alpine

 

how do I attach to the container and run the command adonis migration:run

 

Thank you for this post.
How would you add a PostgreSQL DB?