<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: miriamhaenle</title>
    <description>The latest articles on Forem by miriamhaenle (@miriamhaenle).</description>
    <link>https://forem.com/miriamhaenle</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F377937%2Fee006acc-0cad-430b-9369-e124ff91a394.png</url>
      <title>Forem: miriamhaenle</title>
      <link>https://forem.com/miriamhaenle</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/miriamhaenle"/>
    <language>en</language>
    <item>
      <title>Dockerizing AdonisJs (v6) with MySQL</title>
      <dc:creator>miriamhaenle</dc:creator>
      <pubDate>Fri, 26 Apr 2024 14:36:07 +0000</pubDate>
      <link>https://forem.com/miriamhaenle/dockerizing-adonisjs-v6-with-mysql-2eo2</link>
      <guid>https://forem.com/miriamhaenle/dockerizing-adonisjs-v6-with-mysql-2eo2</guid>
      <description>&lt;p&gt;While working on a new API for my company, I decided to use &lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://github.com/miriamhaenle/adonis-mysql-docker"&gt;GitHub repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this example we will create a very simple API which returns random quotes from Arnold Schwarzenegger.&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating a Dockerfile
&lt;/h2&gt;

&lt;p&gt;As it is now mainly recommended to use &lt;a href="https://docs.docker.com/build/building/multi-stage/"&gt;Docker multistage builds&lt;/a&gt; to keep the Docker image small and cache each step, we will also build a multi stage Dockerfile. &lt;/p&gt;
&lt;h3&gt;
  
  
  First stage: BASE
&lt;/h3&gt;

&lt;p&gt;The first stage will be our base layer, which will be used as a base for the other steps. &lt;br&gt;
As it is &lt;a href="https://snyk.io/blog/10-best-practices-to-containerize-nodejs-web-applications-with-docker/"&gt;recommended for Node.js and Docker&lt;/a&gt; we are changing the user from &lt;code&gt;root&lt;/code&gt; to &lt;code&gt;node&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ARG NODE_IMAGE=node:20.12.1-bullseye-slim

FROM $NODE_IMAGE as base
RUN mkdir -p /home/node/app &amp;amp;&amp;amp; chown node:node /home/node/app 
WORKDIR /home/node/app
USER node
RUN mkdir tmp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Second stage: DEPENDENCIES
&lt;/h3&gt;

&lt;p&gt;In the next step we will install the dependencies that we need to later build the Node.js app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM base AS dependencies
COPY --chown=node:node ./package*.json ./
RUN npm ci
COPY --chown=node:node . .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Third stage: BUILD
&lt;/h3&gt;

&lt;p&gt;With the base all set and the dependencies installed, we can now build our app!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM dependencies AS build
RUN node ace build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Fourth stage: PRODUCTION
&lt;/h3&gt;

&lt;p&gt;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 &lt;code&gt;--production&lt;/code&gt; dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Combined
&lt;/h3&gt;

&lt;p&gt;This is how the complete Dockerfile should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ARG NODE_IMAGE=node:20.12.1-bullseye-slim

###### First Stage - Creating base ######
FROM $NODE_IMAGE as base
RUN mkdir -p /home/node/app &amp;amp;&amp;amp; 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Wiring up docker-compose
&lt;/h2&gt;

&lt;p&gt;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. &lt;br&gt;
In your project root create a &lt;code&gt;docker-compose.yml&lt;/code&gt; and add the following contents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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:

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to follow the complete project setup, here are the steps for starting from scratch: &lt;/p&gt;

&lt;h2&gt;
  
  
  Step-by-Step-Setup
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Ensure you have Node.js and npm installed on your machine. AdonisJs needs &lt;strong&gt;Node.js &amp;gt;= 20.6&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node -v
# v20.12.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;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.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; npm init adonisjs@latest adonis-mysql-docker  -- --kit=api --db=mysql --git-init=true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;During installation you will be asked to select your authentication guard, to keep this example simple, we will leave authentication so skip that step.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When the installation is done, go into the project directory and open the project with your code editor
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd adonis-mysql-docker
code .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;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.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Adding our route and controller&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the &lt;code&gt;routes.ts&lt;/code&gt; adjust the example route to return random quotes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
router.get('/', async () =&amp;gt; {
  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)]}`
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Add the &lt;code&gt;Dockerfile&lt;/code&gt; and the &lt;code&gt;docker-compose.yml&lt;/code&gt; as described at the beginning of the post.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now let's see if everything worked.&lt;br&gt;
Start your docker container by running&lt;br&gt;
&lt;code&gt;docker-compose up -d&lt;/code&gt;&lt;br&gt;
Once the container has started you should be able to reach your api:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;http://127.0.0.1:3333/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;:)&lt;/p&gt;

</description>
      <category>node</category>
      <category>devops</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
