Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create and Deploy Sveltekit inside a Nodejs Docker Container #287

Open
skshahriarahmedraka opened this issue Jul 26, 2022 · 0 comments
Open

Comments

@skshahriarahmedraka
Copy link
Contributor

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 node:16 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:16 as build


# create a working directory 
WORKDIR /app


# Copy all local files into the image.
COPY . .

# clean install all dependencies
RUN npm ci



### 2nd stage
FROM node:16
# create a working directory 
WORKDIR /app

# copy your project file from build stage 
COPY --from=build /app .

# expose port from public access
EXPOSE 3000

# run index.js file
CMD ["node", "./build/index.js"]

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:18 AS build-env
ENV NODE_ENV=production 

COPY . /app
WORKDIR /app

RUN npm run build  

FROM gcr.io/distroless/nodejs:16
COPY --from=build-env /app /app
WORKDIR /app
EXPOSE 3000
CMD ["node", "./build/index.js"]

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:16 as build

ENV NODE_ENV=production 


WORKDIR /app
COPY . .
COPY package.json package-lock.json svelte.config.js ./
RUN npm run build 


FROM node:16-alpine3.15

WORKDIR /app
COPY --from=build /app .


EXPOSE 3000
CMD ["node", "./build/index.js"]

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant