Docker
Source:
1. ChatGPT
2. Docker Model Runner | Docker Docs
Docker Definition
Docker is an open-source platform for developing, shipping, and running applications. It
allows you to package an application with all of its dependencies into a container, ensuring
that it runs smoothly in any environment.
Key Concepts:
1. Container - A lightweight, standalone package that includes everything needed to
run a piece of software (code, runtime, libraries, settings). Think of it like a mini
virtual machine, but more efficient.
2. Image - A blueprint for a container. It’s a read-only template with instructions for
creating containers.
3. Docker Engine - The core software that runs and manages containers.
4. Docker Hub - A cloud-based registry where Docker users can share container images
(like GitHub for Docker images).
Why Use Docker?
• Consistency: Runs the same on any machine (developer’s PC, server, cloud).
• Isolation: Each container is independent and doesn't affect others.
• Portability: Works on any platform that supports Docker.
• Efficiency: Uses system resources more efficiently than virtual machines.
Example Use Case:
You’re developing a Python web app. Instead of worrying about Python version, pip
packages, OS settings, etc., you:
1. Create a Docker image with everything your app needs.
2. Run the app in a container using that image.
3. Share the image or push it to Docker Hub so others can run the same app easily.
Virtual Machines and Containers
Virtual Machines
A Virtual Machine is like a complete computer inside your computer. It has its own operating
system, storage, and apps, but it's running on top of your real computer using special
software called a hypervisor.
Container
A Container is a lightweight package that includes an app and everything it needs to run, but
it shares the host OS instead of running a full OS. Containers are faster and use fewer
resources.
Docker Architecture
1. Docker CLI
This is what you interact with.
You type commands like docker build, docker run, etc.
• Think of it as: You placing an order.
• It communicates with the Docker Daemon.
2. Doker Daemon
The engine that does the heavy lifting.
It builds, runs, and manages containers on your system.
• Think of it as: The kitchen that prepares your tiffin box.
• It talks to the Docker Client and Containerd (internally).
3. Docker Images
An image is like a recipe for your app. It contains code + dependencies +
environment.
• Think of it as: A ready-to-cook recipe.
• You build it once, and it can run anywhere.
4. Docker Container
These are running instances of Docker images. They’re lightweight, isolated, and fast.
• Think of it as: A packed and delivered tiffin box ready to eat.
5. Dockerfile
A text file with instructions to build a Docker image (like install Python, copy files, run
server, etc.).
• Think of it as: The instructions/steps to prepare your tiffin box.
6. Docker Hub (or Private Registry)
A cloud library of Docker images. You can push (upload) your image here or pull
(download) others'.
• Think of it as: A food delivery app where you choose from many recipes.
Docker Installation
1. First of all, we need to install Linux
(WSL stands for Windows Subsystem for Linux)
(This may take few minutes)
When Linux will be done then it will start downloading Ubuntu automatically
2. Now enable your virtualization in BIOS of your system
(Mine was already on)
3. Download Docker Desktop
- https://www.docker.com/products/docker-desktop/
(Download - Windows AMD-64 and install it)
Now if you will enter command -
docker - - version then it will show version of your docker
Docker Commands
1. docker run hello-world
(Initially my docker was not able to find “hello-world” image locally so it
automatically downloaded it or pulled it)
(After then it will create a container and run it)
2. How to pull an Image
You can visit Docker Hub Container Image Library | App Containerization to
download images
For now I am downloading node in my system
3. Command to show all docker images available in your system
4. Command to remove a docker image
5. Command to force remove any image
6. Command to show running containers
7. Docker command to show all containers (including stopped ones)
8. For now node is present in all containers but not showing in running containers, to
run this container we can start this container
9. Docker -it command
10. Docker -d command
11. Now to stop any running container we are having a command
12. Now to remove any stopped container we are having a command
13. To remove all stopped containers at once we are having a command
Interact with Running Containers
docker exec lets you run a command inside a running container — like opening a terminal
inside the container and doing stuff.
1. Run a Node.js container
2. Open the terminal inside the node container
3. List files inside a folder (e.g., /usr/src/app)
4. You can run Node commands directly from your host
(In this command node is signifying command not an image )
We can also achieve it like this
node -e “console.log(‘…’)” is code execution command
docker logs <container_name_or_id>
View logs from a container (like console.log or print() output).
docker inspect <container_name_or_id>
Gives detailed JSON info about the container (ports, env, volumes, etc.).
Docker Images & Dockerfile
What is a Docker Image?
• A Docker image is a read-only template used to create containers.
• It includes your application code, dependencies, runtime, and everything needed to
run the app.
• Think of it like a blueprint or a frozen snapshot of your app's environment.
What is a Dockerfile?
• A Dockerfile is a text file that contains a list of instructions to build a Docker image.
• It's like a recipe for baking your app into an image.
You have to change directory to your project directory and then you have to run these
commands
This will create a new image for your project
Docker Volumes
Docker volumes are used to persist data outside the container’s filesystem, Because when a
container is deleted or restarted, its data is lost. Volumes allow data to survive container
restarts or deletion.
Volumes are a super important concept in Docker — especially when you're dealing with
databases, logs, or persistent data.
Types of Volume
1. Name Volumes
You can’t create a volume for a existing container because Docker does not support
modifying volumes on an existing container directly.
Docker does not support modifying volumes on an existing container directly.
First of all you will create a new container and make sure you are running it in
background
Now you can see logs folder here
Or you can do one more thing to check whether it is attached or not
.
.
Now we are checking whether this my_logs volume is storing changes or not. To
check this first of all I’ll create a file inside logs
Now exit from the bash, stop that container and then remove it
Now start a new container with same volume
And now check whether logs/test.txt is there or not
You can check volumes also by entering command
You can remove unnecessary volumes also
docker volume rm <volume_name>
2. Anonymous Volume
An anonymous volume is a volume that Docker creates automatically for a container
when you mount a volume without giving it a name.
It’s “anonymous” because:
• You didn’t name it
• Docker generates a random name
• It’s still persistent — but you don’t directly control or reuse it easily
Now you can check volume list
You can inspect it also
3. Bind Mount
A bind mount lets you mount a specific folder or file from your host machine
(Windows/Mac/Linux) into a container — so the container and host can see and
share the same files in real time.
Creating a volume
Now we are creating a text file inside data folder inside container
Now changes are looking in my local machine
Docker Networking
Docker networking allows containers to communicate with each other, the host, and the
internet. Each container gets its own virtual network interface, and you can decide how they
connect — either in isolation, with each other, or with your system.
Types of Networks
1. bridge (Default)
A bridge network is Docker’s default private network for containers. Think of it as a
virtual LAN where containers can talk to each other internally, isolated from your
host machine unless you expose ports.
When you run a container without specifying a network, it gets attached to the
default bridge network.
Now we are creating our custom network
Now we are running 2 different containers on that network
Test Connectivity (Ping)
(Get inside container 1 and ping container 2)
2. host (Only on Linux)
3. None
4. Overlay
How to create and use overlay network
Step 1: Initialize Swarm (on one host)
Step 2: Create Overlay Network
Step 3: Deploy a Service using that Network
Now inspect overlay network
Docker Compose
Example
Docker Hub
Multistage build in Docker
Health Check in Docker
Docker secret keys