Skip to main content

Command Palette

Search for a command to run...

Docker

Published
8 min readView as Markdown

Module-4: Docker Assignment

  1. Explain the difference between Virtualization, Containerization and Microservices?

    Virtualization:

    • Purpose: Creates virtual machines (VMs) that act as independent computers with their own operating systems (OS).

    • Isolation: VMs are fully isolated from each other and the host hardware, ensuring security and resource allocation.

    • Resources: VMs are resource-intensive, requiring their own CPU, memory, disk space, and other resources.

    • Use cases: Hosting multiple operating systems on a single server, running legacy applications, isolating development environments.

Containerization:

  • Purpose: Packages applications with all their dependencies and libraries into containers that share the host's operating system.

  • Isolation: Containers are isolated from each other using namespaces, cgroups, and other mechanisms, but share the underlying OS.

  • Resources: Containers are more lightweight than VMs, requiring fewer resources and booting faster.

  • Use cases: Deploying multiple applications on a single server, microservices architecture, continuous integration/continuous delivery (CI/CD) pipelines.

Microservices:

  • Purpose: Breaks down applications into smaller, independent, and loosely coupled services.

  • Focus: Each microservice has its own functionality, codebase, deployment, and scaling process.

  • Communication: Microservices communicate through APIs, allowing independent development and deployment.

  • Benefits: Increased agility, scalability, and resilience.

Here's a table summarizing the key differences:

FeatureVirtualizationContainerizationMicroservices
PurposeCreate isolated virtual machinesPackage applications with dependenciesBreak down applications into smaller services
IsolationFull isolation of VMs and host hardwareIsolation of containers on shared OSIndependent microservices communicate through APIs
ResourcesResource-intensiveMore lightweight than VMsVary depending on the service
Use casesMultiple OS, legacy apps, development environmentsMultiple applications, microservices, CI/CDIncreased agility, scalability, resilience

Relationship between them:

  • Virtualization can be used to create a platform for running containers or deploying microservices.

  • Containerization can be used to package and deploy microservices.

  • Microservices can be deployed on VMs or containers.

  1. Define Docker Engine, Docker Image, Docker File, Docker Compose? Describe the lifecycle of Docker Container.

    Docker Engine:

    • A software platform that installs on your computer and manages Docker containers.

    • It provides the runtime environment and tools to build, run, and manage containers.

    • Docker Engine includes a daemon that listens for API requests and manages container creation, execution, and cleanup.

Docker Image:

  • A read-only template used to create Docker containers.

  • It contains all the necessary dependencies and libraries needed to run an application.

  • Images are built using Dockerfiles, which specify the instructions for creating the image layer by layer.

  • Images are portable and can be shared and run on any system with Docker Engine installed.

Dockerfile:

  • A text file containing instructions for building a Docker image.

  • Each line in the Dockerfile specifies a command that is executed when the image is built.

  • Dockerfiles include commands for installing software, setting environment variables, copying files, and running scripts.

  • Dockerfiles are used to create consistent and repeatable images that can be easily shared and deployed.

Docker Compose:

  • A tool used to define and run multi-container applications.

  • It uses a YAML file to define the services, networks, and volumes for your application.

  • Compose allows you to start, stop, and manage all the containers in your application with a single command.

  • It simplifies the deployment and management of complex applications composed of multiple containers.

Docker Container Lifecycle:

  1. Image Pull: The Docker Engine downloads the desired image from a registry (e.g., Docker Hub) if it's not already available locally.

  2. Container Creation: The Docker Engine unpacks the image and creates a container from its layers.

  3. Container Start: The Docker Engine starts the container's process and assigns resources (CPU, memory, etc.).

  4. Running: The application runs inside the container, isolated from the host system and other containers.

  5. Stop/Pause: The Docker Engine can stop or pause the container, allowing it to be restarted or resumed later.

  6. Kill/Remove: The Docker Engine can kill the container process and remove the container object.

  7. Image Removal: The Docker Engine can remove the image locally, freeing up disk space.

  1. Explain the different Docker Components - Docker Client, Docker Host and Docker Registry.

    Docker Components:

    Docker consists of several components that work together to create, run, and manage containers. Here's a breakdown of the key components:

    1. Docker Client:

    • Function: The user interface for Docker.

    • Purpose: Interacts with the Docker daemon through a REST API to send commands and receive responses.

    • Location: Typically installed on your local machine.

    • Responsibilities:

      • Building, running, stopping, and removing containers.

      • Pulling and pushing images to and from registries.

      • Managing networks, volumes, and other Docker resources.

