The practicality of host data sharing

In the previous chapter, we launched an HTTP service in a Docker container. However, if you remember correctly, the log file for the HTTP service is still inside the container, and it cannot be accessed directly from the Docker host. Here, in this section, we elucidate the procedure of accessing the log files from the Docker host in a step-by-step manner:

  1. Let's begin with launching an Apache2 HTTP service container by mounting the /var/log/myhttpd directory of the Docker host to the /var/log/apache2 directory of the container, using the -v option of the docker run subcommand. In this example, we are leveraging the apache2 image, which we had built in the previous chapter, by invoking the following command:
      $ sudo docker run -d -p 80:80 
-v /var/log/myhttpd:/var/log/apache2 apache2
9c2f0c0b126f21887efaa35a1432ba7092b69e0c6d523ffd50684e27eeab37ac

If you recall the Dockerfile in Chapter 6, Running Services in a Container, the APACHE_LOG_DIR environment variable is set to the /var/log/apache2 directory, using the ENV instruction. This will make the Apache2 HTTP service to route all log messages to the /var/log/apache2 data volume.

  1. Once the container is launched, we can change the directory to /var/log/myhttpd on the Docker host:
      $ cd /var/log/myhttpd
  1. Perhaps, a quick check of the files present in the /var/log/myhttpd directory is appropriate here:
      $ ls -1
access.log
error.log
other_vhosts_access.log

Here, the access.log file contains all the access requests handled by the Apache2 HTTP server. The error.log file is a very important log file, where our HTTP server records the errors it encounters while processing any HTTP requests. The other_vhosts_access.log file is the virtual host log, which will always be empty in our case.

  1. We can display the content of all the log files in the /var/log/myhttpd directory using the tail command with the -f option:
      $ tail -f *.log
==> access.log <==

==> error.log <==
AH00558: apache2: Could not reliably determine the
server's fully qualified domain name, using 172.17.0.17.
Set the 'ServerName' directive globally to suppress this
message
[Thu Nov 20 17:45:35.619648 2014] [mpm_event:notice]
[pid 16:tid 140572055459712] AH00489: Apache/2.4.7
(Ubuntu) configured -- resuming normal operations
[Thu Nov 20 17:45:35.619877 2014] [core:notice]
[pid 16:tid 140572055459712] AH00094: Command line:
'/usr/sbin/apache2 -D FOREGROUND'
==> other_vhosts_access.log <==

The tail -f command will run continuously and display the content of the files, as soon as they get updated. Here, both access.log and other_vhosts_access.log are empty, and there are a few error messages on the error.log file. Apparently, these error logs are generated by the HTTP service running inside the container. The logs are then stocked in the Docker host directory, which is mounted during the launch of the container.

  1. As we continue to run tail -f *, let's connect to the HTTP service from a web browser running inside the container, and observe the log files:
      ==> access.log <==
111.111.172.18 - - [20/Nov/2014:17:53:38 +0000] "GET /
HTTP/1.1" 200 3594 "-" "Mozilla/5.0 (Windows NT 6.1;
WOW64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65
Safari/537.36"
111.111.172.18 - - [20/Nov/2014:17:53:39 +0000] "GET
/icons/ubuntu-logo.png HTTP/1.1" 200 3688
"http://111.71.123.110/" "Mozilla/5.0 (Windows NT 6.1;
WOW64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65
Safari/537.36"
111.111.172.18 - - [20/Nov/2014:17:54:21 +0000] "GET
/favicon.ico HTTP/1.1" 404 504 "-" "Mozilla/5.0 (Windows
NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/39.0.2171.65 Safari/537.36"

The HTTP service updates the access.log file, which we can manipulate from the host directory mounted through the -v option of the docker run subcommand.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset