UK based website and application support - contact form.

Blog.

How to Containerize your project with Docker

Cover Image for How to Containerize your project with Docker

Why Containerize?

Containers eliminate "it works on my machine" problem. They are lightweight, isolated environments that can run on any machine. They are also easy to scale and deploy. Containers ensure that applications run the same way in development, testing, and production environments.

Containerize your project

To create a container we will use docker. Docker is a open-source platform. It allows developers to package applications and their dependencies into containers. Containers are lightweight, isolated environments that can run on any machine.

Dockerfile

Here's an example of how to containerize a svelte project. In root directory create new file Dockerfile.

# Builder stage
# Build an image
FROM --platform=linux/amd64 node:18-alpine AS builder
# GitHub authentication
ARG GITHUB_PAT_TOKEN
ENV GITHUB_PAT_TOKEN=$GITHUB_PAT_TOKEN
# Expose port
ARG EXP_PORT=3000
ENV EXP_PORT=${EXP_PORT}
# Set working directory
WORKDIR /app
# Additional tasks
COPY package*.json .
COPY .npmrc .
# Install dependencies
RUN npm ci
# Copy application files
COPY . .
# Build application
RUN npm run build
# Remove dev dependencies
RUN npm prune --production

# Runner stage
FROM node:18-alpine as run
WORKDIR /app
COPY --from=builder /app/build build/
COPY --from=builder /app/node_modules node_modules/
COPY package.json .
EXPOSE $EXP_PORT
ENV NODE_ENV=production
CMD [ "node", "build" ]

First stage is called builder. It is used to build the application.

Second stage is called runner. It is used to run the application.

I opted for node:18-alpine as the base image because it's a lightweight option built on Alpine Linux. This makes it an excellent choice for production applications, as it is significantly smaller than full Linux distributions, helping to keep the container size minimal.

I also pass GITHUB_PAT_TOKEN enviroinment variable to the builder stage. This is used to authenticate with GitHub when installing private packages.

Run docker build

When you finish customizing the Dockerfile, you can build the Docker image using the docker build command:

docker build --build-arg GITHUB_PAT_TOKEN=<ghp_TOKEN> -t <CONTAINER_NAME> .```

Once docker build command is finished, you can run the container using the docker run command:

docker run -d -p 8080:3000 <CONTAINER_NAME>

To upload and share docker image with others you can push it to docker hub using docker push command.

docker push <DOCKER_USERNAME>/<CONTAINER_NAME>:latest

Conclusion

Containerization is a game-changer because it makes sure your app runs the same way on your laptop as it does on the server, so you avoid those frustrating “it works on my machine” moments.

It also allows you to run several apps side by side without them stepping on each other’s toes, which is super handy when you're juggling different projects. Plus, it speeds up the whole process of getting your app out there, letting you focus more on building features rather than dealing with setup headaches.

← Back to homepage

Looking for website and application support in the Midlands? I specialize in providing comprehensive website maintenance and support services.
Whether you need hosting solutions, web application development, security updates, or assistance with in-house or remote teams, I can help.

My expertise covers React, Next.js, JavaScript, TypeScript, Web Components, Lit, Stencil.js, Node.js, RESTful APIs, Docker, Kubernetes, and Amazon Web Services.

Get in touch!