Docker
Docker
Docker is an open-source platform that allows you to easily build, package, and run applications in
isolated environments called containers.
🚢 Imagine this:
You build an app on your laptop — it works fine. But when you move it to a server or your friend’s
computer, it throws errors. This is the classic “it works on my machine” problem.
Docker solves this by letting you package your app with everything it needs — code, runtime,
libraries, environment variables — into a container that runs the same anywhere.
🧱 Key Concepts:
Image: A blueprint for a container. You build an image, and then you can run multiple
containers from that image.
Docker file: A file with instructions for building a Docker image (like a recipe).
Docker Hub: A registry (like GitHub) where you can store and share Docker images.
Easy deployment
Portable yes
Package:
"package" means putting everything your application needs into one bundle — like zipping up your
app with all its files, settings, and tools it needs to run.
System libraries
Environment variables
You build an image once, and then run containers from it as many times as you want.
🧱 Analogy:
Think of a Docker image like a class, and a container like an object (instance) created from that class.
⚙️How to Work with Docker Images:
Command Description
This command builds an image named docker_apna_college with the default latest tag and stores it
locally. It is not associated with any remote registry.
This command builds an image named testapp with the default latest tag and associates it with the
Docker Hub repository shyamsingh841442/testapp. To push this image to Docker Hub, you would
use:
bash
CopyEdit
Tag: 1.0
This command builds an image named testapp with the tag 1.0 and associates it with the Docker Hub
repository shyamsingh841442/testapp. To push this specific version to Docker Hub, you would use:
bash
CopyEdit
Summary
A Docker container is a lightweight, isolated environment where an application runs. It’s created
from a Docker image, and it contains everything your app needs: code, libraries, dependencies, and
configuration.
Think of it like:
docker exec -it container_id bash Opens a terminal inside the container
You cannot run it directly — just like you can’t live in a house plan.
✅ Analogy:
A cake recipe 📄 – it has all the instructions, but you can’t eat it.
🚀 Docker Container
You can stop, start, delete, or run multiple containers from the same image.
✅ Analogy:
A cake 🎂 you made using the recipe – now it’s real and ready to use!
🧠 Summary Table:
Container registries
Container registries are centralized repositories for storing and managing container images, which
are packages that contain everything needed to run a software application (e.g., the code, runtime,
libraries, and dependencies). These registries are essential in containerized applications and DevOps
workflows, especially when using Docker or Kubernetes.
1. Public Registries:
o Docker Hub: The most popular public container registry. It contains a vast number of
pre-built container images for various applications and operating systems.
o [Link]: Another widely used public registry, often preferred for its advanced
security features.
2. Private Registries:
o Amazon Elastic Container Registry (ECR): AWS’s managed container registry that
integrates with other AWS services and offers private storage for container images.
o Google Container Registry (GCR): Google's registry that integrates with Google
Cloud services.
o Azure Container Registry (ACR): A managed private registry for storing container
images with integration into Azure services.
Storage: Container registries store container images and provide versioning to keep track of
different versions of the same image.
Access Control: Registries allow you to control access to your container images using
authentication methods like OAuth, SSO, or private credentials.
Scanning and Security: Many registries provide security features like image vulnerability
scanning to detect known security flaws in your container images.
Replication: Some registries support replicating images across multiple regions or other
registries for disaster recovery or performance optimization.
1. Push Images: After building a container image locally (e.g., using Docker), you can push it to
a container registry using commands like docker push.
2. Pull Images: To use an image stored in a registry, you can pull it into your local environment
or deployment platform using commands like docker pull.
docker login
Containers
A container in Docker is a lightweight, portable, and self-sufficient unit that packages an application
and all its dependencies to run consistently across different environments. Containers run
applications in isolated environments, which means they include everything needed for an
application to execute, such as the code, libraries, system tools, and settings.
Isolation: Containers run in their own isolated environment, which means they don't
interfere with other containers or the host system.
Lightweight: Unlike virtual machines (VMs), containers share the same OS kernel, making
them more resource-efficient and quicker to start.
Portability: Containers can run on any system that has Docker installed, regardless of the
underlying infrastructure (e.g., local machines, cloud services, or on-premise data centers).
Consistency: Containers ensure that an application will run the same way across
development, testing, and production environments.
A Docker container is created from a container image, which is a blueprint that contains the
application code and environment configuration. The image is read-only, but when you run a
container, a writable layer is added on top.
Executes applications and manages Stores images and facilitates sharing and
Scope
resources. versioning.
Containers are created from images stored Registries store images that containers are
Interaction
in registries. created from.
Conclusion:
Container Registries are where the Docker images (which are used to create containers) are
stored, shared, and versioned.
Highly portable (works the same across Less portable due to dependency on
Portability
environments) hypervisors
🧠 Key Analogy:
Containers are like compartments in a shared apartment – they share the same roof (OS),
but live separately.
VMs are like independent houses – each with its own OS, utilities, and overhead.
Docker Architecture Overview
1. Docker Client
Interface for users to interact with Docker.
Sends commands (docker build, docker run, docker pull) to the Docker Daemon.
Can communicate with local or remote daemon via REST API.
3. Docker Images
Blueprint for containers.
Read-only templates with instructions to create a container.
Built using a Docker file.
Can be versioned and shared via a registry.
4. Docker Containers
Running instances of Docker images.
Lightweight and isolated, yet share the host OS kernel.
Can be started, stopped, moved, and deleted easily.
Ideal for running microservices or applications.
5. Docker Registry
Storage for Docker images.
Public: Docker Hub
Private: Custom registry for enterprises.
Commands like docker pull and docker push interact with the registry.
🧠 Example Workflow:
docker build → creates image from Dockerfile.
docker run → creates and starts a container from an image.
docker push → uploads image to registry.
docker pull → downloads image from registry.
▶️docker run
Purpose: Starts a new container from an image.
Syntax:
Example:
Options:
🛑 docker stop
Purpose: Gracefully stops a running container.
Syntax:
❌ docker rm
Purpose: Removes a stopped container.
Syntax:
docker rm <container_id_or_name>
docker images
Purpose: Lists all available images on your system.
🧹 docker rmi
Purpose: Removes a Docker image from your system.
Syntax:
💬 docker exec
Purpose: Executes a command inside a running container.
Syntax:
🔗 docker attach
Purpose: Attaches your terminal to a running container’s main process.
Syntax:
Command Description
docker ps -a
✅ What it does:
Shows all containers, whether they are currently running, stopped, or exited.
Displays useful info: container ID, name, image used, status, ports, and start time.
🟩 2. List only Running Containers
docker ps
✅ What it does:
Helpful for checking which containers are active and consuming resources.
✅ What it does:
If the image is not found locally, Docker pulls it from Docker Hub.
📝 Example:
✅ What it does:
📝 Example:
✅ What it does:
📝 Example:
When you run a container, it may expose certain ports (like 3306 for MySQL). But unless you map
them to your host system, you can't access them from outside.
🔧 Command Syntax
✅ Example:
📦 In the Diagram:
The left box labeled older maps port 3306 to host port 8080.
The right box labeled latest maps the same 3306 to host port 5000.
This allows two containers running MySQL to operate simultaneously by exposing them on
different host ports.
Useful for local development, testing multiple versions of services (like different MySQL
versions).
docker run -d -e VAR=value --name [name] -p HOST_PORT:CONTAINER_PORT [image]
5. -p: Map host port to container port (e.g., -p 8080:3306 maps host’s 8080 → container’s
3306).
In this pic above the host ports wants to bind two diff container than it start showing error. two or
more container can bind different host port, but there map be two container with same port no
docker exec -it mysql-latest /bin/bash
Explanation:
✅ What it does:
📝 Example:
✅ What it does:
📝 Example:
✅ What it does:
docker rm <container_name_or_id>
✅ What it does:
📝 Example:
docker rm myweb
This image shows troubleshooting commands used in Docker when something goes wrong inside a
container. Let’s break down each command:
Purpose: View the logs (standard output and error) of a running or stopped container.
Use it when: You want to check what’s going wrong inside the container (like crash reports,
startup errors, etc.).
Example:
Use it when: You need to debug, check files, run commands, or inspect environment
variables inside the container.
Example:
📝 Note: Only works if the container has bash installed (common in Ubuntu-based images).
Use it when: You're working with lightweight images like Alpine Linux or BusyBox which
don’t have bash.
Example:
docker exec -it 5a1b3c2f4g /bin/sh
🧠 Summary
Command Use
docker exec -it ... /bin/sh Open sh shell in container (fallback for minimal images)
Think of an image like a recipe for running a program (e.g., a MySQL server, [Link] app, etc.)
DockerHub hosts public and private images that you can pull, push, or search for.
🧠 What it does:
Example:
🧠 What it does:
Uploads your locally built image to Docker Hub under your account.
Example:
You must first tag your local image with your Docker Hub username:
or simply:
docker login
🧠 What it does:
After logging in, you can push images, see your private images, etc.
✅ You can logout anytime:
docker logout
🧠 What it does:
Example:
1. Pull an image
➜ docker pull node
5. Login
➜ docker login
6. Push it
➜ docker push shyam84/my-node-app
A Docker network is like a private chat room where your containers (small apps) can talk to each
other.
🧠 Think of it like this:
If containers are in the same network, they can send messages to each other easily.
📦 Example:
If you have:
Putting them on the same Docker network lets your app connect to the database smoothly.
In Docker, containers are isolated by default. Docker networks allow containers to communicate with
each other and with external systems.
host
none
docker network ls
🔹 Description:
This command lists all existing Docker networks on your system.
🔹 Output Example:
sql
CopyEdit
🔹 Explanation:
2. Create a Network
🔹 Example:
bash
CopyEdit
🔹 Description:
Containers on the same user-defined bridge network can communicate using container
names.
🧹 3. Remove a Network
🔹 Example:
bash
CopyEdit
🔹 Description:
⚠️Important:
🔹 Description:
🔹 Confirmation:
pgsql
CopyEdit
WARNING! This will remove all custom networks not used by at least one container.
🔹 Use Case:
✅ Summary
Command Description
Docker Compose is a tool used to define and run multi-container applications (apps that need more
than one service — like a frontend, backend, and a database).
Instead of running docker run multiple times for different containers, you can define everything in
one file — and with one command, all containers will start up together.
[Link]
version: "3.8"
services:
mongo:
image: mongo
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: qwerty
mongo-express:
image: mongo-express
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: qwerty
ME_CONFIG_MONGODB_URL: mongodb://admin:qwerty@mongo:27017/
docker-compose up
-f [Link]: specifies the file that contains the configuration (you can name it anything,
but by default it’s [Link]).
down: stops and removes the containers, networks, volumes, and images created by up.
Note:
If your YAML file is named [Link] and it's in the root directory, then you can simply
run:
docker compose up -d
o So, containers can talk to each other using service names (like mongo, backend,
etc.).
o You can define all your services, volumes, networks, and environment variables in
one place.
bash
CopyEdit
docker compose up -d
4. 🔁 Reproducibility
o Makes it easy to recreate the exact same environment on any system – great for
teams or deployment.
5. 🔧 Environment management
o Easy to configure different setups (dev, test, prod) using different .yaml or .env files.
o Just use the service name (e.g., mongo) and it works like magic!
7. 📂 Volume management
o You can quickly spin up a full app stack for testing, experimenting, or demos.
What is Dockerizing?
Dockerizing means packaging your application (code + dependencies) into a Docker container, so it
can run anywhere, regardless of the underlying system.
Your code
Required libraries
You write your own Dockerfile, and build/run containers using Docker CLI.
1. Write a Dockerfile
Examples:
Heroku / Render / Railway → Sometimes generate containers under the hood for
deployment
🔁 Summary
What is Jenkins?
Jenkins is an open-source automation server used to automate software building, testing, and
deployment — basically the heart of CI/CD pipelines.
It automatically:
[GitHub Push] ➡️[Jenkins Job] ➡️[Test Code] ➡️[Build App] ➡️[Deploy to Server]
🧰 Jenkins Features
🔌 Plugins – Jenkins has 1000+ plugins for Git, Docker, Slack, etc.
🐳 Docker & Kubernetes – Jenkins works beautifully with Docker and K8s
Explanation
A Dockerfile is a text-based document that's used to create a container image. It provides
instructions to the image builder on the commands to run, files to copy, startup command, and
more.
FROM python:3.12
WORKDIR /usr/local/app
COPY [Link] ./
EXPOSE 5000
# Setup an app user so the container doesn't run as the root user
USER app
Common instructions
FROM <image> - this specifies the base image that the build will extend.
WORKDIR <path> - this instruction specifies the "working directory" or the path in the image
where files will be copied and commands will be executed.
COPY <host-path> <image-path> - this instruction tells the builder to copy files from the host
and put them into the container image.
RUN <command> - this instruction tells the builder to run the specified command.
ENV <name> <value> - this instruction sets an environment variable that a running container
will use.
EXPOSE <port-number> - this instruction sets configuration on the image that indicates a
port the image would like to expose.
USER <user-or-uid> - this instruction sets the default user for all subsequent instructions.
CMD ["<command>", "<arg1>"] - this instruction sets the default command a container using
this image will run.
To verify
docker images
it will list the available image
Go to [Link]
docker login
php-template
<your-dockerhub-username>/<image-name>:<tag>
For example, if your Docker Hub username is skr84 and your image is testapp:
bash
CopyEdit
In your case, you are using a Bind Mount (manual mapping of host to container).
Any file created or edited in /test/data (inside the container) appears on your Desktop.
➡️This mounts the volume mydata to the /app/data directory inside the container.
yaml
CopyEdit
version: '3'
services:
mongo:
image: mongo
volumes:
- mongo-data:/data/db
nodeapp:
image: mynodeapp
depends_on:
- mongo
volumes:
mongo-data: