Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 5406

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T08:56:10+00:00 2024-11-27T08:56:10+00:00

Dockerizing AdonisJs (v6) with MySQL

  • 61k

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/

🙂

devopsnodewebdev
  • 0 0 Answers
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.