How To Create A Docker Image For Your Node Application (e.g. SvelteKit)

2 Comments
Published: 02.07.2023

Do you want to create a Docker image for your Node application? This guide will show you exactly how to do it! We will first create a SvelteKit sample application, create a Docker file for it, then build the image, and finally check if it works.

  1. Introduction
  2. Create the sample application
  3. Create the Dockerfile
  4. Build and check the image
  5. Conclusion
Don’t want to read? Watch the video instead!

Introduction

To create a Docker image for your node application, you first need to create a Dockerfile. A Dockerfile serves as a template for the image and includes all the steps needed to make the application available inside. The image is then used to create a Docker container. The container can then be run on any system, such as your server. In the next step, we will create the sample application we want to dockerize.

Server icon

VPS Hosting Course

Learn everything you need to know about servers and hosting your own applications!

Create the sample application

In this guide, we will use the standard SvelteKit example application as our sample app. We will then run it on the node-adapter to make it executable with node. For that, we will execute the following steps:

  1. Run: npm create svelte@latest sample
  2. Select the SvelteKit demo app
  3. Select Typescript
  4. Select the utilities that you want
  5. Open VS Code in the directory using: code sample
  6. Create a git repository: git init
  7. Install the required packages: npm install
  8. Install the node adapter: npm i -D @sveltejs/adapter-node
  9. Configure it in the svelte.config.js (replace adapter-auto with adapter-node in the import): import adapter from '@sveltejs/adapter-node';
  10. Create a .dockerignore file and add node_modules inside
  11. Launch the app: npm run dev
  12. Check the application in the browser under the provided URL

With that, we have a running node application and can build a Docker image for it. In the next step, we will create the Dockerfile.

Create the Dockerfile

The Dockerfile consists of a two-step process. First, we will build the application in step one and then copy the needed files into the final image in step two. The Dockerfile looks like this:

FROM node:lts-alpine as build
WORKDIR /app
COPY ./package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM node:lts-alpine AS production
COPY --from=build /app/build .
COPY --from=build /app/package.json .
COPY --from=build /app/package-lock.json .
RUN npm ci --omit dev
EXPOSE 3000
CMD ["node", "."]

Build and check the image

To now create a docker image from the Dockerfile, we need to run the following command: docker build . -t sample-image. This creates a Docker image called sample-image and contains the node application. We can then check if everything works correctly by using the image to create a Docker container. For that, we run: docker run -p 3000:3000 –name sample-container sample-image. This creates a container called sample-container based on the sample-image. In addition, it has a port mapping of port 3000 inside the container to port 3000 on the local machine. That way, we can visit the application under: http://localhost:3000.

Need help or want to share feedback? Join my discord community!

Conclusion

With that, we successfully created a Docker image for our SvelteKit node application! I hope this guide was helpful to you, and if you have any questions, feel free to join the discord, leave a comment, or email me!

In the next guide, we will use the Docker image to deploy our application to our server automatically. We will either use GitHub or GitLab.

KOFI Logo

If this guide is helpful to you and you like what I do, please support me with a coffee!

Don’t miss out on any updates or future guides by subscribing to my monthly newsletter.

Discussion (2)

Add Comment

Your email address will not be published. Required fields are marked *