DEV Community

Abhishek Gupta
Abhishek Gupta

Posted on Edited on

Docker: From Zero to Production (Complete Guide for Developers)

๐Ÿšง Problem Before Docker

As a developer, youโ€™ve probably faced this:

โŒ โ€œIt works on my machine, but not on yours.โ€

Your code runs perfectly on your system but fails on someone elseโ€™s.
It works on Windows but breaks on Linux.
It works locally but crashes in production.

๐Ÿ‘‰ The problem is NOT your code
๐Ÿ‘‰ The problem is the environment


๐ŸŽฏ Key Problems Without Docker

1. Environment Inconsistency

  • Different OS (Windows, Linux, macOS)
  • Different Node versions
  • Different configs

๐Ÿ‘‰ Same code โ†’ different results


2. โ€œIt Works on My Machineโ€

  • Developer: โœ… works
  • Tester: โŒ broken

๐Ÿ‘‰ Root cause: environment mismatch


3. OS Differences

  • File paths behave differently
  • System libraries vary

4. Dependency Conflicts

  • Different package versions
  • Missing libraries

๐Ÿ‘‰ Result:

  • Crashes
  • Bugs
  • Debug hell

5. Difficult Setup

To run a project:

  • Install Node
  • Install MongoDB
  • Install RabbitMQ
  • Setup env variables

๐Ÿ‘‰ Takes hours ๐Ÿ˜“


6. Dev vs Production Mismatch

  • Dev โ†’ Windows
  • Prod โ†’ Linux

๐Ÿ‘‰ Works locally โ†’ fails in production


7. Unreliable Testing

  • Bugs cannot be reproduced

8. No Standardization

  • Every developer = different setup

9. Hidden Dependencies

  • System-level libraries missing

10. Manual Setup = Error-Prone

  • Time wasted
  • Mistakes everywhere

๐Ÿข Real Company Scenario

Imagine a team of 100+ developers:

  • Different OS
  • Different setups
  • Different tool versions

What happens?

  • โŒ Bugs cannot be reproduced
  • โŒ Developers blame testers
  • โŒ Onboarding takes days
  • โŒ Releases get delayed

๐Ÿ“ฆ What is Docker?

Docker is a tool that packages an application along with all its dependencies (like code, runtime, and libraries) into a container, so it can run consistently on any system.


๐Ÿง  Simple Understanding

๐Ÿ‘‰ Docker creates a container (a box)
๐Ÿ‘‰ This box has everything your app needs

๐Ÿ‘‰ So your app:

  • Runs anywhere
  • Gives same result
  • Has no setup issues

Docker is a tool used to package and run applications in containers, ensuring they work the same across different environments.

> Docker = Package app + run anywhere without issues ๐Ÿš€
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ What Docker Does

๐Ÿ‘‰ Docker solves the problem by:

โ€œpackaging your application along with everything that is needed needs to run your application โ€”such as code, runtime, libraries, and configurationsโ€”into a container, ensuring it runs consistently across all environments.โ€


๐Ÿ“ฆ What Goes Inside This Box?

Docker bundles:

๐Ÿ”น 1. Your Code

๐Ÿ‘‰ Your application files

๐Ÿ”น 2. Runtime

๐Ÿ‘‰ Software needed to run your code
Examples:

  • Node.js
  • Python

๐Ÿ”น 3. Libraries (Dependencies)

๐Ÿ‘‰ All required packages
Examples:

  • npm install packages
  • pip install packages

๐Ÿ”น 4. Configuration

๐Ÿ‘‰ Settings your app needs

  • Environment variables
  • Config files

๐ŸŒ Why It Works Everywhere

๐Ÿ‘‰ Because Docker gives:

Same environment on every system

๐Ÿง  Meaning:

  • No need to install Node
  • No need to install libraries
  • No system differences

๐Ÿ‘‰ Everything is already inside the container

๐Ÿ” Same Result Everywhere

๐Ÿ‘‰ You run the container on:

  • Your laptop
  • Friendโ€™s laptop
  • Server

๐Ÿ‘‰ Result will be:

โœ… SAME every time

๐ŸŽฏ Goal of Docker

โœ… Build once, run anywhere

๐Ÿ’ก Birth of Docker

Docker was created to solve environment problems, not coding problems.

๐Ÿ‘จโ€๐Ÿ’ป Creator

  • Solomon Hykes
  • Company: dotCloud
  • Year: 2013

๐Ÿ“ฆ What is a Container?

A container is a running instance of an application, which means your app is actively executing inside an isolated environment with everything it needs to work.

