DEV Community

DeVoresyah ArEst
DeVoresyah ArEst

Posted on

Dockerizing NuxtJS with Bun

Background 💬

Previously, I'm using the old way to deploy my NuxtJS app into AWS EC2. Building the production locally, and then uploading it through sftp. It's because I don't understand yet about building a custom docker image

Learning by Doing ✍️

The best way to learn fast is by doing. Fortunately, I've a custom Dockerfile for my backend using AdonisJS. My friend who created it. Then I just read all the lines from that file and asked my friend what's the meaning of each line.

Thank god he understands well about docker, so he can explain it to me very well.

Time to Cook 🔥

Why Bun?

Bun is a fast JavaScript package manager. And previously I was using Yarn as my main package manager. But, Bun is faster.

First stage - Base

The first stage will be our base layer which will later on be used for other steps.

ARG NODE_IMAGE=oven/bun:1-alpine

FROM --platform=linux/amd64 $NODE_IMAGE AS base
WORKDIR /usr/src/app
RUN apk --no-cache add openssh g++ make python3 git
Enter fullscreen mode Exit fullscreen mode

Second stage - Installing Dependencies

Our second stage will be used to install all dependencies, so we can build our app in later stages.

FROM base AS install
RUN mkdir -p /temp
COPY package.json bun.lockb /temp/
RUN cd /temp && bun install --frozen-lockfile
Enter fullscreen mode Exit fullscreen mode

If you realize, I didn't use --production. It's because I still need to install devDependencies so I can run the nuxt command

Third stage - Build

We have everything to start our build phase! This very simple and self-explanatory stage.

FROM install AS prerelease
COPY --from=install /temp/node_modules node_modules
COPY . .

ENV NODE_ENV=production
RUN bun run build
Enter fullscreen mode Exit fullscreen mode

Fourth stage - Production

Finally, we can create our last stage.

FROM base AS release
COPY --chown=bun:bun --from=install /temp/node_modules node_modules
COPY --chown=bun:bun --from=prerelease /usr/src/app/.output .

USER bun
ENTRYPOINT [ "bun", "run", "server/index.mjs" ]
Enter fullscreen mode Exit fullscreen mode

Test Locally 🤖

After I successfully built the docker image in under 5 minutes. I just need to test the docker image by running a container on my local. And I'm using Docker Desktop by the way.

Image description

Boom, it can run smoothly without any errors.

Top comments (0)