2. Docker Daemon:

  • Function: The backend service that handles Docker commands and manages container operations.

  • Purpose: Listens for API requests from the Docker client and executes them.

  • Location: Runs on the same host as the Docker client or a remote host.

  • Responsibilities:

    • Creating and starting containers from Docker images.

    • Managing the lifecycle of containers (start, stop, kill, remove).

    • Maintaining the state of containers and their resources.

    • Communicating with Docker registries to pull and push images.

3. Docker Registry:

  • Function: A server that stores and distributes Docker images.

  • Purpose: Allows users to share and discover public and private Docker images.

  • Types:

    • Public: Open to anyone, most commonly used is Docker Hub.

    • Private: Hosted by organizations or individuals for internal use.

  • Responsibilities:

    • Storing and managing Docker images.

    • Providing access control for image sharing.

    • Performing security scans on uploaded images.

  1. Explain with an example, about how you would push a local image to DockerHub and AWS ECR? (Complexity - Simple)

  2. Explain the Docker file statements - COPY, ADD, CMD, RUN, ENTRYPOINT

    COPY:

    • Function: Copies files and directories from the host machine to the container's filesystem.

    • Syntax: COPY <source> <destination>

    • Example: COPY . /app copies all files and directories from the current directory to the /app directory inside the container.

2. ADD:

  • Function: Similar to COPY, but also supports extracting archives and remote URLs.

  • Syntax: ADD <source> <destination>

  • Example: ADD https://example.com/file.tar.gz /app/ downloads the file, extracts it, and copies the extracted content to the /app directory.

3. CMD:

  • Function: Specifies the default command to execute when the container starts.

  • Syntax: CMD ["executable", "parameter1", "parameter2"] or CMD command parameter1 parameter2

  • Example: CMD ["/bin/bash"] starts the container with a bash shell.

4. RUN:

  • Function: Executes commands during the image build process.

  • Syntax: RUN <command>

  • Example: RUN apt-get update && apt-get install -y nginx installs the nginx package on the image.

5. ENTRYPOINT:

  • Function: Defines the main process that the container will run.

  • Syntax: ENTRYPOINT ["executable", "parameter1", "parameter2"] or ENTRYPOINT command parameter1 parameter2

  • Example: ENTRYPOINT ["/usr/sbin/nginx"] starts the nginx server when the container starts.

Key Differences:

  • CMD vs. ENTRYPOINT:

    • ENTRYPOINT is executed first, and its arguments cannot be overridden when running the container.

    • CMD is used as a default command and can be overridden with arguments when running the container.

  • COPY vs. ADD:

    • COPY only copies files and directories.

    • ADD supports extracting archives and remote URLs.

  1. Create a Docker Volume Container and attach it to two different nginx containers. (Complexity - Simple)

  2. Create a Docker File for a Python based flask application. Execute the same using Jenkins Pipeline. You should have a :5000 flask webpage running. (Complexity - Medium)

  3. Create and deploy Wordpress application using Docker Compose file. Create a Jenkins Job to execute your docker compose file and the output should show the ip-address of Wordpress application.(Complexity - Medium)

  4. Create a Docker Multistage build for Python based application.

  5. Explain the different Docker networking and the significance of each mode.

    Docker Networking Modes and their Significance:

    Docker offers various networking modes to configure how containers interact with the network and each other. Choosing the appropriate mode depends on your specific needs and application requirements.

    Here's a breakdown of the different Docker networking modes and their significance:

    1. Bridge (default):

    • Function: Creates a virtual bridge network that connects the containers to each other and the host network.

    • Significance:

      • Provides isolation between containers and the host network.

      • Allows containers to communicate with each other through DNS names or IP addresses.

      • Offers a good balance between isolation and connectivity for most applications.

2. Host:

  • Function: Shares the host's network namespace with the container.

  • Significance:

    • Provides the container with the same network identity as the host.

    • Enables direct access to host resources like network interfaces and ports.

    • Useful for applications requiring full access to the host network, but not recommended for production environments due to security implications.

3. None:

  • Function: Disables the network stack for the container.

  • Significance:

    • Creates isolated containers with no access to the network.

    • Useful for debugging purposes or when network access is not required.

4. Overlay:

  • Function: Creates a virtual network that spans multiple Docker hosts.

  • Significance:

    • Enables communication between containers across different hosts in a Docker swarm.

    • Offers scalability and fault tolerance for distributed applications.

5. Macvlan:

  • Function: Assigns a dedicated MAC address and IP address from the host's network to the container.

  • Significance:

    • Provides finer-grained control over the container's network configuration.

    • Useful for integrating containers with existing network infrastructure.

6. Custom Bridge:

  • Function: Allows creating custom bridge networks with specific configurations.

  • Significance:

    • Enables fine-tuning network settings like DNS servers, IP ranges, and subnet masks.

    • Useful for advanced network configurations and specific application requirements.