Create and Deploy Sveltekit inside a Nodejs Docker Container

Create and Deploy Sveltekit inside a Nodejs Docker Container

Node js is a great environment for deploying Sveltekit project . It runs smoothly.

  1. Install Nodejs adapter for Sveltekit . This is a official package for deployment Sveltekit inside a Nodejs environment
npm install @sveltejs/adapter-node@next --save-dev
  1. check inside the package.json for devDependencies and you find @sveltejs/adapter-node
"devDependencies": {
        "@sveltejs/adapter-node": "^1.0.0-next.83",
}
  1. Edit your svelte.config.js file and add your nodejs adapter
import adapter from '@sveltejs/adapter-node';
import preprocess from 'svelte-preprocess';
/** @type {import('@sveltejs/kit').Config} */
const config = {
    // Consult https://github.com/sveltejs/svelte-preprocess
    // for more information about preprocessors
    preprocess: [
        preprocess({
            postcss: true
        })
    ],
   kit: {
        adapter: adapter()
    }
};
export default config;
  1. Run and check everything and check everything is ok .
npm run dev
  • run your test stage before running in the production
  1. Then build your project
npm run build
  • if everything is ok and all the test has ran successfully then you should move on to the deployment stage .

There is many ways to build a docker container

As till now nodejs 16 is long term support version of nodejs. That’s why we are using nodejs 16 docker image . It’s always recommended to use the image of long term support version of of nodejs.

1 Option. Use nodeJS Latest image

Here we are building a multistage docker image . first building and installing and copying the project inside docker container than create another image move your project to that docker container

docker image for multi stage build

FROM node:latest as build
ENV NODE_ENV=production
WORKDIR /app
COPY package.json ./
COPY package-lock.json ./
RUN npm install
COPY . ./
RUN npm run build
FROM node:latest
WORKDIR /app
COPY --from=build /app .
ENV HOST=0.0.0.0
EXPOSE 4173
CMD ["npm","run", "preview","--", "--host", "0.0.0.0"]

2 Option. Use Distroless Nodejs image

“Distroless” images contain only your application and its runtime dependencies. They do not contain package managers, shells or any other programs you would expect to find in a standard Linux distribution.

Why should I use distroless images?

Restricting what’s in your runtime container to precisely what’s necessary for your app is a best practice employed by Google and other tech giants that have used containers in production for many years. It improves the signal to noise of scanners (e.g. CVE) and reduces the burden of establishing provenance to just what you need.

Distroless images are very small. The smallest distroless image, gcr.io/distroless/static-debian11, is around 2 MiB. That's about 50% of the size of alpine (~5 MiB), and less than 2% of the size of debian (124 MiB).

FROM node:19 as build
ENV NODE_ENV=production
WORKDIR /app
COPY package.json ./
COPY package-lock.json ./
RUN npm install
COPY . ./
RUN npm run build
FROM gcr.io/distroless/nodejs:16
WORKDIR /app
COPY --from=build /app .

ENV HOST=0.0.0.0
EXPOSE 4173
CMD ["npm","run", "preview","--", "--host", "0.0.0.0"]

3 Option. Use node:16 Alpine image

Alpine is the base image which is based on Alpine Linux, a very compact Linux distribution. So, node:12.2.0-alpine is a Alpine Linux image with node 12.2.0 installed.

For the latest Alpine based image you can simply do node:alpine. If you want latest but not specifically Alpine you can do node:latest, that image will be based on stretch which is a Debian distribution.

FROM node:19 as build
ENV NODE_ENV=production
WORKDIR /app
COPY package.json ./
COPY package-lock.json ./
RUN npm install
COPY . ./
RUN npm run build
FROM node:19-alpine3.16
WORKDIR /app
COPY --from=build /app .
ENV HOST=0.0.0.0
EXPOSE 4173
CMD ["npm","run", "preview","--", "--host", "0.0.0.0"]

If you are concerned about the image size of docker than let me tell you . First option , using node:16 image in the second stage will give the largest imgae size . Although Distroless image should give you the smallest image but in my experience i found node:16-alpine gives the smallest image .

Image Size -> 1.option(node:16)>2.option(distroless/nodejs:16)>3.option(node:16-alpine)

Build docker image based on your requirement