0% found this document useful (0 votes)
1 views40 pages

Docker

docker

Uploaded by

skr841442
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views40 pages

Docker

docker

Uploaded by

skr841442
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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:

 Container: A lightweight, standalone, and executable package that includes everything


needed to run an application.

 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.

🔥 Why Docker is useful:

 Consistent environment (dev, test, prod)

 Easy deployment

 Works well with microservices

 Lightweight compared to traditional VMs


How is it different from a Virtual Machine ?

Feature Container Virtual Machine

Size Lightweight (MBs) Heavy (GBs)

Boot Time Seconds Minutes

Isolation Process-level Hardware-level

Performance Near-native Some overhead

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.

So when we say Docker packages your app, we mean:

👉 It includes your code,


👉 the libraries,
👉 the tools,
👉 and even the system settings your app needs.

What is a Docker Image?

A Docker image is a blueprint or template for a container. It contains:

 Your application code

 All necessary dependencies

 System libraries

 Environment variables

 Instructions for how to run the app

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

docker images List all available images on your system

docker pull image_name Download an image from Docker Hub

docker build -t my-image-name . Build an image from a Dockerfile

docker rmi image_name Remove an image

1. docker build -t docker_apna_college .

 Image Name: docker_apna_college

 Tag: latest (default)

 Registry: Local Docker registry

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.

2. docker build -t shyamsingh841442/testapp .

 Image Name: testapp

 Tag: latest (default)

 Registry: Docker Hub under the user shyamsingh841442

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

docker push shyamsingh841442/testapp

3. docker build -t shyamsingh841442/testapp:1.0 .

 Image Name: testapp

 Tag: 1.0

 Registry: Docker Hub under the user shyamsingh841442Medium

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

docker push shyamsingh841442/testapp:1.0

Summary

Command Image Name Tag Registry Association

docker build -t docker_apna_college . docker_apna_college latest Local only

docker build -t Docker Hub


testapp latest
shyamsingh841442/testapp . (shyamsingh841442/testapp)

docker build -t Docker Hub


testapp 1.0
shyamsingh841442/testapp:1.0 . (shyamsingh841442/testapp)

What is a Docker Container?

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:

📦 A container is a running instance of a Docker image.

⚙️Common Docker Container Commands:


Command What it does

docker ps Lists running containers

docker ps -a Lists all containers (including stopped ones)

docker run image_name Runs a container from an image

docker stop container_id Stops a running container

docker start container_id Starts a stopped container

docker rm container_id Removes a container

docker exec -it container_id bash Opens a terminal inside the container

Docker Image vs Docker Container


📦 Docker Image

Think of a Docker image like a blueprint or recipe.

 It's a read-only template.

 Contains all the code, dependencies, libraries, etc.

 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

A Docker container is like a running instance of an image.

 It's created from an image.

 It’s live, running, and can be interacted with.

 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:

Concept Docker Image Docker Container

What is it? Blueprint/template Running instance of the image


Concept Docker Image Docker Container

State Static (read-only) Dynamic (can change, run, stop, etc.)

Use Used to create containers Used to run apps/services

Analogy Cake recipe 📄 Actual cake 🎂

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.

Here are some key aspects of container registries:

Types of Container Registries:

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.

o GitHub Container Registry: Hosted by GitHub, this registry allows seamless


integration with GitHub actions and workflows.

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.

o Harbor: An open-source container registry with advanced features like role-based


access control (RBAC), replication, and vulnerability scanning.

Features of Container Registries:

 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.

 CI/CD Integration: Registries integrate with Continuous Integration and Continuous


Deployment (CI/CD) pipelines, allowing automatic image push/pull operations as part of your
build and deployment workflows.

 Replication: Some registries support replicating images across multiple regions or other
registries for disaster recovery or performance optimization.

How to Use a Container Registry:

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.

3. Authentication: If you're working with private registries, authentication is necessary before


