Deploy with Node using Docker

This Dockerfile is used to build a Docker image for our Qwik Node.js application. You can edit it accordingly to use npm, pnpm or bun, by replacing yarn commands and yarn.lock file.

We are using the official Node.js Alpine image with Node version set to 18.18.2 (a long term support version). You are free to choose some other version based on your needs.

We then proceed to install dependencies, utilizing Docker caching mechanism by leveraging bind mounts for package.json and yarn.lock to avoid unnecessary reinstallation of dependencies.

Once the dependencies are installed, we proceed to build, setting the NODE_ENV and ORIGIN environment variables.

We finally specify the default command to run our Qwik application with yarn serve, exposing the port 3000: Please note that port number should match the one you chose on your entry adapter (e.g.: src/entry.express.tsx).

ARG NODE_VERSION=18.18.2
 
################################################################################
# Use node image for base image for all stages.
FROM node:${NODE_VERSION}-alpine as base
 
# Set working directory for all build stages.
WORKDIR /usr/src/app
 
################################################################################
# Create a stage for installing production dependencies.
FROM base as deps
 
# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.yarn to speed up subsequent builds.
# Leverage bind mounts to package.json and yarn.lock to avoid having to copy them
# into this layer.
RUN --mount=type=bind,source=package.json,target=package.json \
   --mount=type=bind,source=yarn.lock,target=yarn.lock \
   --mount=type=cache,target=/root/.yarn \
   yarn install --frozen-lockfile
 
################################################################################
# Create a stage for building the application.
FROM deps as build
 
# Copy the rest of the source files into the image.
COPY . .
 
# Run the build script.
RUN yarn run build
 
################################################################################
# Create a new stage to run the application with minimal runtime dependencies
# where the necessary files are copied from the build stage.
FROM base as final
 
# Use production node environment by default.
ENV NODE_ENV production
ENV ORIGIN https://example.com
 
# Run the application as a non-root user.
USER node
 
# Copy package.json so that package manager commands can be used.
COPY package.json .
 
# Copy the production dependencies from the deps stage and also
# the built application from the build stage into the image.
COPY --from=deps /usr/src/app/node_modules ./node_modules
COPY --from=build /usr/src/app/dist ./dist
COPY --from=build /usr/src/app/server ./server
 
# Expose the port that the application listens on.
EXPOSE 3000
 
# Run the application.
CMD yarn serve

You can now build your docker image:

docker build -t your-image .

And start a Docker container:

docker run -dp 127.0.0.1:3000:3000 your-image

Contributors

Thanks to all the contributors who have helped make this documentation better!

  • nelsonprsousa
  • aendel