My Dockerfile for SvelteKit

November 24, 2021 by Steven Ng

I pretty much do most of my web development in Sveltekit these days. Because I mostly prefer to deploy to Docker containers, it comes in handy having a reusable Dockerfile template.

I've got a template below that can be used with Sveltekit applications built with the node-adapter.

FROM node:16-alpine

ENV NODE_ENV production

RUN apk add dumb-init
RUN apk update && apk add bash

WORKDIR /home/app

COPY package*.json ./
COPY .env ./

RUN npm ci --only=production

COPY . .

EXPOSE 3000

CMD ["dumb-init", "node", "/home/app/build/index.js"]

The key points to know:

  1. I use /home/app as my working folder, feel free to substitute that with your own preferred path.
  2. I don't use my Sveltekit .env for storing environment variables. For me, it's more like a config file for my web application. My .env file contains the names of the environment variables that I use in the app, which is why I copy it to the container.To access environment variables in my Docker compose file, I access them using process.env.
  3. I like to have a bash shell in my containers in case something bad happens.
  4. I use dumb-init to start my process as PID 1.
  5. I copy . . because I have additional files beyond the build folder as part of my application (for database migrations, etc.). I use .dockerignore to exclude anything not required in my container image.