๐Ÿ‘‰ A container is:

> โ€œYour app + all dependencies + runtime in one packageโ€
Enter fullscreen mode Exit fullscreen mode

Like a bag ๐ŸŽ’:

  • Code
  • Node.js
  • Libraries
  • Config

๐Ÿง  What does โ€œRunning Instanceโ€ mean?

๐Ÿ‘‰ Running instance = Your app is currently active and working


๐Ÿ” Break it down:

  • Instance = A copy of something

* Running = Currently working / executing

Hereโ€™s a clear, detailed, and well-structured explanation you can use ๐Ÿ‘‡


โš–๏ธ Docker vs Virtual Machine

๐Ÿ‘‰ In a Virtual Machine (VM):

  • Each VM runs a full operating system
  • It includes its own:

    • OS (Windows/Linux)
    • Kernel
    • Libraries
  • So it becomes heavy and slow


๐Ÿ‘‰ In Docker (Containers):

  • Containers do not run a full OS
  • They share the host systemโ€™s OS kernel
  • Only include:

    • App
    • Runtime
    • Libraries

๐Ÿ‘‰ So they are lightweight and fast

๐Ÿ“Š Comparison Table

Feature Virtual Machine (VM) ๐Ÿ–ฅ๏ธ Docker (Container) ๐Ÿ“ฆ
OS Full OS (separate OS for each VM) Shared OS (uses host OS)
Size Heavy (GBs) Lightweight (MBs)
Speed Slow startup Fast startup
Resource Use High (more RAM/CPU) Low (efficient use)
Boot Time Minutes Seconds
Isolation Strong (hardware level) Process-level isolation

๐Ÿ” Deep Understanding (Simple Words)

๐Ÿ–ฅ๏ธ Virtual Machine

๐Ÿ‘‰ Think like:

โ€œRunning a computer inside another computerโ€

  • Needs full OS
  • Takes more memory
  • Slower

๐Ÿ“ฆ Docker

๐Ÿ‘‰ Think like:

โ€œRunning apps in separate boxes on same systemโ€

  • No full OS needed
  • Shares system
  • Faster and efficient

๐Ÿ” Real-Life Analogy

VM ๐Ÿ–ฅ๏ธ Docker ๐Ÿ“ฆ
Full house ๐Ÿ  Rooms in a house ๐Ÿข

๐Ÿ‘‰ VM = Separate house (expensive)
๐Ÿ‘‰ Docker = Rooms in same house (efficient)

In a virtual machine, each instance runs a complete operating system, making it heavier and slower, whereas Docker containers share the host OS kernel, making them lightweight, faster, and more efficient.


โšก One-Line Revision

VM = Full OS (heavy)
Docker = Shared OS (light & fast) ๐Ÿš€


๐Ÿง  Core Docker Concepts

๐Ÿงญ 1. Full Flow

Dockerfile โ†’ Image โ†’ Container โ†’ Docker Compose
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ This is how Docker works step-by-step


๐Ÿ”น 2. Dockerfile ๐Ÿงพ (Starting Point)


๐Ÿ“– Definition

Dockerfile is a set of instructions used to build a Docker image


๐Ÿง  Simple Meaning

๐Ÿ‘‰ Dockerfile = Recipe to prepare your app


๐Ÿงพ Example

FROM node:18
WORKDIR /app
COPY . .
RUN npm install
CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Important Instructions

Instruction Purpose
FROM Base image
WORKDIR Set working folder
COPY Copy files
RUN Install dependencies
CMD Start application

๐Ÿงช Exam Points

  • Used to build image
  • Step-by-step instructions
  • Each line creates a layer

๐ŸŽฏ Interview Answer

Dockerfile is a script that defines how to build a Docker image.


๐Ÿ”น 3. Image ๐Ÿงฑ (Blueprint)


๐Ÿ“– Definition

An Image is a read-only blueprint of your application


๐Ÿง  Simple Meaning

๐Ÿ‘‰ Image = App setup (not running)


๐Ÿ“ฆ Contains

  • Code
  • Runtime
  • Libraries
  • Config

โš™๏ธ Command

docker build -t my-app .
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Key Points

  • Immutable
  • Reusable
  • Portable
  • Layer-based

๐Ÿงช Exam Points

  • Static
  • Built from Dockerfile
  • Used to create containers

๐Ÿ”น 4. Container ๐Ÿ“ฆ (Running Instance)


๐Ÿ“– Definition

A container is a running instance of an image, meaning an active version of your app.


