Docker and Docker Swarm: Containerization (June 13)
Introduction: Docker, a powerful open-source platform released in March 2013, has revolutionized the way developers create, deploy, and run applications. Developed by Solomon Hykes and Sebastien Paul, Docker leverages containerization to enable efficient application execution across different operating systems without the need for full virtualization. In this blog post, we will explore the basics of Docker, its advantages and disadvantages, its architecture, and important commands for installation and usage.
What is Docker? Docker is a centralized platform that facilitates OS-level virtualization, also known as containerization. It allows applications to run using the same Linux Kernel as the host computer, eliminating compatibility issues faced by developers and users. Docker is particularly useful in ensuring consistent application behavior across different systems.
Advantages of Docker:
Resource efficiency: Docker dynamically allocates resources based on application needs, optimizing RAM allocation and reducing wastage.
Continuous integration (CI) efficiency: Docker allows developers to build a container image once and use it across various stages of the deployment process, ensuring consistency and speeding up development cycles.
Cost-effective: Docker's lightweight approach minimizes infrastructure requirements, resulting in reduced costs.
Portability: Docker can run on physical hardware, virtual hardware, or in the cloud, providing flexibility in deployment options.
Image reusability: Docker images can be reused across different environments, making it easier to replicate and deploy applications.
Rapid container creation: Docker enables quick container creation, saving time during development and deployment processes.
Disadvantages of Docker:
Limited GUI support: Docker is not suitable for applications that heavily rely on graphical user interfaces (GUIs).
Container management complexity: Managing a large number of containers can be challenging, requiring careful orchestration and monitoring.
Lack of cross-platform compatibility: Docker containers designed for specific operating systems (e.g., Windows or Linux) cannot run on incompatible systems without additional configuration or adjustments.
Different OS requirements: When the development and testing operating systems differ, using virtual machines may be more appropriate than Docker containers.
Data recovery and backup: Docker does not provide built-in solutions for data recovery and backup, requiring additional measures to ensure data resilience.
Understanding Docker's Architecture: Docker's architecture comprises several key components:
Docker Daemon: The Docker daemon runs on the host operating system and is responsible for managing containers, running applications, and coordinating Docker services. It can communicate with other daemons.
Docker Client: Docker users interact with the Docker daemon through the Docker client. The client uses commands and REST API to communicate with the daemon, allowing users to manage containers, images, and other Docker resources.
Docker Host: The Docker Host provides the environment to execute and run applications. It encompasses the Docker daemon, images, containers, networks, and storage.
Docker Hub/Registry: Docker Hub is a public registry that manages and stores Docker images. It allows users to search for and download images. Private registries are also available for enterprise use, enabling image sharing within an organization.
Docker Images: Docker images are read-only binary templates that serve as the foundation for creating Docker containers. They encapsulate all dependencies and configurations required to run an application.
Docker Containers: Docker containers are lightweight, isolated runtime instances created from Docker images. Containers hold everything necessary to run an application, including the application code, runtime, libraries, and system tools.
Installation and Important Commands: To install Docker, follow these steps:
Create a machine on AWS with Docker installed AMI or install Docker if not already installed using the command:
yum install docker.
Verify the installation by checking the local images:
Search for images on Docker Hub:
docker search <image_name>.
Download an image from Docker Hub to the local machine:
docker pull <image_name>.
Give a name to the container and run it:
docker run -it --name <container_name> <image_name> /bin/bash(use
-itfor interactive mode and direct access to the terminal).
Manage Docker services:
service docker status,
service docker start,
service docker stop.
docker start <container_name>,
docker stop <container_name>,
docker ps -a(view all containers),
docker ps(view only running containers).
Remove containers and images:
docker rm <container_name>,
docker image rm <image_name>.
Dockerfile Creation: To create a Docker image using an existing Dockerfile, follow these steps:
Create a container from the image:
docker run -it --name <container_name> <image_name> /bin/bash.
Make changes within the container, such as creating files or installing dependencies.
Inspect the differences between the base image and the modified container:
docker diff <container_name>.
Commit the changes to create a new image:
docker commit <container_name> <container_image_name>.
Create a new container from the updated image:
docker run -it --name <container_name> <updated_image_name> /bin/bash.
Docker has revolutionized the way applications are deployed and managed by providing a lightweight, portable, and efficient solution. In this blog post, we will explore Dockerfile creation and the use of Docker volumes, which play a crucial role in decoupling containerized applications from storage.
Dockerfile is a text file that contains a set of instructions for automating the creation of a Docker image. Let's go through some key instructions used in Dockerfile:
FROM: Specifies the base image for the Docker image. This command must be at the top of the Dockerfile.
RUN: Executes commands and creates a new layer in the image.
MAINTAINER: Specifies the author, owner, or description of the Dockerfile.
COPY: Copies files from the local system (dockerVM) to the image. It requires source and destination paths.
ADD: Similar to COPY, it allows downloading files from the internet and extracting files within the image.
EXPOSE: Exposes specific ports, such as port 8080 for Tomcat or port 80 for Nginx.
WORKDIR: Sets the working directory for the container.
CMD: Executes commands during container creation.
ENTRYPOINT: Similar to CMD but has higher priority. The first commands specified in ENTRYPOINT will be executed.
ENV: Sets environment variables within the container.
ARG: Defines the name and default value for a parameter. ARG values cannot be accessed after running the Docker container.
Now, let's create a Dockerfile and build an image from it:
Create a file named Dockerfile.
Add instructions to the Dockerfile.
Build the Dockerfile to create an image.
Run the image to create a container.
Docker volumes provide a way to persist data generated or used by containers. They decouple containers from storage, enabling easy data sharing and ensuring data availability even if containers are stopped or restarted. Let's explore some important aspects of Docker volumes:
Volume Creation: Volumes are created while creating the container and can be shared across multiple containers. You cannot create a volume from an existing container.
Container-to-Container Volume Sharing: Volumes can be shared between containers, allowing data sharing and synchronization.
Host-to-Container Volume Sharing: Volumes can also be mapped from the host machine to the container, allowing files from the host to be accessible within the container.
Now, let's create and work with Docker volumes:
Create a Dockerfile with the following contents: FROM ubuntu VOLUME ["/myvolume1"]
Build an image from the Dockerfile using the command: docker build -t <image_name> .
Create a container from the image and access the container's shell: docker run -it --name <container_name> <image_name> /bin/bash
Inside the container, navigate to the volume directory: cd /myvolume1
Share the volume with another container using the volumes-from flag: docker run -it --name <container_name> --privileged=true --volumes-from <container_name> <os_image_name_ex-ubuntu> /bin/bash
Now, any changes made in one volume will be visible in the other volume.