Forem

Cover image for Docker 101: A Simple & Clear Introduction
Dhvani
Dhvani

Posted on

Docker 101: A Simple & Clear Introduction

Just starting out with docker so thought why not make the doc and share it with everyone who is just starting out. this is my first time writing a post feedback are just welcomed so that i can improve and help you better.

Table of Contents

  1. Why Do We Need Docker? πŸš€
  2. What is Docker?
  3. Why Use Docker?
  4. Docker Image vs Docker Container
  5. How Does Docker Work?
  6. Basic Docker Commands
  7. Push Your Container to Docker Hub
  8. Push Your Container to Docker Hub
  9. Building and Running a Docker Image
  10. Port Mapping in Docker
  11. Docker Compose
  12. Final Thoughts

Why Do We Need Docker? πŸš€

Imagine you built a Node.js app on your computer, and it works perfectly. But when you share it with a friend or deploy it to a server, it doesn’t work because:

  • Your friend doesn’t have Node.js installed.
  • The server has a different Node.js version.
  • Some dependencies are missing.
  • It works on your machine but not elsewhere.

This is called the "It works on my machine" problem πŸ›‘.


What is Docker?

Docker solves this problem by packing everything your app needs (code, dependencies, runtime, OS) into a container.

A container is like a mini-computer that runs the app the same way everywhere, no matter if it’s:

  • βœ… Your computer
  • βœ… A friend’s computer
  • βœ… A server
  • βœ… The cloud

Why Use Docker?

  • βœ… Consistency – Your app works the same everywhere.
  • βœ… No setup needed – No more "install this, install that" nightmares.
  • βœ… Lightweight – Unlike full virtual machines, containers use fewer resources.
  • βœ… Fast Deployment – Just pull and run the container, and your app is ready!
  • βœ… Easy to Share – Send one Docker image, and others can run your app instantly.

Docker Image vs Docker Container

1️⃣ Docker Image = The Recipe πŸ“œ

  • A Docker Image is like a recipe for making pizza.
  • It has all the ingredients and instructions needed to make the pizza.
  • But the recipe alone is not a pizza yet!

πŸ’‘ In Docker terms, an image is a blueprint that tells Docker how to create a running application.

2️⃣ Docker Container = The Actual Pizza πŸ•

  • A Docker Container is like a real, baked pizza made using the recipe.
  • It is an instance of the image, meaning it is running.
  • You can eat the pizza (use the app), modify it, or throw it away.

πŸ’‘ In Docker terms, a container is a running instance of an image.


How Does Docker Work?

  1. You write a Dockerfile (like a recipe for your app).
  2. You build a Docker image (your app + everything it needs).
  3. You run the image inside a container.
  4. The app runs the same everywhereβ€”no missing dependencies, no "works on my machine" issues.

Visual Tip: Imagine this as a cycle: Write β†’ Build β†’ Run β†’ Deploy.


Basic Docker Commands

Command Description
docker images List all images on your system.
docker ps List running containers.
docker ps -a List all containers (running + stopped).
docker start <container_name> Start a stopped container.
docker stop <container_name> Stop a running container.
docker rm <container_name> Remove a container.
docker rmi <image_name> Remove an image.
docker exec -it <container_name> bash Open a terminal inside a running container.
docker build -t my-app . Build a Docker image from a Dockerfile.

Breakdown of Some Parts

  1. docker exec - Runs a command inside an already running container.
  2. -it - Combines two flags:
    • -i (interactive mode) - Keeps the input open so you can interact with the container.
    • -t (TTY mode) - Allocates a terminal, making it look and feel like a regular shell.
  3. docker build - Tells Docker to build a new image.
  4. -t my-app - Tags the image with the name my-app.
  5. . (dot) - Specifies the current directory as the build context.

Push Your Container to Docker Hub

  1. Login to Docker Hub:
   docker login
Enter fullscreen mode Exit fullscreen mode
  1. Build your image:
   docker build -t <docker_hub_username>/<image_name> .
Enter fullscreen mode Exit fullscreen mode
  1. Push the image:
   docker push <docker_hub_username>/<image_name>
Enter fullscreen mode Exit fullscreen mode

Now anyone can pull and use your image:

docker pull <docker_hub_username>/<image_name>
Enter fullscreen mode Exit fullscreen mode

Building and Running a Docker Image

Dockerfile Example 1

FROM ubuntu