๐Ÿง  Simple Meaning

๐Ÿ‘‰ Container = App running live


๐Ÿ“ฆ Contains

  • Code
  • Runtime
  • Libraries
  • Config

โš™๏ธ Lifecycle

Create โ†’ Start โ†’ Running โ†’ Stop โ†’ Remove
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ง Commands

๐Ÿงญ 1. Basic Container Commands

Command Description When to Use
docker run <image> Create + start a container First time running an app
docker create <image> Create container only (not start) When you want to configure before starting
docker start <id> Start a stopped container Restart existing container
docker stop <id> Stop a running container Normal shutdown
docker restart <id> Restart container Apply quick changes
docker kill <id> Force stop container When container is stuck

๐Ÿ“Š 2. Viewing Containers

Command Description When to Use
docker ps Show running containers Check active containers
docker ps -a Show all containers See stopped + running
docker inspect <id> Detailed info about container Debugging / deep info
docker stats Show resource usage (CPU, RAM) Monitor performance

๐Ÿงน 3. Remove / Cleanup Commands

Command Description When to Use
docker rm <id> Remove a container Delete unused container
docker rm -f <id> Force remove container Remove even if running
docker container prune Remove all stopped containers Clean up system

๐Ÿ“„ 4. Logs & Debugging

Command Description When to Use
docker logs <id> Show logs of container Check errors
docker logs -f <id> Live logs (real-time) Monitor app
docker exec -it <id> bash Access container terminal Debug inside container

โš™๏ธ 5. Advanced Useful Commands

Command Description When to Use
docker attach <id> Attach to running container Interact with container
docker top <id> Show running processes Check internal processes
docker rename <old> <new> Rename container Organize containers
docker pause <id> Pause container Temporarily freeze
docker unpause <id> Resume container Continue paused container

๐ŸŽฏ Most Important Commands (Must Know)

docker run my-app
docker ps
docker stop <id>
docker rm <id>
docker logs <id>
docker exec -it <id> bash
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Lifecycle Mapping

Create โ†’ Start โ†’ Running โ†’ Stop โ†’ Remove
   |        |        |        |        |
 create   run     ps/logs   stop     rm
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Key Points

  • Lightweight
  • Fast
  • Isolated
  • Temporary

๐Ÿ”น 5. Docker Compose โš™๏ธ (Multi-Container)


๐Ÿ“– Definition

Docker Compose is a tool to run multiple containers together


๐Ÿง  Simple Meaning

๐Ÿ‘‰ Used when your app has:

  • Backend
  • Database
  • Cache

๐Ÿ‘‰ Run all with one command


๐Ÿงพ Example

