Docker
What is Docker ?
Docker is a containerization platform that enables developers to
package and distribute applications and their dependencies as
lightweight, portable containers. Containers are isolated environments
that encapsulate an application and its dependencies, ensuring
consistency across different environments.
Use Cases of Docker
• Application Deployment and Scaling:
Docker simplifies the deployment process, making it easier to package and distribute applications. It also facilitates the
scaling of applications by replicating containers across multiple hosts.
• Micro services Architecture:
Docker is well-suited for micro services architectures, allowing developers to build, deploy, and scale individual services
independently in separate containers.
• Continuous Integration/Continuous Deployment (CI/CD):
Docker containers streamline the CI/CD pipeline, ensuring that applications behave consistently across development,
testing, and production environments.
• DevOps Practices:
Docker enhances collaboration between development and operations teams by providing a standardized environment,
reducing conflicts and discrepancies between development and production environments.
Platforms for Docker
• Docker Desktop:
- Ideal for local development and testing.
- Available for Windows and macOS.
- Provides a user-friendly interface for managing containers.
• Docker Hub:
- A cloud-based registry for sharing and distributing Docker images.
- Offers a vast repository of pre-built images for popular software.
• Docker Enterprise:
- Designed for enterprise-level container orchestration and management.
- Provides advanced features for security, scalability, and collaboration.
Docker vs. Virtualization
• Resource Efficiency:
Docker: Shares the host OS kernel, resulting in less overhead and greater efficiency.
Virtualization Requires separate OS instances for each virtual machine, consuming more
resources.
• Isolation:
Docker Uses containerization for application isolation.
Virtualization Provides stronger isolation through complete virtualized environments.
• Speed and Performance:
Docker Faster start-up times and lower resource consumption.
Virtualization Slower start-up times and higher resource usage.
• Flexibility:
Docker Lightweight and portable, easy to move and deploy.
Virtualization Requires more effort in moving and replicating virtual machines.
Docker Architecture
Docker employs a client-server architecture with a set of components that work together
to facilitate the containerization process. Understanding these components is crucial for effectively
working with Docker.
• Docker Daemon:
The Docker daemon (dockerd) is a background process that manages Docker containers on a host
system. It is responsible for building, running, and managing containers. The daemon listens for Docker API
requests and communicates with the Docker CLI (Command Line Interface) or other tools.
• Docker Client:
The Docker client (docker) is a command-line tool or API that allows users to interact with the Docker
daemon. It serves as the primary interface for developers to build, manage, and deploy containers. The client
sends commands to the Docker daemon, which then executes them.
• Docker Images:
Docker images are lightweight, standalone, and executable packages that include application code,
libraries, dependencies, and the runtime environment. Images serve as the blueprint for creating containers.
They can be stored in registries like Docker Hub and shared across different environments.
• Docker Containers:
Containers are instances of Docker images that run as isolated processes on a host system. They
encapsulate the application and its dependencies, providing a consistent and reproducible environment.
Containers are portable and can run on any system that supports Docker.
• Docker Registry:
A Docker registry is a centralized repository for storing and distributing Docker images. Docker Hub is
a public registry, but organizations often use private registries for hosting proprietary images. Registries
facilitate the sharing and versioning of Docker images across teams and environments.
• Docker Compose:
Docker Compose is a tool for defining and running multi-container Docker applications. It uses a
YAML file to configure the services, networks, and volumes required for an application. Compose simplifies the
process of managing complex, multi-container setups.
• Docker Networking:
Docker provides networking capabilities to enable communication between containers and external
networks. Containers can be connected to user-defined bridge networks, overlay networks for multi-host
communication, or the host network for direct access.
• Docker Volumes:
Docker volumes are used to persist data generated by and used by Docker containers. Volumes
provide a way to share data between container instances and with the host system. They are crucial for
handling data that needs to survive the lifecycle of containers.
• Dockerfile:
Dockerfile is a text file that contains instructions for building a Docker image. It specifies the base
image, sets up the environment, installs dependencies, and defines how the application should run. Dockerfiles
are used in the image-building process.
• Docker Swarm (Optional):
Docker Swarm is Docker's native orchestration tool for managing and scaling a cluster of Docker
nodes. It enables the deployment of multi-container applications across multiple hosts, providing features for
load balancing, service discovery, and rolling updates.
Docker Installation and Basic Commands
Installing Docker on Linux:
a. Update Package Index:
• bashCopy code
• sudo apt-get update
b. Install Docker Dependencies:
• bashCopy code
• sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
c. Add Docker GPG Key:
• bashCopy code
• curl -fsSL [Link] | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-
[Link]
d. Set Up Stable Docker Repository:
• bashCopy code
• echo "deb [signed-by=/usr/share/keyrings/[Link]] [Link]
$(lsb_release -cs) stable" | sudo tee /etc/apt/[Link].d/[Link] > /dev/null
e. Install Docker:
• bashCopy code
• sudo apt-get update sudo apt-get install docker-ce docker-ce-cli [Link]
f. Verify Docker Installation:
• bashCopy code
• Installing Docker on Windows
a. Download Docker Desktop for Windows:
• Download and install Docker Desktop from the official Docker website.
b. Run Docker Desktop Installer:
• Follow the installation wizard to complete the installation.
c. Verify Docker Installation:
• Open a command prompt and run:
• bashCopy code
Basic Docker Commands
a. Pull Docker Image:
bashCopy code
docker pull image_name
b. List Docker Images:
• docker imagesbashCopy code
•
c. Run Docker Container:
• bashCopy code
• docker run -it image_name
d. List Running Containers:
• bashCopy code
• docker ps
e. List All Containers (including stopped ones):
• bashCopy code
• docker ps -a
f. Stop a Running Container:
• bashCopy code
• docker stop container_id
• g. Remove a Container:
• bashCopy code
• docker rm container_id
h. Execute Command in a Running Container:
• bashCopy code
• docker exec -it container_id /bin/bash
Provisioning
• Docker provides a mechanism for provisioning through Dockerfiles. A Dockerfile is a script that
contains instructions for building a Docker image. Here's a simple example:
• DockerfileCopy code
• Build the Docker image using:
• bashCopy code
• docker build -t image_name .
• This Dockerfile demonstrates how to create a Docker image, set up a working directory, copy
application files, install dependencies, expose a port, and define the command to run the
application.
• Docker's provisioning capabilities make it easy to create reproducible and portable environments
for your applications.
Docker Hub and Container Operations
1. Docker Hub: Downloading Docker Images
Docker Hub is a cloud-based registry service that allows you to store and share Docker images.
To download Docker images from Docker Hub, you can use the docker pull command:
bashCopy codedocker pull username/image_name:tag
Replace username, image_name, and tag with the appropriate values.
2. Uploading Images to Docker Registry
a. Tagging the Docker Image:
bashCopy codedocker tag local_image_name:tag username/registry_name:tag
b. Logging into Docker Registry:
bashCopy codedocker login -u username -p password registry_name
c. Pushing the Image to Docker Registry:
bashCopy code
3. AWS ECS (Elastic Container Service):
AWS ECS is a fully managed container orchestration service. To
deploy Docker containers on ECS:
a. Create an ECS Task Definition:
• Define container settings, environment variables, and resource requirements.
b. Create an ECS Cluster:
• Define the cluster to run your containers.
c. Run a Task on ECS:
• Launch the task on the cluster to start your container.
4. Understanding Containers:
A container is an isolated, lightweight execution environment for applications. Containers share the host OS kernel but run as isolated processes.
5. Running Commands in a Container:
a. Start an Interactive Shell in a Running Container:
bashCopy code
_id /bin/bash
b. Run a Single Command in a Running Container:
bashCopy code
6. Running Multiple Containers:
a. Using Docker Compose:
Create a [Link] file defining multiple services:
yamlCopy codeversion: '3' services: web: image: nginx app: image: your_app_image
b. Running Compose:
bashCopy code
This will start both containers defined in the [Link] file.
c. Scaling Services:
bashCopy codempose up --scale app=3
This will scale the app service to run three instances. Running multiple containers is simplified with Docker Compose, allowing
you to define, configure, and scale multiple services in a single file.
By leveraging Docker Hub for image distribution and AWS ECS for orchestration, you can efficiently manage and scale your
containerized applications, ensuring seamless deployment and operation. Understanding Docker commands and container operations is
crucial for effective container management and development workflows.
What is Docker Networking?
Docker networking enables a user to link a Docker container to as many networks
as he/she requires. Docker Networks are used to provide complete isolation for
Docker containers.
Advantages of Docker Networking
Some of the major benefits of using Docker Networking are:
They share a single operating system and maintain containers in an isolated
environment.
It requires fewer OS instances to run the workload.
It helps in the fast delivery of software.
It helps in application portability.
How Does Docker Networking Work?
• For a more in-depth understanding, let’s have a look at how Docker Networking
works.
• Docker File builds the Docker Image.
• Docker Image is a template with instructions, which is used to build Docker
Containers.
• Docker has its own cloud-based registry called Docker Hub, where users store and
distribute container images.
• Docker Container is an executable package of an application and its dependencies
together.
Types of Network Drivers
• bridge: If you build a container without specifying the kind of driver, the container will
only be created in the bridge network, which is the default network.
• host: Containers will not have any IP address they will be directly created in the system
network which will remove isolation between the docker host and containers.
• none: IP addresses won’t be assigned to containers. These containments are not accessible
to us from the outside or from any other container.
• overlay: overlay network will enable the connection between multiple Docker demons
and make different Docker swarm services communicate with each other.
• ipvlan: Users have complete control over both IPv4 and IPv6 addressing by using the
IPvlan driver.
• macvlan: macvlan driver makes it possible to assign MAC addresses to a container.
Docker - Container Linking
Container Linking allows multiple containers to link with each other. It is a better option than exposing
ports. Let’s go step by step and learn how it works.
Step 1 − Download the Jenkins image, if it is not already present, using the Jenkins pull command.
Step 2 − Once the image is available, run the container, but this time, you can specify a name to the container by
using the –-name option. This will be our source container.
Step 3 − Next, it is time to launch the destination container, but this time, we will link it with our source
container. For our destination container, we will use the standard Ubuntu image.
• When you do a docker ps, you will see both the containers running.
Step 4 − Now, attach to the receiving container.
Then run the env command. You will notice new variables for linking with the source container.
Summary
• Docker is a containerization platform that enables developers to
package and distribute applications and their dependencies as
lightweight, portable containers.
• Docker vs. Virtualization
• Docker Architecture: