Day 16/17 - Intro to Docker

Day 16/17 - Intro to Docker

Docker

Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. Using Docker, you can quickly deploy and scale applications into any environment and know your code will run.

Tasks

As you have already installed docker in previous days tasks, now is the time to run Docker commands.

  • Use the docker run command to start a new container and interact with it through the command line. [Hint: docker run hello-world]

  • Use the docker inspect command to view detailed information about a container or image.

  • Use the docker port command to list the port mappings for a container.

  • Use the docker stats command to view resource usage statistics for one or more containers.

  • Use the docker top command to view the processes running inside a container.

  • Use the docker save command to save an image to a tar archive.

  • Use the docker load command to load an image from a tar archive.

These tasks involve simple operations that can be used to manage images and containers.

For reference you can watch this video: https://youtu.be/Tevxhn6Odc8

You can Post on LinkedIn and let us know what you have learned from this task by #90DaysOfDevOps Challange. Happy Learning :)


1. What Is Docker??

Docker is a centralized open-source platform for packaging, deploying, and running applications. Before Docker, many users face the problem that a particular code is running in the developer's system but not in the user's system. So, the main reason to develop docker is to help developers to develop applications easily, ship them into containers and can be deployed them anywhere.

Docker was first released in March 2013. It is used in the Deployment stage of the software development life cycle that's why it can efficiently resolve issues related to the application deployment.

2. Usage Of Docker

Docker uses containers on the host's operating system to run applications. It allows applications to use the same Linux kernel as a system on the host computer, rather than creating a whole virtual operating system. Containers ensure that our application works in any environment like development, test, or production.

3. What is Container?

Docker containers are lightweight alternatives to the virtual machine. It allows developers to package up the application with all its libraries and dependencies, and ship it as a single package. The advantage of using a docker container is that you don't need to allocate any RAM and disk space for the applications. It automatically generates storage and space according to the application requirement.

4. What is a Virtual Machine?

A virtual machine is software that allows us to install and use other operating systems (Windows, Linux, and Debian) simultaneously on our machine. The operating system in which virtual machine runs are called virtualized operating systems. These virtualized operating systems can run programs and perform tasks that we perform in a real operating system.

Hypervisor: A piece of software that enables virtualization in a system**.** A hypervisor allows one host computer to support multiple guest VMs by virtually sharing its resources, such as memory and process.

5.Virtualization:

Installing Multiple Guest Operating Systems in one Host Operating System

Hypervisor Software will be used to achieve this

We need to install all the required softwares in HOST OS to run our application, It is old technique to run the applications

System performance will become slow in this process

To Overcome the problems of Virtualization we are going for Containerization concept

6. Containerization:

It is used to package all the softwares and application code in one container for execution

Container will take care of everything which is required to run our application

We can run the containers in Multiple Machines easily

Docker is a containerization software

Using Docker we will create container for our application

Docker images we can share easily to multiple machines

Using Docker image we can create docker container and we can execute it

7. Docker Images:

A Docker image is a read-only template that contains a set of instructions for creating a container that can run on the Docker platform. It provides a convenient way to package up applications and preconfigured server environments, which you can use for your private use or share publicly with other Docker users.

8. Docker Architecture:

Docker Components:

  • Docker Daemon: Docker Daemon runs on the host operating system. It is responsible for running containers to manage docker services.

  • Docker Client: Docker client uses commands and REST APIs to communicate with the Docker Daemon (Server). When a client runs any docker command on the docker client terminal, the client terminal sends these docker commands to the Docker daemon. The Docker daemon receives these commands from the Docker client in the form of commands and REST API requests. The client uses these three commands:

    docker build

    docker pull

    docker run

  • Docker Host: Docker Host is used to providing an environment to execute and run applications. It contains the docker daemon, images, containers, networks, and storage.

  • Docker Registry: Docker Registry manages and stores the Docker images.

    There are two types of Registries:

    Public and Private

9. Docker installation:

Prerequisites

  1. Ubuntu 20.04 server

  2. An account on dockerhub

First, update your existing list of packages:

sudo apt-get update

Install docker

sudo install docker.io -y

Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running:

sudo systemctl status docker

The output should be similar to the following, showing that the service is active and running:

Installing Docker now gives you not just the Docker service (daemon) but also the docker command line utility, or the Docker client.

If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group: and reboot the docker

sudo usermod -aG docker ${USER}
sudo reboot

Now reconnect to the ec2 instance and use the docker commands without sudo privileges.

Pulling an image called hello--world from dockerhub and check the image using

docker images

docker pull nginx
docker images

Now run the docker image to create a container

Docker run command :

Command to start a new container and interact with it through the command line

docker run -d -p 80:80 nginx

The container is created now access the nginx using the Publicip:80 in web browser


Docker push command

Used to push the images to docker hub repo

Docker login

Provide the dockerhub user and password details

Creating a Docker image with a tag

docker build . -t rjthapaa/nodeimage

Using Docker push rjthapaa/nodeimage command to push nodeimage to dockerhub

Image named nodeimage is pushed to dockerhub repo called rjthapaa

Docker Inspect command :

Command to view detailed information about a container or image

docker inspect <container ID>

Docker Port command :

Command to list the port mappings for a container.

docker ports <container ID>

Docker stats command :

Command to view resource usage statistics for one or more containers.

docker stats                  --- Gives all the stats of running Containers
docekr stats <container Id>   --- provides stats of particular Container

Docker top command :

Command to view the processes running inside a container.

docker top <container Id>

Docker save command :

Command to save an image to a tar archive

docker save <image name> > directorypath.tar
docker save nodeimage > /home/ubuntu/nodeimage.tar

Docker load command :

Command to load an image from a tar archive

docker load < directorypath.tar
docker load < /home/ubuntu/nodeimage.tar