Learn Docker : Beginner to Expert !

Table of contents
- What is Docker? (Think of a Portable Toy Box)
- How Does Docker Work? (The Key Pieces)
- Getting Started with Docker on Linux (Ubuntu)
- Production Example 1: Running a Company’s E-Commerce Website
- Production Example 2: Running a Microservices-Based Chat Application
- Tips for Using Docker on Linux
- Why Learn Docker?

What is Docker? (Think of a Portable Toy Box)
Imagine you have a favorite toy set, like a LEGO kit. Itfoo bar To keep things simple, I’ll use Ubuntu, a popular Linux distribution, for all examples, as you requested. You can install Docker on Ubuntu and start using it to manage containers.
Why is Docker Awesome?
Works anywhere: Your container runs the same way on any Linux computer, whether it’s your laptop or a company’s server.
Saves time: You don’t need to install and configure programs on every machine.
Keeps things clean: Containers don’t mess with other programs on your computer.
How Does Docker Work? (The Key Pieces)
Docker has a few main parts that make it work. Here’s what they are in simple terms:
Docker Image: This is like a recipe for your program. It includes the program, its code, and everything it needs to run, like tools and settings.
Docker Container: This is the actual running program, made from the image. You can run multiple containers from one image.
Docker Hub: An online store where you can find ready-made images, like for databases or web servers.
Docker Engine: The software on your Linux computer that creates and runs containers.
Getting Started with Docker on Linux (Ubuntu)
Let’s set up Docker on Ubuntu and try some commands. I’ll explain each command word by word so you understand exactly what’s happening.
Step 1: Install Docker on Ubuntu
First, you need to install Docker. Open a terminal on your Ubuntu computer (you can do this by pressing Ctrl + Alt + T
). Run these commands one by one:
Command:
sudo apt update
sudo: Runs the command as a superuser (like an admin), because installing software needs permission.
apt: Ubuntu’s tool for managing software packages.
update: Refreshes the list of available software so you get the latest versions.
What it does: Makes sure your system knows about the latest software, including Docker.
Command:
sudo apt install
docker.io
sudo: Again, gives admin permission.
apt: The package manager.
install: Tells the system to download and set up a program.
docker.io: The name of the Docker package in Ubuntu.
What it does: Installs Docker on your computer.
Command:
sudo systemctl start docker
sudo: Admin permission.
systemctl: A tool to control services (like Docker) on Linux.
start: Tells the service to begin running.
docker: The name of the Docker service.
What it does: Starts the Docker Engine so you can use Docker.
Command:
sudo systemctl enable docker
sudo: Admin permission.
systemctl: Service control tool.
enable: Makes the service start automatically when your computer boots.
docker: The Docker service.
What it does: Ensures Docker starts every time you turn on your computer.
Command:
sudo usermod -aG docker $USER
sudo: Admin permission.
usermod: A tool to modify user settings.
-aG: Adds the user to a group without removing them from other groups.
docker: The Docker user group, which lets you run Docker commands without sudo.
$USER: Your username (automatically filled in).
What it does: Lets you run Docker commands without typing
sudo
every time. You may need to log out and back in for this to work.
Step 2: Test Docker with a Simple Container
Let’s run a test container to make sure Docker is working.
Command: docker run hello-world
docker: The Docker command-line tool.
run: Tells Docker to create and start a container from an image.
hello-world: The name of a simple test image from Docker Hub.
What it does: Downloads the
hello-world
image from Docker Hub, creates a container, and runs it. You’ll see a message saying “Hello from Docker!” if it works.
Production Example 1: Running a Company’s E-Commerce Website
Big online stores like Amazon or Flipkart use Docker to run their websites. Let’s say a company wants to run a web store using NGINX (a popular web server) on their Linux servers. They need the website to work the same way in testing, staging, and production environments.
How They Use Docker
Create a Dockerfile:
The company writes a file calledDockerfile
to define their web server. Here’s an example:FROM nginx:latest COPY website /usr/share/nginx/html EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
FROM nginx:latest: Uses the latest NGINX image from Docker Hub as the base.
FROM: Specifies the starting image.
nginx: The name of the image.
latest: The most recent version of the image.
What it does: Sets up a container with NGINX already installed.
COPY website /usr/share/nginx/html: Copies the website files (like HTML and images) into the container.
COPY: Moves files from your computer to the container.
website: The folder on your computer with website files.
/usr/share/nginx/html: The folder in the container where NGINX looks for website files.
What it does: Puts the website files where NGINX can serve them.
EXPOSE 80: Tells Docker the container uses port 80 (for web traffic).
EXPOSE: Declares which port the container listens on.
80: The standard port for websites (HTTP).
What it does: Informs Docker that the container serves web pages on port 80.
CMD ["nginx", "-g", "daemon off;"]: Runs NGINX when the container starts.
CMD: Specifies the command to run when the container starts.
["nginx", "-g", "daemon off;"]: Runs NGINX in the foreground (required for containers).
What it does: Starts the NGINX web server.
Build the Image:
Command:docker build -t ecommerce-web .
docker: The Docker tool.
build: Creates an image from a Dockerfile.
-t ecommerce-web: Names the image
ecommerce-web
.-t: Stands for “tag,” used to name the image.
ecommerce-web: The custom name for the image.
.: Tells Docker to use the Dockerfile in the current folder.
What it does: Builds a Docker image with the company’s website.
Run the Container:
Command:docker run -d -p 8080:80 ecommerce-web
docker: The Docker tool.
run: Starts a container from an image.
-d: Runs the container in the background (detached mode).
-p 8080:80: Maps port 8080 on your computer to port 80 in the container.
-p: Stands for “publish,” used to map ports.
8080: The port on your computer.
80: The port inside the container.
ecommerce-web: The nameburst: The name of the image to run.
What it does: Starts the container, and you can access the website by opening
http://localhost:8080
in a browser.
Why This is Production-Level
Companies use this setup because:
The same container runs in development, testing, and production, ensuring no surprises.
They can run multiple containers (e.g., for load balancing) to handle millions of users.
Containers are lightweight, so they save server resources compared to full virtual machines.
Production Example 2: Running a Microservices-Based Chat Application
Big chat apps like WhatsApp or Slack use microservices, where different parts of the app (like messaging, user management, and notifications) run in separate containers. Let’s say a company builds a chat app with a Node.js backend and a MongoDB database.
How They Use Docker
Create a Docker Compose File:
Docker Compose lets you run multiple containers together. They create a file calleddocker-compose.yml
:version: "3.8" services: backend: build: ./backend ports: - "3000:3000" mongodb: image: mongo:latest volumes: - mongodb_data:/data/db volumes: mongodb_data:
version: "3.8": Specifies the Docker Compose file format version.
services: Lists the containers to run.
backend: The name of the Node.js service.
build: ./backend: Builds the backend image from a Dockerfile in the
backend
folder.ports: - "3000:3000": Maps port 3000 on the computer to port 3000 in the container.
mongodb: The name of the MongoDB service.
image: mongo:latest: Uses the latest MongoDB image from Docker Hub.
volumes: - mongodb_data:/data/db: Stores MongoDB data in a persistent volume.
volumes: Defines named volumes for data storage.
- mongodb_data: A named volume to keep MongoDB data even if the container stops.
What it does: Defines two services (backend and database) that work together.
Backend Dockerfile:
In thebackend
folder, they create aDockerfile
:FROM node:18 WORKDIR /app COPY . . RUN npm install CMD ["npm", "start"]
FROM node:18: Uses the Node.js version 18 image.
WORKDIR /app: Sets the working directory in the container to
/app
.WORKDIR: Creates and sets the directory for commands.
/app: The directory path in the container.
What it does: Makes
/app
the default folder for the next commands.
COPY . .: Copies all files from the
backend
folder to/app
in the container.RUN npm install: Installs Node.js dependencies.
RUN: Executes a command during image building.
npm install: Installs packages listed in
package.json
.What it does: Sets up the backend’s dependencies.
CMD ["npm", "start"]: Runs the backend server.
What it does: Starts the Node.js app when the container runs.
Run the Application:
Command:docker-compose up
docker-compose: The Docker Compose tool.
up: Starts all services defined in
docker-compose.yml
.What it does: Builds and runs the backend and MongoDB containers, connecting them so the chat app works.
Why This is Production-Level
Scalability: The company can run multiple backend containers to handle thousands of chats at once.
Reliability: The MongoDB volume ensures no data is lost if a container restarts.
Modularity: Each microservice (backend, database) is independent, so updates to one don’t break the others.
Tips for Using Docker on Linux
Start with simple images like
hello-world
ornginx
to practice.Use Docker Hub to find pre-built images for almost anything.
Keep containers small and focused on one task (e.g., one container for the web server, another for the database).
Check logs if something goes wrong:
docker logs <container_name>
shows what’s happening inside a container.docker: The Docker tool.
logs: Shows the container’s output.
: The name or ID of the container (find it with
docker ps
).What it does: Displays error messages or logs to debug issues.
Why Learn Docker?
Docker is used by companies worldwide to build reliable, scalable apps. As a student, learning Docker on Linux is like learning a superpower for tech. You can build real-world projects, like websites or apps, and impress your friends or future employers. Plus, it’s fun to see your programs run in containers!
Give these commands a try, and let me know how it goes. Happy Dockering!
Subscribe to my newsletter
Read articles from Zasim directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Zasim
Zasim
DevOps Engineer