By Vivek - November 10, 2020

How to Dockerize A Svelte App

Dockerizing a Svelte app gives you a repeatable production runtime for local testing, servers, and CI. The exact Dockerfile depends on whether your app is a static Svelte app, a SvelteKit app, or an older Rollup-based app, but the core steps are the same: install dependencies, build the app, expose the right port, and run the production server.

Table of Contents

  1. Create a Dockerfile
  2. Build the image
  3. Run the container
  4. Common mistakes
  5. Related Svelte guides

Create a Dockerfile

For an older Rollup-based Svelte app, create a Dockerfile in the app directory:

FROM node:14-alpine

# install dependencies
WORKDIR /app
COPY rollup.config.js ./
COPY package*.json ./
RUN npm install

COPY ./src ./src
COPY ./public ./public
RUN npm run-script build

EXPOSE 5000
ENV HOST=0.0.0.0
CMD [ "npm", "start" ]

For a current SvelteKit app, make sure your SvelteKit adapter produces a Node server before using a Node runtime image.

Build the image

docker build -t myapp .

Run the container

docker run -p 5000:5000 myapp

The first port is the host port. The second port is the port exposed by the app inside the container.

Common Mistakes

The app only works on localhost inside the container

Make sure the production server binds to 0.0.0.0, not just localhost, so traffic can reach it through Docker’s port mapping.

Dependencies are reinstalled too often

Copy package.json and the lockfile before copying the rest of the source. That lets Docker reuse the dependency layer when only app code changes.

The wrong port is exposed

Match the EXPOSE value, the app’s runtime port, and the docker run -p mapping.

Related Svelte Guides