pushing or pulling images. This might involve logging in with docker login or using an API key.

Example: Pushing to Docker Hub

# Build the image

docker build -t username/my-app:tag .

# Log in to Docker Hub

docker login

# Push the image to Docker Hub

docker push username/my-app:tag

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.

Key characteristics of containers:

 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.

Example of a Docker container command:

# Run a Docker container from an image

docker run -d --name my-app-container my-app-image:latest

Differences b/w Docker container & Container Registry:


Aspect Container Container Registry

A storage and management system for


Definition A running instance of a Docker image.
Docker images.

To run applications in isolated


Purpose To store and distribute container images.
environments.

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:

 Containers are the running instances that execute applications.

 Container Registries are where the Docker images (which are used to create containers) are
stored, shared, and versioned.

Containers vs Virtual Machines


Feature Containers Virtual Machines (VMs)

Share the host OS kernel; run as isolated


Architecture Include full OS with virtual hardware
processes
Feature Containers Virtual Machines (VMs)

Startup Time Very fast (seconds) Slower (minutes)

Slightly less due to virtualization


Performance Near-native performance
overhead

Resource Heavy, need more CPU, memory, and


Lightweight, use fewer resources
Usage storage

Isolation Process-level isolation OS-level isolation (stronger)

Highly portable (works the same across Less portable due to dependency on
Portability
environments) hypervisors

Running multiple OS types, legacy app


Use Case Microservices, DevOps, CI/CD pipelines
support

Docker, Podman, Kubernetes (orchestrates


Examples VMware, VirtualBox, Hyper-V
containers)

🧠 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.

2. Docker Daemon (dockerd)


 Core engine running in the background.
 Listens to Docker API requests and manages Docker objects (containers, images, networks,
volumes).
 Responsible for:
1. Building images
2. Running containers
 Managing system resources

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.

🔧 Basic Docker Commands Explained

▶️docker run
Purpose: Starts a new container from an image.

Syntax:

docker run [OPTIONS] IMAGE [COMMAND]

Example:

docker run -d -p 8080:80 nginx

 Runs an NGINX container in detached mode (-d)

 Maps host port 8080 to container port 80


📋 docker ps
Purpose: Lists running containers.

Options:

 docker ps -a → shows all containers (running + stopped)

🛑 docker stop
Purpose: Gracefully stops a running container.

Syntax:

docker stop <container_id_or_name>

❌ docker rm
Purpose: Removes a stopped container.

Syntax:

docker rm <container_id_or_name>

Tip: Combine commands like:

docker stop my_container && docker rm my_container

docker images
Purpose: Lists all available images on your system.

🧹 docker rmi
Purpose: Removes a Docker image from your system.

Syntax:

docker rmi <image_id_or_name>

Note: Cannot remove an image if it's being used by a container.

💬 docker exec
Purpose: Executes a command inside a running container.

Syntax:

docker exec -it <container> <command>


Example:

docker exec -it my_container bash

 Opens an interactive shell inside the container.

🔗 docker attach
Purpose: Attaches your terminal to a running container’s main process.

Syntax:

docker attach <container>

Note: Exits the container if you press Ctrl+C — unlike exec.

🧠 Quick Summary Table

Command Description

docker run Create & start container

docker ps List running containers

docker stop Stop a container

docker rm Remove a stopped container

docker images List local images

docker rmi Remove an image

docker exec Run command in container

docker attach Attach terminal to container

🧱 CONTAINER COMMANDS IN DOCKER


🟦 1. List all Local Containers (Running + Stopped)

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:

 Lists only the containers that are currently running.

 Helpful for checking which containers are active and consuming resources.

🚀 3. Create & Run a New Container

docker run <image_name>

✅ What it does:

 Creates and starts a new container from the specified image.

 If the image is not found locally, Docker pulls it from Docker Hub.

 This runs the container in the foreground by default.

📝 Example:

