What is docker?
Docker is an open-source platform that enables developers to build, ship, and run applications in lightweight, portable, and isolated environments called containers. Containers package an application along with all its dependencies (libraries, binaries, and configuration files), ensuring it runs consistently across different computing environments.
Key Features of Docker
Containerization: Encapsulates applications and their dependencies into containers, which are isolated and portable.
Portability: Containers can run on any system that supports Docker, ensuring a consistent environment across development, testing, and production.
Resource Efficiency: Containers share the host operating system kernel, making them more lightweight and faster than virtual machines.
Scalability: Docker makes it easy to scale applications horizontally by running multiple containers.
Version Control: Using Docker images, you can version and roll back application updates.
Why Use Docker?
Consistency: Ensures applications work the same way in development, staging, and production environments.
Speed: Containers start in seconds, allowing rapid development and testing cycles.
Isolation: Containers run independently, preventing conflicts between applications.
Resource Optimization: Containers use fewer resources compared to virtual machines since they don’t require a full operating system.
Ecosystem: Docker provides a vast library of pre-built images via Docker Hub.
How Docker Works
Docker Images:
A read-only template containing the application and its dependencies.
Built using a
Dockerfile
, which specifies the steps to assemble the image.
Docker Containers:
A running instance of a Docker image.
Isolated environments where the application runs.
Docker Engine:
- The runtime environment that manages containers.
Docker Hub:
- A cloud-based registry for sharing Docker images.
Use Cases of Docker
Microservices Architecture: Simplifies building and deploying services independently.
Continuous Integration/Continuous Deployment (CI/CD): Automates testing, building, and deployment pipelines.
Development Environment: Quickly set up consistent environments for developers.
Application Modernization: Migrate legacy apps into containers for better performance.
Testing and Debugging: Test applications in isolated environments without affecting the host system.
Pre-requisite to complete the below tasks
Install docker in the system
Update the existing package list
sudo yum update -y
Install required dependencies
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
Add docker official repository
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Install docker
sudo yum install -y docker-ce docker-ce-cli containerd.io
Start and Enable Docker
sudo systemctl start docker
sudo systemctl enable docker
Verify Installation
docker --version
Post Installation Step
Allow Non-Root User to Use Docker : This is required to run docker as non-root user
sudo usermod -a -G docker $user
Reboot your system and log in again, go to /etc/group and check whether your user is added to docker or not
Tasks
Use the
docker run
command to start a new container and interact with it through the command lineUse the
docker inspect
command to view detailed information about a container or imageUse the
docker port
command to list the port mappings for a containercommand syntax : docker port container-name/container id
The
hello-world
container does not expose any ports. This is why even if thedocker port
command runs successfully, it won't return any output. Thehello-world
container simply runs a short-lived command and exits without requiring port mappingUse the
docker stats
command to view resource usage statistics for one or more containersUse the
docker top
command to view the processes running inside a containercommand syntax : docker top container id/container name
Use the
docker save
command to save an image to a tar archiveUse the
docker load
command to load an image from a tar archive