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:
- I use
/home/appas my working folder, feel free to substitute that with your own preferred path. - I don't use my Sveltekit
.envfor storing environment variables. For me, it's more like a config file for my web application. My.envfile 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 usingprocess.env. - I like to have a
bashshell in my containers in case something bad happens. - I use
dumb-initto start my process as PID 1. - I copy
. .because I have additional files beyond thebuildfolder as part of my application (for database migrations, etc.). I use.dockerignoreto exclude anything not required in my container image.