docker run ubuntu

💤 4. Run Container in the Background (Detached Mode)

docker run -d <image_name>

✅ What it does:

 Runs the container in detached mode (in the background).

 Useful for long-running processes or services like servers.

📝 Example:

docker run -d nginx

5. Run Container with a Custom Name

docker run --name <container_name> <image_name>

✅ What it does:

 Assigns a custom name to your container instead of a randomly generated one.

 Helps with easy identification and management.

📝 Example:

docker run --name myweb nginx


🌐 6. Port Binding in Container

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

docker run -p <hostPort>:<containerPort> IMAGE_NAME

✅ Example:

docker run -p 8080:3306 mysql

 3306 is the port inside the container (used by MySQL).

 8080 is the port on your host machine (accessible via localhost:8080).

 Docker maps traffic from host 8080 to container 3306.

📦 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.

🧠 Why This Matters:

 You can't bind two containers to the same host port.

 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]

1. docker run: Create and start a new container.

2. -d: Run in detached mode (background).

3. -e: Set environment variables (e.g., MYSQL_ROOT_PASSWORD=secret).

4. --name: Assign a name to the container (e.g., mysql-latest).

5. -p: Map host port to container port (e.g., -p 8080:3306 maps host’s 8080 → container’s
3306).

6. [image]: Docker image to use (e.g., mysql:8.0 or mysql:latest).

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:

docker exec: Runs a command in a running container.

-it: Interactive terminal (so you can type commands).

mysql-latest: The name of the Docker container running MySQL.

/bin/bash: Opens a Bash shell inside that container.

🌱 7. Set Environment Variables in a Container

docker run -e <var_name>=<var_value> <image_name>

✅ What it does:

 Sets environment variables inside the container.

 Useful for configuration, secrets, or runtime behavior.

📝 Example:

docker run -e ENV=production myapp

🔄 8. Start or Stop an Existing Container

docker start <container_name_or_id>


docker stop <container_name_or_id>

✅ What it does:

 start: Starts a stopped container.

 stop: Gracefully stops a running container.

📝 Example:

docker stop myweb

docker start myweb

🔍 9. Inspect a Running Container

docker inspect <container_name_or_id>

✅ What it does:

 Gives detailed JSON output about the container.

 Includes config, volumes, networking info, IP addresses, environment variables, etc.

❌ 10. Delete a Container

docker rm <container_name_or_id>

✅ What it does:

 Removes a container permanently.

 You must stop it before removal if it’s running.

📝 Example:

docker stop myweb

docker rm myweb
This image shows troubleshooting commands used in Docker when something goes wrong inside a
container. Let’s break down each command:

🔍 docker logs CONT_ID

 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:

docker logs 5a1b3c2f4g

💻 docker exec -it CONT_ID /bin/bash

 Purpose: Opens an interactive shell session (bash) inside the container.

 Use it when: You need to debug, check files, run commands, or inspect environment
variables inside the container.

Example:

docker exec -it 5a1b3c2f4g /bin/bash

📝 Note: Only works if the container has bash installed (common in Ubuntu-based images).

🐚 docker exec -it CONT_ID /bin/sh

 Purpose: Same as above, but uses sh shell instead of bash.

 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 logs Check logs (errors, output)

docker exec -it ... /bin/bash Open bash shell in container

docker exec -it ... /bin/sh Open sh shell in container (fallback for minimal images)

🐳 What is Docker Hub?


Docker Hub is like GitHub but for Docker 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.

🔽 1. Pull an image from Docker Hub

docker pull <image_name>

🧠 What it does:

 Downloads the image from Docker Hub to your local machine.

 Example:

docker pull mysql

o This pulls the latest version of the official mysql image.

✅ You can also pull a specific version:

docker pull mysql:8.0

🔼 2. Push an image to Docker Hub

docker push <username>/<image_name>

🧠 What it does:

 Uploads your locally built image to Docker Hub under your account.

 Example:

