2 minutes
Dockerizing AdonisJS v5
This guide is outdated, refer to » up-to-date guide «
Tried putting AdonisJS v5 preview build into container. It is doable but requires some workarounds to work fine.
Whole source code: https://gitlab.com/McSneaky/youtube-downloader
Dockerfile
# Build AdonisJS
FROM node:14-alpine as builder
# Workaround for now, since bodyparser install relies on Git
RUN apk add --no-cache git
# Set directory for all files
WORKDIR /home/node
# Copy over package.json files
COPY package*.json ./
# Install all packages
RUN npm install
# Copy over source code
COPY . .
# Build AdonisJS for production
RUN npm run build --production
# Install packages on different step,
# since bodyparser install requires git
# but runtime does not need it
FROM node:14-alpine as installer
# Workaround
RUN apk add --no-cache git
# Set directory for all files
WORKDIR /home/node
# Copy over package.json files
COPY package*.json ./
# Install only prod packages
RUN npm ci --only=production
# Build final runtime container
FROM node:14-alpine
# Set environment variables
ENV NODE_ENV=production
# Disable .env file loading
ENV ENV_SILENT=true
# Set app key at start time
ENV APP_KEY=
# Install deps required for this project
RUN apk add --no-cache ffmpeg
# Use non-root user
USER node
# Make directory for app to live in
# It's important to set user first or owner will be root
RUN mkdir -p /home/node/app/
# Set working directory
WORKDIR /home/node/app
# Copy over required files from previous steps
# Copy over built files
COPY --from=builder /home/node/build ./build
# Copy over node_modules
COPY --from=installer /home/node/node_modules ./node_modules
# Copy over package.json files
COPY package*.json ./
# Expose port 3333 to outside world
EXPOSE 3333
# Start server up
CMD [ "node", "./build/server.js" ]
server.ts
Server.ts required some workarounds too to make Adonis close itself when kill signal is sent to container: https://github.com/AdonisCommunity/create-adonis-ts-app/issues/5
import 'reflect-metadata'
import sourceMapSupport from 'source-map-support'
import { Ignitor } from '@adonisjs/core/build/standalone'
sourceMapSupport.install({ handleUncaughtExceptions: false })
const server = new Ignitor(__dirname)
.httpServer()
server.start()
.catch(console.error)
// Without it process won't die in container
process.on('SIGINT', () => {
server.kill(10)
})
Read other posts