What is Docker with examples

What Is Docker
Docker is an open-source platform to create, deploy, and run applications by using containers.it is open-source software that helps us to create a wrapper around the file of a code, the OS, and the libraries which are required to run a code and do not need to be in GB a container could be as low as 50MB.
Now you have a question, what is a container?
What is Container:

The container is an operating system level virtualization method used to deploy and run the distributed application without fetching and entire virtual machine(OS) for each app.
In other words, the Container is a lightweight, portable and self-contained environment that can run applications and their dependencies in a consistent and reproducible way across different environments. Containers provide an isolated runtime environment for applications to run, ensuring that they do not interfere with other applications running on the same host.
Containers achieve this isolation by using a combination of kernel-level technologies, including namespaces, cgroups, and container images. Namespaces provide process isolation, cgroups limit the resources available to a container, and container images define the application and its dependencies.
Each container runs as an isolated process on the host operating system, but it has its own file system, network interfaces, and other resources that are separate from those of other containers and the host system. This means that multiple containers can run on the same host without interfering with each other.
Why do we need Docker: Suppose, there is a developer who developed a code and sends it to another person to check or for any testing purpose and then the second person comes and said this code is not working on my machine and says this code is faulty but same code is working on developer's machine here both are right at their places so to resolve this issue we need a wrapper or a container which contain all required libraries and OS and should work anywhere and portable here Docker comes in the picture who can provide all the facilities in the form of container which has all required software and OS libraries which are required to run a software or a file.
Docker Architecture.

Docker architecture is comprised of several components that work together to create, deploy, and run applications in containers. These components include:
Docker daemon: The Docker daemon is the core component of the Docker architecture. It runs on the host operating system and manages the lifecycle of containers, including their creation, starting, stopping, and deletion. The Docker daemon communicates with the Docker client, which sends commands to the daemon to manage containers and images.
Docker client: The Docker client is the command-line interface (CLI) that allows users to interact with the Docker daemon. The client sends commands to the daemon to create, run, and manage containers and images. The client can run on the same machine as the daemon, or it can connect to a remote daemon running on a different machine.
Docker registries: Docker registries are centralized repositories for storing and sharing Docker images. Docker Hub is the default public registry provided by Docker, but users can also set up their own private registries to store and share their own images.
Docker images: Docker images are read-only templates that define the application and its dependencies. They contain the necessary files, libraries, and configurations to run the application. Users can create their own images or use existing images from Docker Hub or other registries.
Docker containers: Docker containers are lightweight, portable, and self-contained environments that run applications and their dependencies. Each container is created from an image and has its own file system, network interfaces, and other resources that are isolated from other containers and the host system.
Overall, Docker architecture provides a powerful platform for creating, deploying, and running applications in containers, making it easier to develop, test, and deploy software across different environments.
Hands-on Practices
Now, Let's jump to our terminal to do hands-on and try to understand in the real world.
If you have Ubuntu OS on your machine you can use the same machine to practice otherwise you can use an EC2 instance.
Our first step is to install Docker, very easy!!
sudo apt-get update
sudo apt get install docker.io
After installing the docker check the service status by the below command.
systemctl status docker
// You will get similar kind of output
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2023-04-12 00:40:58 IST; 1 day 15h ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 1439 (dockerd)
Tasks: 30
Memory: 65.2M
CGroup: /system.slice/docker.service
├─ 1439 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
├─13087 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8000 -container-ip 172.17.0.3 -container-port 8000
└─13093 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 8000 -container-ip 172.17.0.3 -container-port 8000
Now I want to see is there any container running right now.
docker ps
// Here is result , it mean there is no container is running //right now
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Note: If you are getting a permission denied error while running docker ps please use below command to allow permissions to your user.
sudo usermod -a -G docker $USER
//here usermod is command to give permission,- a refer append an individual user , -G refer group , $USER refer your current user
Note: Reboot required after the above command
Now I want to install MySQL on my docker.
What Docker will do, He will pick an image of MySQL from the DockerHub and run it.
How? Let's see here...
$ docker pull mysql
To see the image we use the command docker images.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 412b8cc72e4a 6 days ago 531MB
Now I want to run MySQL but it will not run yet and I have to run the image to use MYSQL.
docker run -d -e MYSQL_ROOT_PASSWORD=Test@123 mysql:latest
// docker run is a command to run image
//-d is to run in demon mode
//-e MYSQL_ROOT_PASSWORD=Test@123 to define root password
//mysql:latest image name
Done........
To see the running containers.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
77ebad39199a mysql:latest "docker-entrypoint.s…" 2 hours ago Up 2 hours 3306/tcp, 33060/tcp vigilant_murdock
Now I want to use MySQL here is the very easy command.
$ docker exec -it 77ebad39199a bash
bash-4.4# mysql -u root -p // we given above
Enter password: // we given above
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Now you are on MySQL and can do whatever you want.
I hope you are now confident in Docker,Container, Images, Run Images.
Thank you for your time!!
If you like my article please hit the Heart and follow me on Linkedln at https://www.linkedin.com/in/saif-ali-53399914b/
Happy Learning!!
Saif Ali