RUN apt-get update
RUN apt-get install -y curl
RUN curl -sL https://deb.nodesource.com/setup_18.x | bash -
RUN apt-get upgrade -y
RUN apt-get install -y nodejs


COPY package*.json ./
COPY main.js ./
RUN npm install

ENTRYPOINT [ "node", "main.js" ]
Enter fullscreen mode Exit fullscreen mode

Explanation

  1. FROM ubuntu β†’ Uses Ubuntu as the base image.
  2. RUN commands β†’ Updates the system, installs curl & Node.js.
  3. COPY commands β†’ Copies package.json and main.js into the container.
  4. RUN npm install β†’ Installs dependencies.
  5. ENTRYPOINT β†’ Runs node main.js when the container starts.

Example 2: Improved Version Using the Node Base Image

FROM node

COPY package*.json ./
COPY main.js ./
RUN npm install

ENTRYPOINT [ "node", "main.js" ]
Enter fullscreen mode Exit fullscreen mode

Explanation

  1. FROM node β†’ Uses the official Node image, which comes pre-installed with Node.js.
  2. COPY commands β†’ Copy package.json (or package-lock.json) and main.js into the container.
  3. RUN npm install β†’ Installs your Node.js dependencies.
  4. ENTRYPOINT β†’ Sets the container to run node main.js on startup.

Benefits of the Improved Version:

  • Simpler and More Efficient: Eliminates multiple RUN commands and avoids manual installation of Node.js.
  • Optimized Image Size: The official Node image is optimized for running Node.js applications, resulting in a smaller and more efficient container.
  • Faster Build Times: Fewer layers and commands mean the image builds more quickly.

Build and Run Commands

docker build -t my-app .   # Build the image
docker run -d -p 3000:3000 my-app  # Run the container
Enter fullscreen mode Exit fullscreen mode

Port Mapping in Docker

When you run a Docker container, you may need to expose certain ports to your machine. Use the -p flag:

docker run -p 8080:80 my-container
Enter fullscreen mode Exit fullscreen mode

Breakdown:

  • 8080:80 β†’ Maps port 80 inside the container to 8080 on your machine.
  • You can access the app at http://localhost:8080.

Docker Compose

Why Do We Need Docker Compose?

Docker Compose allows you to run multiple containers together with a single command. Instead of running many docker run commands, you define all services in a docker-compose.yml file.

Example: Running a Node.js App with MongoDB

version: "3"
services:
  app:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - database

  database:
    image: mongo
    ports:
      - "27017:27017"
Enter fullscreen mode Exit fullscreen mode

How It Works

  • Services: Defines two services: app (Node.js) and database (MongoDB).
  • Build & Dependencies: The app service builds from the Dockerfile and depends on database, ensuring MongoDB starts first.

Running with Docker Compose

  1. Start all services in the background:
   docker-compose up -d
Enter fullscreen mode Exit fullscreen mode
  1. Stop and remove all services:
   docker-compose down
Enter fullscreen mode Exit fullscreen mode

Common Docker Compose Commands

  • docker-compose up -d β†’ Start all services in the background.
  • docker-compose down β†’ Stop and remove all containers.
  • docker-compose ps β†’ List running services.
  • docker-compose logs β†’ View logs of all services.

Final Thoughts

πŸš€ Docker makes development and deployment easier, faster, and more consistent.

πŸ’‘ Key Takeaways:

  • Docker Image = Recipe πŸ“œ (Blueprint for an app)
  • Docker Container = Pizza πŸ• (A running instance of the app)
  • Docker Compose helps run multiple services together

Additional Tips

  • Experiment: Don’t be afraid to experiment with small projects. The best way to learn is by doing!
  • Troubleshooting: If you run into issues, check Docker’s official documentation and community forums.
  • Practice Commands: Regularly use the basic commands to build muscle memory.
  • Version Control: Combine Docker with Git to manage your application versions effectively.

Docker 102 Coming Soon

I post different articles on different platforms so must check them out...you won't regret.

Medium
Daily Dev

Top comments (2)

Collapse
 
robin-ivi profile image
Robin Mishra

Even though I'm still new to Docker, your post really made the basics seem approachable. I'm grateful for your clear explanationsβ€”it’s sparked my curiosity to learn more about containerization. Thanks for sharing your insights!

Collapse
 
knight03 profile image
Dhvani

Thank you! Glad that it helped you. but you know the next part in docker is networking and i barely know the raw concepts of networking so before docker 102. i'll learn raw networking concepts and will make every one learn too πŸ˜