Day16-Docker for DevOps Engineers

90Days of DevOps Challenge

Day16-Docker for DevOps Engineers

What is Docker?

Docker is a container runtime. Docker is important to both the development community and the container community because it makes using containers so easy. It is an open-source platform that allows you to automate the deployment and management of software applications using containerization.

Containerization is a lightweight approach to virtualization that allows applications and their dependencies to be packaged into isolated, self-contained units called containers.

Docker vs Virtual Machine

While Docker and virtual machines serve a similar purpose, their performance, OS support, and portability differ significantly.

The main difference is that Docker containers share the host’s operating system, while virtual machines also have a guest operating system running on top of the host system. This method of operation affects the performance, hardware needs, and OS support.

Docker provides a set of tools and technologies that make it easy to build, distribute, and run containers.

Docker also allows you to scale and manage your application by running multiple containers across a cluster of machines, providing increased flexibility and scalability.

Docker Containers

A Docker container is a software package with all the required dependencies to run a specific application. All of the configuration and instructions to start or stop containers are dictated by the Docker image. Whenever a user runs an image, a new container is created.

The Docker architecture consists of four main components along with Docker containers .

  • Docker client – the main component to create, manage, and run containerized applications. The Docker client is the primary method of controlling the Docker server via a CLI like Command Prompt (Windows) or Terminal (macOS, Linux).

  • Docker server – also known as the Docker daemon. It waits for REST API requests made by the Docker client and manages images and containers.

  • Docker images – instruct the Docker server with the requirements on how to create a Docker container. Images can be downloaded from websites like

  • Docker Hub. Creating a custom image is also possible – to do it, users need to create a Dockerfile and pass it to the server. It’s worth noting that Docker doesn’t clear any unused images, so users need to delete image data themselves before there’s too much of it.

  • Docker registry – an open-source server-side application used to host and distribute Docker images. The registry is extra useful to store images locally and maintain complete control over them. Alternatively, users can access the aforementioned Docker Hub – the world’s largest repository of docker images.

Tasks:

⫸ Use the docker run command to start a new container and interact with it through the command line:

firstly we have to use the docker pull command to get an image from the docker hub.

docker pull dockerimage

The docker run command creates a writeable container layer over the specified image and then starts it using the specified command.

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

docker run -d hello-world latest

Here we are using option -d means detach to run a container in the background and print container ID.

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

docker inspect commands display the low-level information of the container. The OPTIONS are optional flags that modify the output format, such as --format for using a Go template, or --type for specifying the type of object to inspect. The NAME or ID are the names or IDs of the objects to inspect. You can specify one or more objects to inspect at once.

docker inspect [OPTIONS] NAME|ID [NAME|ID...]

docker inpect hello-world

⫸ Use the `docker port` command to list the port mappings for a container:

All the application in the container runs on particular ports when you run a container. If you want to access the particular application with the help of a port number you need to map the port number of the container with the port number of the docker host.

When running a Docker container, you can map a port on the container to a port on the host or external network using the -p or —publish options. If you use the below command it will publish nginx, for instance, would publish port 80 from the container to port 8080 on the host or external network.

docker port CONTAINER [PRIVATE_PORT[/PROTO]]
docker run -d -P nginx 8080:80

⫸Use the docker stats command to view resource usage statistics for one or more containers:

The docker stats command is used to display real-time resource usage statistics for running containers. It provides information such as CPU usage, memory consumption, network I/O, and block I/O of each container.

docker stats

⫸Use the docker top command to view the processes running inside a container:

The docker top command displays the running processes inside a container. You can use it to see what processes are consuming resources or causing issues inside your container.

docker top container_id

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

The docker save command saves one or more images to a tar archive. You can use it to back up your images or transfer them to another machine.

docker save [OPTIONS] IMAGE [IMAGE...]

docker save -o hello-world.tar hello-world

The OPTIONS are optional flags that modify the output format, such as -o for specifying the output file name. The IMAGE are the names or IDs of the images to save. You can specify one or more images to save at once.

⫸Use the docker load command to load an image from a tar archive:

The docker load command loads an image or a repository from a tar archive. You can use it to restore your images or transfer them to another machine.

docker load <options>

docker load -i hello-world.tar

This will load the image and its metadata from the file and add it to your local image repository. You should see something like this:

You can also load multiple images from a single file by using the same command. For example, if you want to load all three images that you saved in docker save from the file called images.tar, you can use this command:

docker load -i images.tar

This will load all three images and their metadata from the file and add them to your local image repository. You should see something like this:

Loaded image: hello-world:latest
Loaded image: ubuntu:latest
Loaded image: nginx:latest

In this blog, I introduced you to Docker, a powerful tool for creating and managing containers. I have also shown you some basic Docker commands that you can use to interact with containers and images. These commands will help you understand how Docker works and what you can do with it.

I hope you find the blog/article helpful. Like👍 , share with your friends and any questions or feedback, please feel free to leave a comment below.

Thank you for reading and happy learning! 😊