DEV Community

Akhil Malviya
Akhil Malviya

Posted on • Edited on

🐳 Mastering Docker: A Complete Beginner to Advanced Guide (With Real-World Use Cases)

Docker is one of the most powerful tools in modern software development. Whether you're a beginner trying to containerize your first app or an advanced engineer managing CI/CD pipelines, this guide will walk you through everything you need to know β€” in plain English.

πŸ”° Beginner Level

  1. What is Docker?

Dockerlets you package and run applications in isolated environments called containers.

βœ… Why Use It?
Ensures your app runs the same everywhere β€” no more "it works on my machine."

  1. What is a Docker Image?

A Docker image is a read-only blueprint of your app that includes the code, dependencies, and OS environment.

bash
docker build -t my-app .

3. What is a Docker Container?

A container is a running instance of an image. It’s isolated, fast, and lightweight.

docker run my-app
Enter fullscreen mode Exit fullscreen mode

4. Dockerfile

A Dockerfile is a set of instructions for building Docker images.

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

5. Docker Hub

Docker Hub is like GitHub, but for images. You can push/pull images from it.

docker pull nginx
docker push myuser/my-app
Enter fullscreen mode Exit fullscreen mode

6. Basic Commands

docker build -t app .    # Build image
docker run -p 3000:3000 app  # Run container
docker ps                 # List containers
docker stop <id>          # Stop container
docker exec -it <id> bash # Open shell inside container
Enter fullscreen mode Exit fullscreen mode

7. Port Mapping

Maps container ports to host ports. Example:

docker run -p 8080:3000 app
Enter fullscreen mode Exit fullscreen mode

Now access the app at localhost:8080.


8. Volumes

Used to persist data outside of the container.

docker run -v /data:/app/data my-app
Enter fullscreen mode Exit fullscreen mode

βš™οΈ Intermediate Level

9. Docker Compose

Use docker-compose.yml to define multi-container apps.

version: "3"
services:
  app:
    build: .
    ports:
      - "3000:3000"
  db:
    image: postgres
    volumes:
      - pgdata:/var/lib/postgresql/data
volumes:
  pgdata:
Enter fullscreen mode Exit fullscreen mode
docker-compose up
Enter fullscreen mode Exit fullscreen mode

10. Docker Networks

Enables communication between containers.

docker network create my-net
docker run --network my-net ...
Enter fullscreen mode Exit fullscreen mode

11. Bind Mount vs Volume

  • Bind Mount: Links to local folders (-v ./data:/data)
  • Volume: Managed by Docker (docker volume create)

12. Docker Context

Lets you switch between environments (e.g., local and remote).

docker context use remote
Enter fullscreen mode Exit fullscreen mode

13. Dockerfile Layers

Each instruction (RUN, COPY, etc.) creates a layer.

βœ… Helps with caching and faster rebuilds.


14. .dockerignore

Ignore files from being sent to the Docker daemon.

node_modules
.env
.git
Enter fullscreen mode Exit fullscreen mode

πŸš€ Advanced Level

15. Docker-in-Docker (DinD)

Run Docker inside Docker. Useful in CI/CD tools like GitLab CI.

image: docker:latest
services:
  - docker:dind
Enter fullscreen mode Exit fullscreen mode

⚠️ Security Risk: Grants full control over the Docker host.


16. Docker Socket

Access the host Docker engine inside containers.

-v /var/run/docker.sock:/var/run/docker.sock
Enter fullscreen mode Exit fullscreen mode

17. Multi-stage Builds

# Builder
FROM node:18 as builder
WORKDIR /app
COPY . .
RUN npm run build

# Final
FROM nginx
COPY --from=builder /app/dist /usr/share/nginx/html
Enter fullscreen mode Exit fullscreen mode

βœ… Reduces image size and exposure of build tools.


18. Healthcheck

HEALTHCHECK CMD curl --fail http://localhost:3000 || exit 1
Enter fullscreen mode Exit fullscreen mode

Docker will monitor container health and restart if needed.


19. Entrypoint vs CMD

ENTRYPOINT ["node"]
CMD ["app.js"]
Enter fullscreen mode Exit fullscreen mode
  • ENTRYPOINT: Always runs
  • CMD: Default arguments

20. Docker Orchestration Tools

  • Docker Swarm: Built-in clustering
  • Kubernetes: Industry standard for scaling, self-healing, rolling deployments

🧠 Real-World Scenarios

Use Case Docker Concepts Used
Simple React App Dockerfile, Image, Container, Port Mapping
MERN Stack App Compose, Networks, Volumes
GitLab CI for Deployments Docker-in-Docker, Multi-stage, Context
Production-grade Builds Multi-stage, Healthcheck, Entrypoint

πŸ“Œ Final Tips

  • 🧹 Clean up unused stuff:
  docker system prune -a
Enter fullscreen mode Exit fullscreen mode
  • πŸ“¦ List volumes:
  docker volume ls
Enter fullscreen mode Exit fullscreen mode
  • πŸ” Debug:
  docker logs <container-id>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)