How To Create A Docker Image For Your Node Application (e.g. SvelteKit)
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.
- Introduction
- Create the sample application
- Create the Dockerfile
- Build and check the image
- Conclusion
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.
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:
- Run:
npm create svelte@latest sample
- Select the SvelteKit demo app
- Select Typescript
- Select the utilities that you want
- Open VS Code in the directory using:
code sample
- Create a git repository:
git init
- Install the required packages:
npm install
- Install the node adapter:
npm i -D @sveltejs/adapter-node
- Configure it in the
svelte.config.js
(replace adapter-auto with adapter-node in the import):import adapter from '@sveltejs/adapter-node';
- Create a
.dockerignore
file and add node_modules inside - Launch the app:
npm run dev
- 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.
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.
[convertkit form=2303042]