The Basics of Docker (part 1)

Tram Ho

Hello everyone, Today I would like to write a presentation on the most generalized docker for everyone to have an overview before starting to practice.

What is Docker

Docker is a platform for developers and systemadmin used to build, run and share apps with a concept called Containers. Using Containers to deploy an application is called Containerization. Docker is increasingly used and popularized, because:

Flexibility: Even the most complex applications can be packaged.

Lightweight: Unlike virtual machines, Docker leverages and shares the host kernel, making them much more resource efficient.

Portable: You can build apps and dependencies from local, then deploy to the cloud and run anywhere …

Docker’s slogan

Build ship and deploy any application, any where

  • Easy software packaging
  • Deploy quickly
  • No need to configure and install cumbersome environment

Architecture

The photo below illustrates the docker’s architecture

Docker is built on top of 3 main components:

  • Docker daemon: daemon (server) receives commands from docker client via CLI or RestAPI
  • Docker client: Can be on the same host or different host with docker daemen
  • Docker registry (hub): is a server service that allows storage of docker images of individuals, companies, teams, … Docker Registry service can be provided by a third party or internal service. build separately if you want. Some popular Docker Registry services such as: Azure Container Registry, Docker Hub …

Image

Image is the docker’s base kernel, configured with the essential applications running inside of it, and a template for creating containers. If we think of OOP, we can see docker image as class, and docker container is object / instance of that class!

So if we have a docker image on the machine, we can create one or more containers with identical internal environments. If docker has successfully installed, run the following command to view the current images you have:

I will see a list of existing images with some information such as:

Repository: where the image was created, here can also be understood as the name of the iamge

Tag: version of the image

Image ID: ID of the image

Container

A runtime form of Docker images used as a runtime environment for applications. To run a container from the image:

The image name is taken from the image list that I got above, if I just type the image name without passing the version, it will run the container with the latest version by default.

Life cycle

In the above two parts I talked about images and containers, two very basic components of docker and process, the life cycle will also revolve around these two components.

Docker basic process

From dockerfile (this I will show in another post) I will create an image through the build command, after creating the image, I will run the container through the run command passed in the image name as introduced in upper part.

The second stage in the docker’s life cycle, the container after being created from the original image, I proceed to manipulate that container like installing additional dependency software, for example, now I will have a container with all the things to I can run an application.

The next step is to use commands

This command will convert a container into an image with the same content as the container at the time of the commit command, noting that it is converting from container to image, not completely deleting the container.

So we can see two commands docker run and docker commit are complementary.

  • The docker run command passes an image to create a container
  • The docker commit command, on the other hand, passes in a container ID to be converted into an image

To summarize, I have the life cycle of docker as follows:

Storage mechanism

Docker uses a copy on write storage engine. Instead of saying I need to create a container . I would say I want to make a copy of the template . Because in essence I create a container with an image available. The container is created in a separate storage area to record the changes, which is independent of the original image storage area, which is why it takes so fast to launch a container. so.

Some commonly used docker commands

The docker create command creates a container like the docker run command, but the difference is that it does not run the container in the first place. But if I want to run, I use docker start as shown below

Next 3 commands

These commands change the state of the container including switching from running to stopping and restarting the container.

This command is used to copy files or folders from local machine to container and vice versa. Applies to both stopped or running containers.

This command is used to delete one or more containers by name or ID Note that before deleting you need to make sure that the container has been stopped.

This command is used to view the output of the container after the container has finished running.

Export and import the image

Docker also has support for backing up and restoring images locally without having to interact with the registry. This is very convenient when I want to bring the image to the client without uploading it to the registry through two commands

This command will export one or more images and compress them into a single file, for example:

It means I export an ubuntu image with the latest version into a compressed file called backup-image.tar.gz.

After sending this archive to someone else or storing it on a local computer, if you want to load it for use, run the command:

For example:

At this point you will see the ubutu image with the latest version that I exported above.

Summary

In this article’s scope, I only focus on the two important foundations of docker, Image and Container, proficient manipulation with these two components is the foundation to access the advanced parts that I will present in the following article. In the next article I will talk about dockerfile, docker composer and how to push an image to the Registry. If the article is lacking, please comment, thank you for reading!

References

https://docs.docker.com/get-started/overview/

https://www.youtube.com/watch?v=4UlA4MX9LUo&list=PLaAY5r3iMVfmyz21DmPNuc8Pnab6wl_do&index=1

Share the news now

Source : Viblo