docker push shyam84/my-node-app

👉 But before you push:

 You must first tag your local image with your Docker Hub username:

docker tag my-node-app shyam84/my-node-app

🔐 3. Login to Docker Hub

docker login -u <username>

or simply:

docker login

🧠 What it does:

 Prompts you to enter your Docker Hub username and password.

 After logging in, you can push images, see your private images, etc.
✅ You can logout anytime:

docker logout

🔍 4. Search for an image on Docker Hub

docker search <image_name>

🧠 What it does:

 Searches for available images related to your keyword.

 Example:

docker search mysql

Will show results like:

NAME DESCRIPTION STARS OFFICIAL AUTOMATED

mysql MySQL is a widely used... 12000+ [OK]

mariadb MariaDB is a community... 4000+ [OK]

🟢 OFFICIAL means it's maintained by Docker or the creators of the technology.

🔁 Summary of Flow (in simple steps):

1. Pull an image
➜ docker pull node

2. Use it to run a container


➜ docker run -it node

3. Build your own image


➜ docker build -t my-node-app .

4. Tag it for Docker Hub


➜ docker tag my-node-app shyam84/my-node-app

5. Login
➜ docker login

6. Push it
➜ docker push shyam84/my-node-app

🧠 What is a Docker Network?


🚢 What is a Docker network?

A Docker network is like a private chat room where your containers (small apps) can talk to each
other.
🧠 Think of it like this:

 Each container is like a person.

 The Docker network is a WhatsApp group.

 If containers are in the same network, they can send messages to each other easily.

📦 Example:

If you have:

 A MongoDB container (database)

 A [Link] container (your app)

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.

Docker provides different network drivers like:

 bridge (default for standalone containers)

 host

 none

 overlay (for swarm)

 macvlan (for advanced use cases)

🔍 1. List All Networks

docker network ls

🔹 Description:
This command lists all existing Docker networks on your system.

🔹 Output Example:

sql

CopyEdit

NETWORK ID NAME DRIVER SCOPE

d1b8e41b6f1f bridge bridge local

e7d53faeefb6 host host local

ac17e6e568f3 none null local

🔹 Explanation:

 bridge: Default network Docker uses for containers.

 host: Shares host’s network stack.

 none: No networking for the container.

 You can also see any user-defined networks you've created.

2. Create a Network

docker network create <network_name>

🔹 Example:

bash

CopyEdit

docker network create my-network

🔹 Description:

Creates a new user-defined bridge network named my-network.

🔹 Why Use It?

 Containers on the same user-defined bridge network can communicate using container
names.

 Useful in microservices, e.g., connecting frontend ↔ backend ↔ database.

🧹 3. Remove a Network

docker network rm <network_name>

🔹 Example:

bash
CopyEdit

docker network rm my-network

🔹 Description:

Removes the specified network from Docker.

⚠️Important:

 You cannot remove a network if containers are still connected to it.

 First stop or disconnect those containers.

🚿 4. Remove All Unused Networks

docker network prune

🔹 Description:

Deletes all unused networks:

 Not used by any container

 Not default networks like bridge, host, none

🔹 Confirmation:

Docker will prompt:

pgsql

CopyEdit

WARNING! This will remove all custom networks not used by at least one container.

Are you sure you want to continue? [y/N]

🔹 Use Case:

 Useful for cleaning up resources.

 Helps avoid clutter in long-running development environments.

✅ Summary

Command Description

docker network ls List all networks

docker network create <name> Create a new network

docker network rm <name> Remove a specific network

docker network prune Remove all unused (dangling) networks


🔹 What is Docker Compose?

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.

🔸 What goes inside a [Link] file?

Here’s a basic example for better understanding:

[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/

🔹 How to run it?

Just go to the folder where [Link] is and run:

docker-compose up

That’s it! 🎉 No more running multiple commands.


1. To start the containers:

docker compose -f [Link] up -d

 docker compose: the command to run Docker Compose.

 -f [Link]: specifies the file that contains the configuration (you can name it anything,
but by default it’s [Link]).

 up: brings up the services (builds images, starts containers).

 -d: runs the containers in detached mode (in the background).

2. To stop the containers:

docker compose -f [Link] down

 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

docker compose down

Advantages of Docker Compose

1. 🧠 No need to create Docker networks manually

o Compose automatically creates a network and connects all services (containers) to


it.

o So, containers can talk to each other using service names (like mongo, backend,
etc.).

2. 📦 Single configuration file ([Link])

o You can define all your services, volumes, networks, and environment variables in
one place.

o Easy to maintain and share across teams or projects.


3. 🚀 Start everything with one command

bash

CopyEdit

docker compose up -d

o It starts all containers at once, in the correct order.

o No need to manually build and run each one separately.

4. 🔁 Reproducibility

o Makes it easy to recreate the exact same environment on any system – great for
teams or deployment.

5. 🔧 Environment management

o You can pass variables through .env files.

o Easy to configure different setups (dev, test, prod) using different .yaml or .env files.

6. Service Linking by Name

o You don't need to remember IPs or set up links.

o Just use the service name (e.g., mongo) and it works like magic!

7. 📂 Volume management

o Docker Compose handles persistent storage (like MongoDB data) by managing


volumes easily.

8. 🧪 Perfect for development & testing

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.

Think of a container as a lightweight box that holds:

 Your code

 Required libraries

 Runtime ([Link], Python, Java, etc.)

 Configuration (like environment variables)

This way, "it works on my machine" becomes "it works everywhere."

🔨 What is Manual Dockerizing?

Manual Dockerizing means:

You write your own Dockerfile, and build/run containers using Docker CLI.

Example Workflow (Manual):

1. Write a Dockerfile

2. Run docker build to create an image

3. Run docker run to start the container

4. Optional: write a [Link] if using multiple services

You're in full control. ✅

Pro: More flexible and production-ready


🧠 Con: Slight learning curve for beginners

🧙‍♂️What is Dockerizing via Software / GUI Tools?


This means using tools or platforms that automatically generate Dockerfiles or manage containers
without needing to write much (or any) code.

Examples:

 Docker Desktop → Has GUI to manage containers/images/volumes

 Portainer → Web-based Docker management tool

 VS Code Docker Extension → Helps generate Dockerfile and [Link]

 Heroku / Render / Railway → Sometimes generate containers under the hood for
deployment

Pro: Quick and easy for beginners


📦 Con: Limited customization and transparency

🔁 Summary

Type You Do Control Level Best For

Manual Dockerizing Write Dockerfile, Compose Full Developers, production apps

Software/GUI Tools Click + Autogenerate Limited Beginners, quick testing

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.

CI/CD = Continuous Integration / Continuous Deployment


Jenkins = The tool that makes it happen

🚀 Why is Jenkins Used?

Imagine you're a developer:

 You push your code to GitHub.

 Jenkins sees the change.

 It automatically:

o Pulls the code


o Runs tests

o Builds the project

o Deploys it to a server (or Docker, or AWS, etc.)

You don’t have to do it manually every time!

🔁 Jenkins in Action (Workflow)

Here's a typical Jenkins flow:

[GitHub Push] ➡️[Jenkins Job] ➡️[Test Code] ➡️[Build App] ➡️[Deploy to Server]

You define steps using a:

 Jenkinsfile (for pipeline-as-code)

 or use GUI to create jobs

🧰 Jenkins Features

 💡 Pipeline Support – You can write full pipelines using Jenkinsfile

 🔌 Plugins – Jenkins has 1000+ plugins for Git, Docker, Slack, etc.

 🧪 Automated Testing – Run unit tests, integration tests, etc.

 🐳 Docker & Kubernetes – Jenkins works beautifully with Docker and K8s

 ☁️CI/CD to Cloud – Deploy to AWS, GCP, Azure

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.

As an example, the following Dockerfile would produce a ready-to-run Python application:

FROM python:3.12

WORKDIR /usr/local/app

# Install the application dependencies

COPY [Link] ./

RUN pip install --no-cache-dir -r [Link]


# Copy in the source code

COPY src ./src

EXPOSE 5000

# Setup an app user so the container doesn't run as the root user

RUN useradd app

USER app

CMD ["uvicorn", "[Link]:app", "--host", "[Link]", "--port", "8080"]

Common instructions

Some of the most common instructions in a Dockerfile include:

 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.

Docker build -t testapp:1.0.

Docker run testapp:1.0

To verify
docker images
it will list the available image

Push your Docker image to Docker Hub:

✅ Step 1: Create a Docker Hub Account (if not already)

 Go to [Link]

 Sign up or log in.


✅ Step 2: Login to Docker Hub from your terminal

docker login

 Enter your Docker Hub username and password when prompted.

 If successful, you'll see: Login Succeeded.

✅ Step 3: Tag your Docker image properly

Docker images must be tagged in the format:

php-template

<your-dockerhub-username>/<image-name>:<tag>

For example, if your Docker Hub username is skr84 and your image is testapp:

bash

CopyEdit

docker tag testapp:1.0 skr84/testapp:1.0

✅ Step 4: Push the image to Docker Hub

docker push skr84/testapp:1.0

 This will upload your image to your Docker Hub account.

 You’ll see progress bars as it uploads the layers.

✅ Step 5: Verify on Docker Hub

 Go to: [Link] (replace with your actual username).

 You should see your testapp image listed there.

✅ (Optional) Pull from any machine

On any machine, you can pull the image using:

docker pull skr84/testapp:1.0


What is a Volume in Docker?

A volume is a way to persist data generated or used by Docker containers.


It allows your data to live outside the container, so it is not lost when the container stops, restarts,
or is deleted.

docker run -it -v "C:\Users\skr84\OneDrive\Desktop\docker_apna_college\data:/test/data" ubuntu

path of local directory docker volume name

📦 Types of Storage in Docker

Storage Type Description

Volumes Managed by Docker. Best for container data.

Bind Mounts Mounting a host directory to a container path.

Tmpfs Temporary in-memory storage (Linux only).


Storage Type Description

In your case, you are using a Bind Mount (manual mapping of host to container).

🔍 Volume Command Example (Bind Mount)

docker run -it -v /host/path:/container/path image_name

🧠 Real Example (from your image)

docker run -it -v /Users/amandhattarwal/Desktop/data:/test/data ubuntu

✅ What this does:

 Runs an interactive Ubuntu container.

 Mounts your local macOS folder (/Users/amandhattarwal/Desktop/data) into the container


path /test/data.

 Any file created or edited in /test/data (inside the container) appears on your Desktop.

🔹 Why Use Volumes?

 Data persistence across container restarts and rebuilds

 Share data between multiple containers

 Avoid cluttering container layers with data

 Better performance compared to bind mounts (in most cases)

🔹 Creating and Using a Volume

Step 1: Create a volume

docker volume create mydata

Step 2: Run a container with the volume

docker run -d --name mycontainer -v mydata:/app/data myimage

➡️This mounts the volume mydata to the /app/data directory inside the container.

Step 3: Check volume usage

docker volume ls # List all volumes


docker volume inspect mydata # Inspect volume details

Step 4: Remove volume

docker volume rm mydata

⚠️This will only work if no containers are using the volume.

🔹 Example: [Link] + MongoDB with Volume

yaml

CopyEdit

version: '3'

services:

mongo:

image: mongo

volumes:

- mongo-data:/data/db

nodeapp:

image: mynodeapp

depends_on:

- mongo

volumes:

mongo-data:

You might also like