🚀 Kubernetes Series – Day 2: How to Dockerize Your Project


“Containers are only useful when you know how to package your application into one. Let’s Dockerize your app today.”
🧠 What Does It Mean to Dockerize a Project?
Dockerizing means packaging your application into a Docker container, making it portable, isolated, and production-ready. This allows you to run it on any system with Docker installed — be it local, staging, or production — without "works on my machine" issues.
In this post, we’ll walk through the process of Dockerizing a sample project, using a Node.js app as an example. The steps are easily adaptable to Python, Java, Go, or any other language.
🛠 Step-by-Step Guide to Dockerizing Your Project
🔹 Step 1: Set Up Your Project Structure
Let’s say you have a simple Node.js app like this:
my-app/
│
├── app.js
├── package.json
├── package-lock.json
└── ...
Example app.js
:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello from Dockerized App!');
});
app.listen(3000, () => {
console.log('App listening on port 3000');
});
Example package.json
:
{
"name": "my-app",
"version": "1.0.0",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "^4.18.0"
}
}
🔹 Step 2: Create a Dockerfile
A Dockerfile contains instructions to build a Docker image for your app.
Sample Dockerfile for Node.js:
# Base image
FROM node:18-alpine
# Set working directory
WORKDIR /app
# Copy package files and install dependencies
COPY package*.json ./
RUN npm install
# Copy the rest of the application
COPY . .
# Expose port and define command
EXPOSE 3000
CMD ["npm", "start"]
🔁 For Python/Java/Go:
Change
FROM
to the appropriate base image (python:3.11
,openjdk:17
, etc.)Modify the
RUN
,CMD
, and dependency installation steps accordingly.
🔹 Step 3: Add a .dockerignore
File
This is like .gitignore
- it prevents unnecessary files from being copied into the Docker image.
Example .dockerignore
:
node_modules
npm-debug.log
.env
.git
✅ This helps:
Reduce image size
Improve build performance
Avoid exposing sensitive files
🔹 Step 4: Build the Docker Image
Open your terminal in the root of your project and run:
docker build -t my-app .
📝 Explanation:
-t my-app
: Tags the image asmy-app
.
: Refers to the current directory (where Dockerfile is located)
🔹 Step 5: Run the Docker Container
Now that your image is ready, run it as a container:
docker run -d -p 3000:3000 my-app
-d
: Detached mode (runs in the background)-p 3000:3000
: Maps port 3000 of your local machine to the container
🧪 Open http://localhost:3000
in your browser and you’ll see your app running inside a Docker container!
🧰 Bonus: Useful Docker Commands
Action | Command |
List running containers | docker ps |
Stop a container | docker stop <container_id> |
Remove a container | docker rm <container_id> |
View all images | docker images |
Remove an image | docker rmi <image_id> |
💡 Pro Tips
Keep your images lean — use Alpine-based images when possible
Use multi-stage builds for large apps with build steps (e.g., React, Angular)
Tag your images properly in real-world CI/CD setups (
my-app:latest
,my-app:v1.2.0
)
By Dockerizing your project, you’ve made it portable, isolated, and production-ready. This forms the foundation for container orchestration with tools like Kubernetes, which we’ll dive into next.
Subscribe to my newsletter
Read articles from Nishank Koul directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
