Tuesday, September 20, 2022

Introduction to Docker - Installation & Basic Commands

docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub,programming,software development,technology
We will learn about Docker in this tutorial. We will go over the Docker components and concepts in brief, as well as the benefits of using Docker. We will also learn how to install Docker, pull Docker images from Docker Hub, run an image, and create a Docker container.


1. What is Docker

Docker is simply described on the Docker website as follows:
Docker is an open platform for developing, shipping, and running
applications. Docker enables you to separate your applications from your
infrastructure so you can deliver software quickly. With Docker, you can
manage your infrastructure in the same ways you manage your applications.
By taking advantage of Docker’s methodologies for shipping, testing, and
deploying code quickly, you can significantly reduce the delay
between writing code and running it in production.
In a nutshell, Docker allows us to separate our application code from the infrastructure on which it runs.

It accomplishes this by enabling us to create infrastructure or an isolated environment known as a container. As a result, our various applications will run in a variety of isolated environments.

In the age of microservices, this architecture - application containerization - is extremely beneficial to developers, as microservices divide business logic into small applications. As a result, developers can focus on developing business logic in applications rather than worrying about infrastructure. The system engineer or DevOps engineer will now set up the necessary infrastructure, such as a Docker Container, in which our application will run.

2. Why Docker

Every application begins on our local machine. Whatever runtime environment or dependencies are required for that development, we create or install them on our personal machine. After some initial development, when we deploy our application to a new machine or server, it does not run!

In the team meeting, a viral conversation will begin: "It works on my machine; I'm not sure why it isn't running there." - This is something that every developer (including myself!) would say. So, what went wrong? After several hours of debugging, we discovered that one of the dependencies is missing. So we need some technology or a mechanism to ensure that we don't forget any dependencies the next time we want to deploy the application in an unfamiliar environment.

This is where Docker comes in - Docker assists us in overcoming these situations by allowing us to create an isolated environment, a container, by installing the dependencies required to run that specific application - using some Docker commands - so our application runtime environment remains intact.

3. Docker Architecture

Docker's architecture is based on the traditional client-server model, with Docker Hub/Registry, Docker Client, and Docker Host components. In the client-server model of Docker architecture, we can consider the Docker client to be the CLIENT and the Docker daemon to be the SERVER. The Docker client passes instructions to the Docker daemon, which then pulls images from Docker Hub, builds a new container, or communicates between containers based on the instructions. If both the Docker client and the Docker daemon are running on the same machine, they communicate via REST API via UNIX sockets or a network interface.
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub

3.1 Docker Registry

The Docker registry is the repository for Docker images, from which we can download and save images. Docker Hub hosts public Docker images (for example, an Ubuntu image) that anyone can use (pull). Another type of registry is a private registry, which is used to share images within the Enterprise.

3.2 Docker Daemon

Docker daemon is the background process that receives and manages requests from the Docker API. The Docker daemon manages Docker images, containers, networks, and volumes.

3.3 Docker Client

The Docker client relays our commands to the Docker daemon. Docker client provides a command line interface (CLI) through which we can issue commands to the Docker daemon such as pull, run, start, and stop. If we use the command docker run, the Docker API will execute the command on the Docker daemon. Docker client can communicate with multiple Docker daemons.

3.4 Docker Host

Docker Host includes the docker daemon as well as images, containers, network, and storage. It provides an environment in which we can manage images and run containers.

3.5 Docker Images

Images are read-only binary templates that contain instructions for creating Docker containers. An image is created by modifying existing images. An image is made up of several layers. We can use Dockerfile to create our own image.      

3.6 Docker Containers

Images are used to make containers. A container is essentially the running state of an image. Docker Container can be thought of as a Box or a Logical Box that encapsulates the code/applications and their dependencies in order to run any specific application within the container. These containers are managed by the Docker Engine (Docker), which communicates with the Host Operating System (OS). Docker CLI is used to create, start, and stop containers. Because images are read-only, Docker creates a container by adding a read-write file system over the image's read-only file system.

Networking is part of Docker which is used to connect the Docker container to each other and the outside world so they can communicate with each other and also they can talk to Docker Host.

4. Install Docker in Ubuntu 20.04

We will learn how to install Docker on Ubuntu 20.04 in this section.      

Let's start with a standard apt-get update command:
sudo apt-get update
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub

