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
- Why Do We Need Docker? π
- What is Docker?
- Why Use Docker?
- Docker Image vs Docker Container
- How Does Docker Work?
- Basic Docker Commands
- Push Your Container to Docker Hub
- Push Your Container to Docker Hub
- Building and Running a Docker Image
- Port Mapping in Docker
- Docker Compose
- 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?
-
You write a
Dockerfile
(like a recipe for your app). - You build a Docker image (your app + everything it needs).
- You run the image inside a container.
- 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
-
docker exec
- Runs a command inside an already running container. -
-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.
-
-
docker build
- Tells Docker to build a new image. -
-t my-app
- Tags the image with the namemy-app
. -
.
(dot) - Specifies the current directory as the build context.
Push Your Container to Docker Hub
- Login to Docker Hub:
docker login
- Build your image:
docker build -t <docker_hub_username>/<image_name> .
- Push the image:
docker push <docker_hub_username>/<image_name>
Now anyone can pull and use your image:
docker pull <docker_hub_username>/<image_name>
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" ]
Explanation
-
FROM ubuntu
β Uses Ubuntu as the base image. -
RUN
commands β Updates the system, installs curl & Node.js. -
COPY
commands β Copiespackage.json
andmain.js
into the container. -
RUN npm install
β Installs dependencies. -
ENTRYPOINT
β Runsnode 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" ]
Explanation
-
FROM node
β Uses the official Node image, which comes pre-installed with Node.js. -
COPY
commands β Copypackage.json
(orpackage-lock.json
) andmain.js
into the container. -
RUN npm install
β Installs your Node.js dependencies. -
ENTRYPOINT
β Sets the container to runnode 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
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
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"
How It Works
-
Services: Defines two services:
app
(Node.js) anddatabase
(MongoDB). -
Build & Dependencies: The
app
service builds from theDockerfile
and depends ondatabase
, ensuring MongoDB starts first.
Running with Docker Compose
- Start all services in the background:
docker-compose up -d
- Stop and remove all services:
docker-compose down
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.
Top comments (2)
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!
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 π