DEV Community

Itachi Uchiha
Itachi Uchiha

Posted on

Dockerizing an AdonisJs App

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 -
Enter fullscreen mode Exit fullscreen mode

2-) Add Docker Repositories to APT resources

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

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
Enter fullscreen mode Exit fullscreen mode

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

apt-cache policy docker-ce
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

5-) Docker Installation

We can install Docker now with this command

sudo apt-get install -y docker-ce
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

adonis –help
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

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"
})
Enter fullscreen mode Exit fullscreen mode

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

adonis serve --dev
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Now our application will use port 8080.

The Dockerfile

To create a Dockerfile we will use this command;

touch Dockerfile
Enter fullscreen mode Exit fullscreen mode

The Dockerfile will be like this

FROM node:8

WORKDIR . /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8080

CMD [ "npm", "start" ]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

web:
  build: .
  ports:
   - "8080:8080"
  volumes:
   - .:/code
Enter fullscreen mode Exit fullscreen mode

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

sudo docker-compose build
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

sudo docker-compose stop
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

you will use this;

bash dock.sh
Enter fullscreen mode Exit fullscreen mode

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

Top comments (5)

Collapse
 
maikenegreiros profile image
Maike Negreiros • Edited

Great. I have a question that disturbs me all time I see a post about dockerizing an Adonis application. Why not install Adonis inside the container, I mean in the Dockerfile? By doing so each dev that will come to work in this project will able to execute docker without needing to install Adonis before

Collapse
 
wemersonrv profile image
Wemerson Couto Guimarães

Hello. Great article.

I'm trying to make it to work locally on my development, so how can i configure it to run only the adonis CLI instead PM2 ? ("adonis serve --dev")

Ah of course, i don't want to run npm install on build, i need to run it manually every time new package was added to package.json; so need to run this inside the container from host.

Collapse
 
dimpiax profile image
Dmytro Pylypenko

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

Collapse
 
southribbletech profile image
Simon Carr

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

Collapse
 
psas profile image
PS

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