DEV Community

Cover image for Dockerizing AdonisJs (v6) with MySQL
miriamhaenle
miriamhaenle

Posted on

Dockerizing AdonisJs (v6) with MySQL

While working on a new API for my company, I decided to use
AdonisJs in combination with MySQL for the implementation. As we are running all of our projects in Docker, the obvious choice was to dockerize the application.

After performing some research I only found documentation on dockerizing AdonisJS v5 applications. But as I was using AdonisJs v6 and needed to do some slight changes, I wanted to share what I have learned with the community.

I will focus mainly on the Docker parts in this article but will share below a few steps to kick-start the implementation of an API with Adonis with an example application. You can also take a look at the code in this GitHub repository.

In this example we will create a very simple API which returns random quotes from Arnold Schwarzenegger.

Creating a Dockerfile

As it is now mainly recommended to use Docker multistage builds to keep the Docker image small and cache each step, we will also build a multi stage Dockerfile.

First stage: BASE

The first stage will be our base layer, which will be used as a base for the other steps.
As it is recommended for Node.js and Docker we are changing the user from root to node.

ARG NODE_IMAGE=node:20.12.1-bullseye-slim

FROM $NODE_IMAGE as base
RUN mkdir -p /home/node/app && chown node:node /home/node/app 
WORKDIR /home/node/app
USER node
RUN mkdir tmp
Enter fullscreen mode Exit fullscreen mode

Second stage: DEPENDENCIES

In the next step we will install the dependencies that we need to later build the Node.js app.

FROM base AS dependencies
COPY --chown=node:node ./package*.json ./
RUN npm ci
COPY --chown=node:node . .
Enter fullscreen mode Exit fullscreen mode

Third stage: BUILD

With the base all set and the dependencies installed, we can now build our app!

FROM dependencies AS build
RUN node ace build
Enter fullscreen mode Exit fullscreen mode

Fourth stage: PRODUCTION

And the last stage is wiring everything together for production. Note that we are building this stage from the base to keep our image as small as possible and we only install --production dependencies.

FROM base as production
ENV NODE_ENV=production
ENV PORT=$PORT
ENV HOST=0.0.0.0
COPY --chown=node:node ./package*.json ./
RUN npm ci --only=production
COPY --chown=node:node --from=build /home/node/app/build .
EXPOSE $PORT
Enter fullscreen mode Exit fullscreen mode

Combined

This is how the complete Dockerfile should look like:

ARG NODE_IMAGE=node:20.12.1-bullseye-slim

###### First Stage - Creating base ######
FROM $NODE_IMAGE as base
RUN mkdir -p /home/node/app && chown node:node /home/node/app 
WORKDIR /home/node/app
USER node
RUN mkdir tmp

###### Second Stage - Installing dependencies ######
FROM base AS dependencies
COPY --chown=node:node ./package*.json ./
RUN npm ci
COPY --chown=node:node . .

###### Third Stage - Building Stage ######
FROM dependencies AS build
RUN node ace build

###### Final Stage - Production ######
FROM base as production
ENV NODE_ENV=production
ENV PORT=$PORT
ENV HOST=0.0.0.0
COPY --chown=node:node ./package*.json ./
RUN npm ci --only=production
COPY --chown=node:node --from=build /home/node/app/build .
EXPOSE $PORT
Enter fullscreen mode Exit fullscreen mode

Wiring up docker-compose

Now that we have the Dockerfile that will describe our Docker image ready, we will configure our docker-compose.yml to run our Docker image.
In your project root create a docker-compose.yml and add the following contents:

version: '3.8'

services:
  arnold-api:
    image: arnold_api
    container_name: arnold_api
    restart: unless-stopped
    build:
      context: .
      target: build
    ports:
      - ${PORT}:${PORT}
      - 9229:9229
    env_file:
      - .env
    volumes:
      - ./:/home/node/app
      - /home/node/app/node_modules
    command: node ace serve --watch
    depends_on:
      - arnold-mysql

  arnold-mysql:
    image: arm64v8/mysql
    container_name: arnold-mysql
    restart: always
    ports:
      - ${DB_PORT}:${DB_PORT}
    expose:
      - ${DB_PORT}
    volumes:
      - ./db:/var/lib/mysql
volumes:
  db:

Enter fullscreen mode Exit fullscreen mode

If you want to follow the complete project setup, here are the steps for starting from scratch:

Step-by-Step-Setup

  1. Ensure you have Node.js and npm installed on your machine. AdonisJs needs Node.js >= 20.6
node -v
# v20.12.1
Enter fullscreen mode Exit fullscreen mode
  1. Create a new application. As this example is used for implementation of an API with a MySQL database, we use the according flags to have the relevant starter kits available.
 npm init adonisjs@latest adonis-mysql-docker  -- --kit=api --db=mysql --git-init=true
Enter fullscreen mode Exit fullscreen mode

During installation you will be asked to select your authentication guard, to keep this example simple, we will leave authentication so skip that step.

  1. When the installation is done, go into the project directory and open the project with your code editor
cd adonis-mysql-docker
code .
Enter fullscreen mode Exit fullscreen mode
  1. Create an .env file Copy the .env.example and add all relevant environment variables. Please note: It is important to set the HOST to the same value as in the Dockerfile! Otherwise you will not be able to reach the application.
TZ=UTC
PORT=3333
HOST=0.0.0.0
LOG_LEVEL=info
APP_KEY=fFNoe5b32DGXLIf4aHqjyQ6dOpaTdD6u
NODE_ENV=production
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=root
DB_PASSWORD=arnold
DB_DATABASE=arnold-mysql
Enter fullscreen mode Exit fullscreen mode
  1. Adding our route and controller

In the routes.ts adjust the example route to return random quotes:


router.get('/', async () => {
  const quotes = [
    'Turn the pain into power.',
    'Life may be full of pain but that is not an excuse to give up.',
    'There are no shortcuts - everything is reps, reps reps.',
    'Doing nothing is easy. That is why so many people do it.',
    'Work harder than you did yesterday.',
    'Be passionate. This is the only way to be among the best.',
    'If you dont find the time. If you dont do the work, you dont get the results.',
  ]

  return `Arnold Quote of the Day: ${quotes[Math.floor(Math.random() * quotes.length)]}`
})
Enter fullscreen mode Exit fullscreen mode
  1. Add the Dockerfile and the docker-compose.yml as described at the beginning of the post.

Now let's see if everything worked.
Start your docker container by running
docker-compose up -d
Once the container has started you should be able to reach your api:

http://127.0.0.1:3333/

:)

Top comments (0)