Install the following prerequisites: ca-certificates, curl, apt-transport-https (allows our machine to communicate via HTTPS), and software-properties-common (contains some Docker-required packages):
sudo apt-get install ca-certificates curl apt-transport-https software-properties-common
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub
It’s done.

Now, download and install the Official Docker repository's GPG key on our machine:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub
That means our machine has the GPG key.

The following command will add the Docker repository to our machine's APT sources, as well as update our machine's package database so that Docker uses the correct files for Ubuntu 20.04 from our stable repository:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub

All set. We are now ready to install the most recent version of Docker (docker-ce).
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub
This will install Docker (docker daemon and client) on our local machine.

Check the Docker daemon status:
sudo service docker status
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub

We have successfully installed Docker on our machine, and the Docker daemon (service) and Docker client (CLI) are both operational. To test the Docker command line utility, enter the following command into the terminal:
sudo docker -v
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub

Now, in the terminal, enter the following command:
sudo docker info
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub
As we can see, the output of the above command displays detailed information about the Docker client and server.


5. Play with Docker Images and Containers

In this section, we will learn how to pull docker images from Docker Hub, how to run an image, and how to create a docker container.

5.1 Pull Image from Docker Hub

We can now pull a Docker image from Docker Hub into our machine and run it because the Docker daemon and client are running. So, in the terminal, enter the following command:
sudo docker pull hello-world
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub

Simply follow the output of the terminal after entering the above command. This command downloads the hello-world image from Docker Hub. Run the above command once more, and the result is:
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub
Can you spot the difference between the previous command's output and this one's?

When we re-run the command, there is no Pull complete message, and the Status message is "Image is up to date for hello-world:latest". In the first run, the Status message is "Downloaded newer image for hello-world:latest".

This means that the docker pull command checks to see if any images (with the image name hello-world) are present on the local machine, and if not, it pulls the image from Docker Hub. If the image is on our local machine, docker pull will NOT pull the image from Docker Hub, and docker pull will also update our local image if the updated image is available on Docker Hub.

5.2 List Docker Images

Let us check the following commands before running hello-world. The first command is as follows:
sudo docker image ls
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub
This command displays a list of all docker IMAGES on our local machine.

5.3 List Docker Containers

The second command is:
sudo docker ps -a
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub
This command displays a list of all docker CONTAINERS on our local machine. There is currently no container in our local machine.

5.4 Run Docker Image

Now enter the following command into the terminal to run the Docker IMAGE:
sudo docker run hello-world
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub

Please first read the output of the preceding command in the terminal. The command docker run creates a CONTAINER named hello-world from the IMAGE, then runs it and displays the message from the Docker CONTAINER.

If any image is not present on the local machine, the docker run command will pull it from Docker Hub and create a CONTAINER from it. As a result, we can use the docker run command instead of the docker pull command. 

Now list all of the CONTAINER on our local machine:
sudo docker ps -a
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub
As we can see, a new CONTAINER named youthful_wing was created from the IMAGE hello-world.

5.5 Remove Docker Container

Now we will see how to delete docker images and containers. To delete the docker container type the following command:
sudo docker rm youthful_wing
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub
We have passed the container name, youthful_wing, in the docker rm command to delete a container and it is successfully removed from our local machine.

5.6 Remove Docker Image

In the terminal, type the following command to delete the docker image:
sudo docker image rm hello-world
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub
We used the docker image rm command to delete an image and passed the image name, hello-world, and it was successfully removed from our local machine.

5.7 Create Docker Container with a Specific Name

We now have no docker container or image on our local machine because we deleted them all in the previous section. We'll get the Ubuntu image from Docker Hub. To pull the image, we will use docker run rather than docker pull. So, in the terminal, enter the following command:
sudo docker run –name my-local-ubuntu ubuntu:20.04
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub
docker container,docker client,docker daemon,docker registry,docker image,docker cli,docker host,docker hub
As a result, the docker run command downloads the Ubuntu image from Docker Hub and launches a new container named my-local-ubuntu.

6. Conclusion

In this tutorial, we will learn about Docker and its architecture. We learned how to install Docker on our local machine, how to pull and run Docker images, and how to list and delete Docker images and containers.

Happy coding!!! 😊
in

No comments:

Post a Comment

Popular posts