services:
  app:
    build: .
    container_name: commdesk-app
    ports:
      - "5000:5000"
    depends_on:
      mongo:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
    env_file:
      - .env.docker
    restart: unless-stopped
    networks:
      - commdesk-net

  mongo:
    image: mongodb/mongodb-community-server:7.0-ubuntu2204
    container_name: commdesk-mongo
    ports:
      - "27017:27017"
    volumes:
      - mongo-data:/data/db
    restart: unless-stopped
    networks:
      - commdesk-net
    healthcheck:
      test: ["CMD", "mongosh", "--quiet", "--eval", "db.runCommand({ ping: 1 })"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 20s

  rabbitmq:
    image: rabbitmq:3-management
    container_name: commdesk-rabbitmq
    ports:
        - "5675:5672"
        - "15680:15672"
    environment:
      RABBITMQ_DEFAULT_USER: admin
      RABBITMQ_DEFAULT_PASS: admin
    restart: unless-stopped
    networks:
      - commdesk-net
    healthcheck:
      test: ["CMD", "rabbitmq-diagnostics", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 20s

volumes:
  mongo-data:

networks:
  commdesk-net:
    driver: bridge
Enter fullscreen mode Exit fullscreen mode

โš™๏ธ Docker Compose Commands (Complete Table)

Title Command Description When to Use
โ–ถ๏ธ Start Services docker-compose up Builds (if needed) and starts all services First time run or start project
โ–ถ๏ธ Start in Background docker-compose up -d Runs services in detached mode (background) Run app without blocking terminal
๐Ÿ”„ Rebuild & Start docker-compose up --build Rebuilds images before starting After code or Dockerfile changes
๐Ÿ›‘ Stop Services docker-compose stop Stops running containers (does not remove) Temporarily stop app
โŒ Stop & Remove docker-compose down Stops and removes containers, networks Clean shutdown / reset
๐Ÿ” Restart Services docker-compose restart Restarts all containers Apply changes quickly
๐Ÿ“„ View Logs docker-compose logs Shows logs from all services Debug errors
๐Ÿ“„ Live Logs docker-compose logs -f Streams logs in real-time Monitor running app
๐Ÿ“ฆ List Containers docker-compose ps Shows running services/containers Check status
๐Ÿ”ง Execute Command docker-compose exec <service> <cmd> Run command inside container Debug / run commands inside
๐Ÿ”จ Build Images docker-compose build Builds images for services Before running containers
โฌ‡๏ธ Pull Images docker-compose pull Downloads images from registry Get latest images
โฌ†๏ธ Push Images docker-compose push Uploads images to registry Share images
๐Ÿ“ View Config docker-compose config Shows final merged config Validate YAML
๐Ÿ“Š Top Processes docker-compose top Shows running processes Debug performance
๐Ÿงน Remove Volumes docker-compose down -v Removes volumes with containers Full cleanup
๐Ÿ”„ Scale Services docker-compose up --scale app=3 Runs multiple instances of a service Load testing / scaling
๐Ÿ“› Stop Specific Service docker-compose stop <service> Stops one service only Control specific container
โ–ถ๏ธ Start Specific Service docker-compose start <service> Starts one stopped service Restart specific service

๐ŸŽฏ Most Used Commands

docker-compose up -d
docker-compose down
docker-compose logs -f
docker-compose ps
docker-compose exec app bash
Enter fullscreen mode Exit fullscreen mode

โšก Quick Understanding

  • up โ†’ Start everything
  • down โ†’ Stop & remove everything
  • logs โ†’ Debug
  • exec โ†’ Go inside container
  • build โ†’ Create images

๐Ÿ”น 6. .dockerignore ๐Ÿšซ (Very Important)


๐Ÿ“– Definition

.dockerignore is a file used to exclude unnecessary files from Docker image build

๐Ÿง  Simple Meaning

๐Ÿ‘‰ Prevents unwanted files from going inside image


๐Ÿงพ Example

node_modules
.git
.env
logs
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Why Use It?

  • Reduce image size
  • Faster build
  • Improve security
  • Works like .gitignore
  • Improves performance
  • Avoids unnecessary files

๐Ÿ”— 8. Relationship (Best Explanation)

project/
โ”œโ”€โ”€ src/
โ”œโ”€โ”€ dist/
โ”œโ”€โ”€ Dockerfile
โ”œโ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ .dockerignore
โ”œโ”€โ”€ .env
โ”œโ”€โ”€ .env.docker
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ pnpm-lock.yaml
Enter fullscreen mode Exit fullscreen mode

๐Ÿงญ 1. Full Flow (Simple View)

.dockerignore โ†’ Dockerfile โ†’ Image โ†’ Container โ†’ Docker Compose
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ This is the complete journey of your app in Docker


๐Ÿ”น 2. Step-by-Step (Clear Understanding)


๐Ÿงพ 1. Dockerfile (Start)

# -------- BUILD STAGE --------
FROM node:20-alpine AS builder

WORKDIR /app

RUN npm install -g pnpm

COPY package.json pnpm-lock.yaml ./

RUN pnpm install --frozen-lockfile

COPY . .

RUN pnpm build


# -------- PRODUCTION STAGE --------
FROM node:20-alpine

WORKDIR /app

RUN npm install -g pnpm

COPY package.json pnpm-lock.yaml ./

RUN pnpm install --prod --frozen-lockfile

COPY --from=builder /app/dist ./dist

EXPOSE 5000

CMD ["node", "dist/server.js"]
Enter fullscreen mode Exit fullscreen mode

Dockerfile = Instructions to build your app

๐Ÿ‘‰ You define:

  • Base environment (Node, Python, etc.)
  • Install dependencies
  • Copy code
  • How to start the app

๐Ÿšซ 2. .dockerignore (Before Build)

node_modules
dist
.git
.env
.env.*
Dockerfile
docker-compose.yml
Enter fullscreen mode Exit fullscreen mode

.dockerignore = Ignore unnecessary files

๐Ÿ‘‰ Docker skips files like:

  • node_modules
  • .git
  • .env

๐ŸŽฏ Why?

  • Smaller image
  • Faster build
  • Better security

๐Ÿงฑ 3. Image (Build Stage)

Image = Ready setup of your app (not running)

๐Ÿ‘‰ Built using:

docker build -t my-app .
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Contains:

  • Code
  • Runtime
  • Libraries
  • Config

๐Ÿ“ฆ 4. Container (Run Stage)

Container = Running instance of image

๐Ÿ‘‰ Run using:

docker run my-app
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Now your app is:

  • Live
  • Working
  • Handling requests

โš™๏ธ 5. Docker Compose (Multi-Container)

Docker Compose = Run multiple containers together

๐Ÿ‘‰ Used when app has:

  • Backend
  • Database
  • Cache

๐Ÿ‘‰ Run everything:

docker-compose up
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”— 3. How They Connect (Core Logic)


๐Ÿ”„ Flow Logic

  • Dockerfile โ†’ defines setup
  • Image โ†’ stores setup
  • Container โ†’ runs setup
  • Docker Compose โ†’ manages multiple containers
  • .dockerignore โ†’ optimizes build

๐Ÿ” 4. Real-Life Analogy (Best)

Concept Example
Dockerfile Recipe ๐Ÿ“–
.dockerignore Remove bad items โŒ
Image Prepared ingredients ๐Ÿฅ—
Container Cooking food ๐Ÿณ
Docker Compose Full meal ๐Ÿฝ๏ธ

๐ŸŽฏ 5. Interview Answer (Perfect)

Dockerfile defines how to build an image, the image is a blueprint of the application, and when executed it creates a container which runs the application. Docker Compose is used to manage multiple containers, and .dockerignore is used to exclude unnecessary files during the build process.


โšก 6. Quick Revision

๐Ÿ‘‰ Dockerfile โ†’ Build
๐Ÿ‘‰ Image โ†’ Setup
๐Ÿ‘‰ Container โ†’ Run

๐Ÿ‘‰ Docker Compose โ†’ Multi-container
๐Ÿ‘‰ .dockerignore โ†’ Optimization

> Dockerfile โ†’ Image โ†’ Container
> Docker Compose โ†’ manage multiple containers
> .dockerignore โ†’ optimize build ๐Ÿš€
Enter fullscreen mode Exit fullscreen mode

๐Ÿ—๏ธ Real Project Setup (Node + Mongo + RabbitMQ)

๐Ÿ” Environment Strategy (CRITICAL)

.env (Local)

MONGO_URI=mongodb://localhost:27017/CommDesk
RABBITMQ_URL=amqp://admin:admin@localhost:5672
Enter fullscreen mode Exit fullscreen mode

.env.docker

PORT=5000
NODE_ENV=development

MONGO_URI=mongodb://mongo:27017/CommDesk
RABBITMQ_URL=amqp://admin:admin@rabbitmq:5672

JWT_SECRET=your_JWT

SMTP_USER=
SMTP_PASS=

DOCKER=true
Enter fullscreen mode Exit fullscreen mode

๐Ÿณ Dockerfile (pnpm + Production Ready)


๐Ÿšซ .dockerignore



๐ŸŒ Networking Magic

Inside Docker:

Service Hostname
Mongo mongo
RabbitMQ rabbitmq

๐Ÿ‘‰ Thatโ€™s why:

mongodb://mongo:27017
Enter fullscreen mode Exit fullscreen mode

works


โš™๏ธ Node Config (IMPORTANT)

import { config } from "dotenv";

if (!process.env.DOCKER) {
  config();
}
Enter fullscreen mode Exit fullscreen mode

โ–ถ๏ธ Run Everything

First time

docker compose up --build
Enter fullscreen mode Exit fullscreen mode

Background

docker compose up -d
Enter fullscreen mode Exit fullscreen mode

Stop

docker compose down
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Debugging

docker compose logs -f app
docker exec -it commdesk-app sh
printenv
Enter fullscreen mode Exit fullscreen mode

๐Ÿฐ RabbitMQ UI

http://localhost:15672
admin / admin
Enter fullscreen mode Exit fullscreen mode

โšก Common Errors

โŒ ENOTFOUND mongo

๐Ÿ‘‰ Using docker run

โŒ ECONNREFUSED

๐Ÿ‘‰ Service not ready


๐Ÿ”ฅ Next Level (Monster Upgrades)

  • Hot reload with volumes
  • Nginx reverse proxy
  • CI/CD pipeline
  • Kubernetes
  • Microservices architecture

๐Ÿง  Final Architecture

Client โ†’ Node App (Docker)
           โ†“
    MongoDB + RabbitMQ
Enter fullscreen mode Exit fullscreen mode

๐Ÿ Final Command

docker compose up --build
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Final Takeaway

Docker solves the biggest problem in development:

โŒ Environment inconsistency
โœ… Consistent, portable, reliable apps

Top comments (0)