Cookie Consent by Free Privacy Policy Generator 📌 Dockerizing AdonisJs (v6) with MySQL


✅ Dockerizing AdonisJs (v6) with MySQL


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

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

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 . .

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

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

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

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
    environment:
      MYSQL_ROOT_PASSWORD: 'arnold'
      MYSQL_PASSWORD: 'arnold'
      MYSQL_USER: 'adonis'
      MYSQL_DATABASE: 'arnold-mysql'
volumes:
  db:

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
  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

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 .
  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
  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)]}`
})
  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/

:)

...

✅ Dockerizing AdonisJs (v6) with MySQL


📈 57.41 Punkte

✅ Dockerizing a Laravel App: Nginx, MySql, PhpMyAdmin, and Php-8.2


📈 29.95 Punkte

✅ Fullstack-Framework: AdonisJS 6 setzt auf ECMAScript und Vite.js


📈 27.46 Punkte

✅ AdonisJS V6 is Out, and it's a Developer's Dream


📈 27.46 Punkte

✅ Announcing AdonisJS v6


📈 27.46 Punkte

✅ How to Build a RESTful API with AdonisJS


📈 27.46 Punkte

✅ Minha experiência com AdonisJS e como fazer seu primeiro CRUD


📈 27.46 Punkte

✅ Building a fullstack login flow with Node.js and remix-adonisjs


📈 27.46 Punkte

✅ Hot module replacement (HMR) in AdonisJS


📈 27.46 Punkte

✅ Fullstack-Framework: AdonisJS 6 setzt auf ECMAScript und Vite.js


📈 27.46 Punkte

✅ Dockerizing Next.js


📈 22.53 Punkte

✅ Dockerizing an Ansible Playbook Part 1


📈 22.53 Punkte

✅ Best Practices and Strategic Insights to Dockerizing Your Linux Applications


📈 22.53 Punkte

✅ Dockerizing your Angular application


📈 22.53 Punkte

✅ Dockerizing a Flask application


📈 22.53 Punkte

✅ 100 Days of Cloud: Day 3 - Dockerizing a Go App for Dita Daystar (Apologies for the Delay!)


📈 22.53 Punkte

✅ Dockerizing a Java Application with Spring and Maven


📈 22.53 Punkte

✅ Dockerizing Python Application


📈 22.53 Punkte

✅ Dockerizing Microservices: Untangling Scaling and Deployment


📈 22.53 Punkte

✅ Dockerizing Laravel (With compose) [Alpine + NGINX + PHP FPM + MariaDB + PHPMyAdmin] 🛳️🛳️


📈 22.53 Punkte

✅ Dockerizing NodeJS, Express, and MongoDB App with NGINX as a Reverse Proxy


📈 22.53 Punkte

✅ Day 6 of 90 Days of DevOps: Dockerizing a Node.js App


📈 22.53 Punkte

✅ Dockerizing Laravel 10 [Ubuntu image + PHP 8.2 FPM + NGINX] 🛳️🛳️


📈 22.53 Punkte

✅ Dockerizing a MERN Stack Web Application


📈 22.53 Punkte

✅ FastAPI for Data Applications: Dockerizing and Scaling Your API. Part II


📈 22.53 Punkte

✅ Dockerizing ML Models: A Deployment Guide


📈 22.53 Punkte

✅ Docker - Dockerizing a Simple Node.js App


📈 22.53 Punkte

✅ Dockerizing Your Application: A Beginner's Guide


📈 22.53 Punkte

✅ Dockerizing a NestJS app and persisting data


📈 22.53 Punkte

✅ Simple Guide to Dockerizing an Angular App


📈 22.53 Punkte

✅ Dockerizing a Node.js Application from Scratch


📈 22.53 Punkte

✅ Conquering the Container: A Guide to Dockerizing Your Angular and Flask App


📈 22.53 Punkte

✅ Building And Dockerizing A Node.js App With Stateless Architecture With Help From Kinsta


📈 22.53 Punkte

✅ Dockerizing Next.js


📈 22.53 Punkte











matomo

Datei nicht gefunden!