Docker Part 1
What is Docker ?
Docker is a platform that allows you to package an application and all its dependencies into lightweight, portable containers.
Why use Docker ?
Docker simplifies application deployment by packaging everything into lightweight portable containers.
What is the difference between a container and a VM(Virtual Machine)?
Containers are lightweight because they share the host operating system , while Virtual machines are heavy because they run their own full Operating System.
Containers start very quickly , use fewer resources and are ideal for microservices. Virtual machines are provide stronger isolation but require more Ram and CPU since each VM containers a full OS.
So overall, containers are fast and efficient , whereas VMs are heavier but more isolated.
What is Container ?
Containers are lightweight, isolated environments that package an application along with all its dependencies so it run consistently on any system.
They share the Host OS kernel, so they are faster and more efficient than Vm.
What are the main components of Docker ?
The main components of Docker are:
1. Docker Engine:- The core service that runs and manage containers.
2. Docker Daemon(Dockerd):- The background process that builds, runs and monitors containers.
3. Docker CLI:- Command line tool used to interact with docker.
4. Docker Image:- Read only templates used to create containers.
5. Docker Containers:- Running instances of Docker image.
6. Docker Registry:- A repository where docker images are stored and pulled form.
Docker engine is the core component of docker that runs and manages containers.
IT works a client-server application where the docker daemon creates, run and monitors containers and the docker CLI is used to interact with it. It basically provides the runtime environment needed to build, run and manage containers on a system.
What is a Docker Image ?
A docker mage is a read only blueprint used to create containers. It contains the application code, dependencies and environment required to run the application.
What is Docker Host ?
A docker host is a machine (physical or virtual) on which docker is installed and where docker containers run.
Docker Installation on Ubuntu (Step-by-Step)
Step 1: System Update
sudo apt update && sudo apt upgrade -y
Step 2: Install Required Packages
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
Step 3: Add Docker’s Official GPG Key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg - dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Step 4: Add Docker Repository
echo \
"deb [arch=$(dpkg --print-architecture) signed by=/usr/share/keyrings/docker-archive-keyring.gpg] \ https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Step 5: Update the Package Index
sudo apt update
Step 6: Install Docker Engine
sudo apt install docker-ce docker-ce-cli containerd.io -y
Step 7: Verify Docker Installation
sudo docker –version
https://youtu.be/IWk5c7FtI2o
https://youtu.be/Ron35IfSoNw
Docker all Commands:-
1. docker --version --> check docker verson
2. docker version --> check client & host version
3. docker container run -d image name --> create a container
4. docker container ls --> list a containers
5. docker container ls -a --> list all containers
6. docker image ls --> list images
7. docker container stop container name --> stop a container
8. docker container start stop container name --> start a stop contaner
9. docker container rm container name --> delete a stop container
10. docker container rm -f container name --> delete a running container
11. docker container run -itd OS image name --> create a OS Container
12. docker container run -d --name web image name --> naming the container
13. docker container exec -it container name bash --> going inside the container
14. docker container inspect container name --> show the complete details of the container
15. docker container stats --> show container details
16. docker container pause container name --> pause a container
17. docker container unpause container name --> unpause a container
18. docker container logs container name --> show container logs
19. docker container prune --> to delete all stop container
20. docker container cp source file container name:/usr/local/apache2/htdocs --> copy a file inside container
21. docker container run -d -p 80:80 image name --> port a container
In docker, a restart policy controls whether and when a container should automatically restart after it stop or if the docker daemon restart.
Types of Restart Policy:-
1. no (default)
2. always policy
3. on-failure policy
4. unless-stopped policy
no policy :- Don't automaticlly restart the container.(default)
on-failure policy:- on-failure policy restarts a docker container only when a carshes or exits whit an a error.
Command:- docker container run -d --restart on-failure httpd
always policy:- Always policy ensures a Docker container is always running by restarting it whenever it stops.
Command:- docker container run -d --restart always httpd
unless-stopped:- Unless-stopped policy restarts a Docker container in all cases except when it is stopped manually.
Command:- docker container run -d --restart unless-stopped httpd
Comments
Post a Comment