Docker for DevOps Engineers

Docker for DevOps Engineers

1. What is Docker?

Docker is a platform that allows you to package and run applications in isolated environments called containers.

Eg: Developers use Docker to create a container for a web application, including the web server, database, and necessary dependencies. This container can then be easily shared, deployed, and run on different systems without worrying about compatibility issues.

Eg. All Different items will be shifted in one box, so nothing is left behind.

2. Docker Container VS Virtual Machine:

Docker ContainerVM
Isolation: Containers provide lightweight process-level isolation, allowing multiple containers to run on a single host while sharing the same OS kernel.Isolation: Virtual Machines (VMs) provide full OS-level isolation, allowing multiple VMs to run on a single host, each with its own OS instance.
Resource Usage: Containers are more lightweight and efficient, as they share the host's OS and only require the resources necessary to run the application and its dependencies.Resource Usage: VMs are more resource-intensive, as they require a full OS instance and allocate resources like CPU, memory, and storage independently.
Boot Time: Containers have fast boot times, typically in seconds, as they leverage the host's OS and don't require the entire OS to start up.Boot Time: VMs have slower boot times, typically in minutes, as they need to boot an entire OS instance.
Portability: Containers are highly portable since they encapsulate the application and its dependencies into a single unit, making it easy to run the same container on different hosts or environments.Portability: VMs are less portable because they encapsulate an entire OS instance, requiring additional steps to migrate or run them on different hosts or cloud platforms.
Scaling: Containers can be easily scaled horizontally, allowing multiple instances of a containerized application to run concurrently on a single host or distributed across multiple hosts.Scaling: VMs can be scaled vertically by allocating more resources to a single VM instance or scaled horizontally by adding more VM instances.
Management: Docker provides a comprehensive set of tools and APIs for managing containers, automating deployments, and managing container lifecycles.Management: VMs require separate tools and management systems for provisioning, deploying, and managing the virtual infrastructure.
Use Cases: Docker containers are ideal for microservices architectures, DevOps workflows, and deploying lightweight applications that benefit from high scalability, portability, and rapid deployment.Use Cases: VMs are suitable for running multiple applications with different OS requirements, legacy applications, or when strong isolation between applications is necessary.

3. Installation of Docker

 sudo apt-get update
 sudo apt-get install docker.io -y
 # To check the version of installed Docker
 docker --version

Change permission:

 sudo usermod -aG docker $USER   
 # reboot to session
 sudo reboot

Docker status

docker ps

Docker Functions:

 # To stop a container 
 docker stop <container_id>
 # To remove a container
 docker rm <container_id>
 # start the docker service
 sudo systemctl start docker
 # stop the docker service
 sudo systemctl stop docker
 # restart the docker service
 sudo systemctl restart docker
 # check the status of the docker service
 sudo systemctl status docker
 # show the environment variables define in systemd file
 sudo systemctl show docker

Task 1: Installing MongoDB

docker pull mongo:latest
# to fetch latest file of mongo
docker run -d -p 27017:27017 -d mongo:latest
# -d for run the container in detached mode, in short: background me chal
# -p for ports ( Will find those ports on dockerhub
docker exec -it <container_id> bash
# to run the mongodb
Type: Magosh
# to enter mongoDB

Task 2: Install nginx

# Read task 1 to understand terminologies.
docker pull nginx
docker run -p 80:80 nginx:latest

Task 3: Stop and Remove the Applications

# to remove the application, you must stop the docker file first
docker stop <container-id>
docker rm <container-id>