🎬 "Ready to become a Docker chef? In this episode, we’ll write your first Dockerfile — the recipe that creates your own custom container. Simple, step-by-step, with a real app. Let’s cook! 👨🍳🍱"
🍳 What is a Dockerfile?
A Dockerfile is like a recipe card for Docker. It tells Docker exactly how to build an image from scratch.
Just like:
- A cake recipe includes ingredients and steps
- A Dockerfile includes base image, dependencies, code, and commands
Once Docker reads the Dockerfile, it builds a Docker image, which you can then run as a container.
👨💻 Let's Build Your First Dockerfile
We’ll dockerize a basic Node.js app.
📁 Project Structure:
my-node-app/ ├── Dockerfile ├── package.json └── index.js 🧠 index.js (your app):
const http = require('http'); const server = http.createServer((req, res) => { res.end('Hello from Docker!'); }); server.listen(3000, () => console.log('Server running on port 3000')); 📦 package.json:
{ "name": "my-node-app", "version": "1.0.0", "main": "index.js", "scripts": { "start": "node index.js" } } 🛠️ Writing the Dockerfile
Now create a file called Dockerfile (no extension!) and add this:
# Base image FROM node:18 # Working directory WORKDIR /app # Copy files COPY package*.json ./ # Install dependencies RUN npm install # Copy app source COPY . . # Expose port EXPOSE 3000 # Start the app CMD ["npm", "start"] 🧪 Build & Run the Docker Image
🔨 Build the image:
docker build -t my-node-app . 🚀 Run the container:
docker run -d -p 3000:3000 my-node-app 🌐 Open your browser:
Visit http://localhost:3000 — you’ll see: Hello from Docker!
🧠 What Did We Just Do?
-
FROM: Sets the base environment (Node.js) -
WORKDIR: The folder where your code lives inside the container -
COPY: Moves your code into the container -
RUN: Installs dependencies -
EXPOSE: Lets Docker know what port the app uses -
CMD: Tells Docker how to start the app
🛡️ Bonus Tips
- Use
.dockerignoreto skipnode_modules,logs, etc. - Always pin your base image (
node:18not justnode) - Keep Dockerfiles short, clean, and readable
🎯 Up Next: Docker CLI Mastery
In Episode 5, we’ll explore:
- Most used Docker commands
- How to manage images and containers
- Tricks to make your Docker life easier
💬 Let’s Build Together
Have you written your first Dockerfile? Did it work? Run into any errors?
Comment below — I’m here to help every beginner succeed 🚀
❤️ If you learned something new today, show some love with a like or drop a comment. Share this episode with your Dev.to fam!
🎬 Next: “Docker CLI Cheat Sheet — 15 Commands You’ll Actually Use”
Top comments (4)
চমৎকারভাবে সাজানো হয়েছে। উদাহরণটা সহজবোধ্য, কিন্তু Dockerfile-এর স্টেপগুলো আরও ভালো লেগেছে COPY, RUN, CMD একদম পরিষ্কারভাবে সাজানো।
একটা ছোট টিপ:
COPY . .যদিnpm installএর আগে দেন, আরpackage-lock.jsonঠিক থাকে, তাহলে ক্যাশিং ভালোভাবে কাজ করবে। না হলে প্রতিবারই সবকিছু রিবিল্ড হবে।এইরকম ক্লিয়ার গাইড আরও দরকার। CLI পার্টের অপেক্ষায় রইলাম।
Thanks a lot for the great tip! 🙌 You’re right — copying package*.json first really helps with caching. CLI part is coming soon, stay tuned!
Great work, Yash!
I’ve checked out Vultr’s docs before, and they’re really good, but your step-by-step guide makes things even clearer and super practical for beginners.
Thanks Bro❤️