All snippets

Dockerfile for AdonisJS and using Bun as a package manager

Jun 18, 2020·1 min read

Dockerfile

ARG NODE_VERSION=22.12.0
ARG ALPINE_VERSION=3.19

##
# 1) Build Stage
##
FROM node:${NODE_VERSION}-alpine${ALPINE_VERSION} AS build

ARG BUN_VERSION=1.1.42

WORKDIR /build

# Install Bun + needed packages
RUN apk add --no-cache bash curl zstd unzip && \
    curl -fsSL https://bun.sh/install | bash -s -- bun-v${BUN_VERSION}

ENV PATH="${PATH}:/root/.bun/bin"

# Copy lock file and package.json, then install dependencies
COPY bun.lockb package.json ./
RUN bun install --frozen-lockfile

# Copy application sources and build
COPY . .
RUN bun run build

# Remove dev dependencies, re-install production deps
RUN rm -rf node_modules \
    && rm -rf /root/.bun/install/cache/ \
    && bun install --frozen-lockfile --production

# Optional: Prune unnecessary files in `node_modules`
RUN curl -sf https://gobinaries.com/tj/node-prune | sh \
    && node-prune

#
# Install Supercronic in build stage
# (Pin the version and verify SHA1 or SHA256)
#
ENV SUPERCRONIC_VERSION=0.2.29
ENV SUPERCRONIC_URL="https://github.com/aptible/supercronic/releases/download/v${SUPERCRONIC_VERSION}/supercronic-linux-amd64"
ENV SUPERCRONIC=supercronic-linux-amd64
ENV SUPERCRONIC_SHA1SUM=cd48d45c4b10f3f0bfdd3a57d054cd05ac96812b

RUN curl -fsSLO "$SUPERCRONIC_URL" \
 && echo "${SUPERCRONIC_SHA1SUM}  ${SUPERCRONIC}" | sha1sum -c - \
 && chmod +x "${SUPERCRONIC}" \
 && mv "${SUPERCRONIC}" /build/supercronic

##
# 2) Distribution Stage
##
FROM node:${NODE_VERSION}-alpine${ALPINE_VERSION} AS distribution

ENV NODE_ENV="production"

WORKDIR /app

# Copy production artifacts from build stage
COPY --from=build /build/node_modules ./node_modules
COPY --from=build /build/build ./
COPY --from=build /build/package.json ./

# Copy the Supercronic binary from build stage into /usr/local/bin
COPY --from=build /build/supercronic /usr/local/bin/supercronic

# Copy your crontab (if needed)
COPY crontab /app/crontab

# Set up storage and database folder
RUN mkdir -p storage/database

# Copy and set up start script
COPY .fly/start.sh ./start.sh
RUN chmod +x ./start.sh

# Copy your cron runner script
COPY .fly/scripts/cron.sh ./cron.sh
RUN chmod +x ./cron.sh

EXPOSE 8080
ENTRYPOINT ["./start.sh"]

.fly/start.sh

node ace migration:run --force

node bin/server.js