In the previous article, we introduced the basic knowledge of Docker, and introduced the use of Log4Net in the article (using Log4Net to output logs to different files according to different log levels). In this article we will combine the two to show how to output the logs of applications in docker containers to docker hosts. When it comes to the output of docker application log, we need to first understand the data management mechanism of Docker.

Docker data management

By default, all files created within a container are stored on the writable container layer, which means:

When the container no longer exists, the data does not persist, and it can be difficult to get it out of the container if another process needs it. The writable layer of the container is tightly coupled to the host running the container, and we can’t easily move the data elsewhere.

Fortunately Docker provides containers with two options to store files on the host so that they are persisted even after the container is stopped or deleted. You can use either Volume or Bind Mounts, or you can use TMPFS Mounts if you’re running Docker on Linux.

Volume (Data volume): Volumes are stored in a part of the host file system managed by Docker. The docker

The process should not modify this part of the file system. Volume is the best way to persist data in Docker.

The bind mounts directory: the bind mounts directory can be stored anywhere on the host system. They can even be very important system files or directories. Docker hosts or non-Docker processes on Docker containers can modify them at any time.

TMPFS mount: Only stored in the memory of the host system and never written to the file system of the host system.

The following figure shows the relationship between Containers and Docker hosts in three types of data persistence:



A simple understanding of volume is to create a volume in the Docker host, which is stored in the docker managed host file system, as shown in the docker area in the figure above. This volume is persisted by mounting it to a file or directory in a Container (I prefer to call mounting a map) to persist the data for that directory or file.

In this article, we will focus on implementing the log output of the application in the container based on volume. Volume is a special directory that can be used by one or more containers. It bypasses Union File System (UFS) and provides features like the following:

  • Volumes can be shared and reused across containers
  • Changes to volume take effect immediately
  • Updates to the volume do not affect mirroring
  • The volume persists by default, even if the container is deleted


The creation of a Volume

Docker volume create volume-name docker volume create demo-log docker volume create demo-log With this command, we create a volume named demo-log in the Docker host, as shown below:



You can run the docker volume ls command to view the volume created in the current Docker host, as shown in the following figure:



Use the docker volume inspect demo-log command to view the information about the created Demo-log volume, as shown in the figure below:



The host path where the volume resides is “C: ProgramData Docker volumes common-log _data”.

Next, we will use the docker run command to mount the Demo-log volume to a path in the container when creating the container. We will containerized the example described in (Log4Net is used to output logs to different files depending on the log level). Add a dockerfile file with the following contents:



Docker build -t logdemo:v1. The command creates an image named logdemo:v1 and passes the command

docker run -d -p 5001:80 --name logdemocontainer --mount source=demo-log,target=c:/app/logs logdemo:v1 

Create a container named logDemoContainer and mount the demo-log volume to the logs folder. The logs folder will map to the demo-log folder of the Docker host. Workdir is set to app in dockerfile, so the app directory must exist in the container. As for which disk it exists in, we can check it through the command Docker inspect logdemo:v1, as shown in the following figure:



Note that the Logs Folder is created when our application writes to the log, which is the output path of the log in the Container.

When the output log is available, we can see the output log in the volume directory of the Docker host, as shown in the figure:



You can use the docker inspect ContainerName command to check information about the current container, including Mounts information, as shown in the figure.




Add the volume demo-log:c:/app/logs command to the dockerfile to define the mount information. When the container is created, the mount information is added to the image, and the volume is created when the container is created. Therefore, we do not need to create the volume beforehand. The dockerfile will look like this:



The above is the whole content of this article. I hope this introduction can help you to better understand Volume.


Related information:

docs.docker.com/storage/

Yeasy. Gitbooks. IO/docker_prac…


Previous Article (Summary of problems encountered in Docker practice)

Net Core App configuration file in Docker container