Docker Usage & Entity

Docker Usage & Entity

The following knowledge describes how to get started with Docker, a platform that uses container virtualization technology.

With the above knowledge, Docker can be used. In addition, We describe the main commands as how to use Docker. Also, we also describe the actual file storage location such as images and containers as an entity.
This knowledge is based on the assumption that Docker is installed and started.

Sponsored Links

Docker main commands

  • Display the list of Docker images currently saved on the local host.
    • You can check the existing image as below.
      • In the result below, there is one image with repository name “hello-world”, tag name “latest”, and ID “XXXXXXXXXXX”.
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              XXXXXXXXXXX       2 months ago        1.85kB
  • Download the latest version of the official ubuntu image from Docker Hub with the docker pull command.
    • The command example “docker pull myregistrydomain: port / aaa / bbb: latest” downloads the image of repository “bbb” and tag “latest” in namespace “aaa” from registry “myregistrydomain” via port “port”.
      • In the example below, the registry, port, and namespace are not specified, but if not specified, the registry will be Docker Hub “docker.io”, the port will be “5000”, and the namespace will be “library”.
$ docker pull ubuntu:latest
Trying to pull repository docker.io/library/ubuntu ...
latest: Pulling from docker.io/library/ubuntu

XXXXXXXXXX: Pull complete
     ~~~
  • Create a new container from the image and execute the command with the “docker run” command.
    • In the example below, a container named “ddd” is created using the “ubuntu: lastest” image. Then, start the created container, execute “/ bin / bash” in the container, connect to the container and log in.
    • “root@Container ID” is displayed, and you can confirm that you can log in to the container.
$ docker run -it --name ddd ubuntu:latest /bin/bash
root@4e0b0bd218e2:/#
  • Log out of the container and stop the container.
root@4e0b0bd218e2:/# exit
  • Show current container list.
    • In the example below, you can see the container with the name “ddd” and the ID “4e0b0bd218e2” created earlier.
    • “STATUS” is “Exited”, and you can confirm that the container is stopped.
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS               NAMES
4e0b0bd218e2        ubuntu              "/bin/bash"         18 minutes ago      Exited (0) 4 minutes ago                           ddd
  • Launch the created container.
    • If you check the container status again, “STATUS” is “UP” and you can confirm that the container is running.
$ docker start ddd
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4e0b0bd218e2        ubuntu              "/bin/bash"         25 minutes ago      Up 2 minutes                            ddd
  • Restart the created container.
$ docker restart ddd
  • Execute “/ bin / bash” with the “docker exec” command to log in to the started container.
$ docker exec -i -t ddd /bin/bash
root@4e0b0bd218e2:/#
  • Log out of the container with the “exit” command.
    • If you check the container status, you can see that it is still running.
      • If you log in to the container with the “docker exec” command, the container will continue to start even if you log out with “exit”.
root@4e0b0bd218e2:/# exit
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4e0b0bd218e2        ubuntu              "/bin/bash"         26 minutes ago      Up 3 minutes                            ddd
  • Next, log in to the started container with the “docker attach” command.
    • You can also log in to the container with this command.
docker attach ddd
root@4e0b0bd218e2:/#
  • To log out without stopping the container after logging in with the “docker attach” command, enter the following key.
    • After that, if you check the status of the container, you can confirm that the “STATUS” remains “UP” and the container is still running.
ctrl-p ctrl-q

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4e0b0bd218e2        ubuntu              "/bin/bash"         27 minutes ago      Up 4 minutes                            ddd
  • Log in to the started container again with the “docker attach” command, and log out there with “exit”.
    • Unlike the operation of “docker exec”, if you execute “exit” from this state, the container will stop at the same time as logout.
    • You can confirm that “STATUS” is “Exited” as shown below.
docker attach ddd
root@4e0b0bd218e2:/# exit

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS               NAMES
4e0b0bd218e2        ubuntu              "/bin/bash"         37 minutes ago      Exited (0) 2 minutes ago                           ddd

Docker file entity and structure

Since Docker is container virtualization, Dokcer related physical files are stored on the host side.
As the disk usage of the container increases, the disk usage on the host side also increases accordingly.
it is necessary to manage the resource of the disk by grasping the location of the actual file and the directory to be used.
Therefore, We describe the location of the files used by Docker in this knowledge.

  • Basically, Docker related files are stored in the following directory.
/var/lib/docker

$ sudo ls /var/lib/docker
builder  buildkit  containers  image  network  overlay2  plugins  runtimes  swarm  tmp  trust  volumes
  • The Docker image and container file entity are stored in the following directory on the host.
    • Due to the mechanism of Docker, it is not simply a single directory or file. It is divided into multiple layers, and there is a directory for each layer.
    • If you check the “diff” directory under the directory, you can check the actual file of the difference updated in the container.
/var/lib/docker/overlay2/

$ sudo ls /var/lib/docker/overlay2
$ sudo ls /var/lib/docker/overlay2/XXXXXXXXXXXXX/diff

There are two types of Docker log files. They are the logs in the container running on Docker and the logs of the Docker service / Docker daemon itself.

  • We describe the actual log file in the container running on Docker. The file is stored in the following directory on the host.
    • TThe names of the directories and log files include the corresponding container IDs.
    • This log describes the STDOUT and STDERR output in each container.
/var/lib/docker/containers/

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS               NAMES
4e0b0bd218e2        ubuntu              "/bin/bash"         43 minutes ago      Exited (0) 1 minutes ago                           ddd
$ sudo ls /var/lib/docker/containers/
4e0b0bd218e2xxxxxxxxxxxxxxxxxxxxxxxx
$ sudo ls /var/lib/docker/containers/4e0b0bd218e2xxxxxxxxxxxxxxxxxxxxxxxx/
4e0b0bd218e2xxxxxxxxxxxxxxxxxxxxxxxx-json.log
  • In addition, although the above is the entity, you can also check the log with the command that specifies the container name as shown below.
$ docker logs ddd
  • The log of the Docker service / Docker daemon itself is as follows.
    • The storage location and confirmation method differ depending on the Linux distribution and environment. We describe the case of an environment where the docker service is managed by systemd, which is common in recent Linux. Ubuntu and CentOS also apply to this case.
    • The “systemd” that manages the Docker service holds the “journal” log system. Therefore, the log of the Docker service itself can be confirmed by using “journal”.
    • As an example of a case different from this case, the log may be confirmed by “/var/log/messages | grep docker” or “/var/log/docker.log”.
$ journalctl -u docker.service
  • The actual file is as follows, but since the journal file is binary, it is necessary to execute the above command to check the log.
/var/log/journal

$ ls /var/log/journal
Sponsored Links