The practicality of data sharing between containers

Earlier in this chapter, you learned the mechanism of accessing the log files of the Apache2 HTTP service from the Docker host. Although it was fairly convenient to share data by mounting the Docker host directory to a container, later we came to know that data can be shared between containers by just using data volumes. So here, we are bringing in a twist to the method of the Apache2 HTTP service log handling by sharing data between containers. To share log files between containers, we will spin off the following containers as enlisted in the following steps:

  1. First, a data-only container that will expose the data volume to other containers.
  2. Then, an Apache2 HTTP service container leveraging the data volume of the data-only container.
  3. A container to view the log files generated by our Apache2 HTTP service.
If you are running any HTTP service on the 80 port number of your Docker host machine, pick any other unused port number for the following example. If not, first stop the HTTP service, then proceed with the example in order to avoid any port conflict.

Now, we'll meticulously walk you through the steps to craft the respective images and launch the containers to view the log files:

  1. Here, we begin with crafting a Dockerfile with the /var/log/apache2 data volume using the VOLUME instruction. The /var/log/apache2 data volume is a direct mapping to APACHE_LOG_DIR, the environment variable set in the Dockerfile in Chapter 6, Running Services in a Container, using the ENV instruction:
      ####################################################### 
# Dockerfile to build a LOG Volume for Apache2 Service
#######################################################
# Base image is BusyBox
FROM busybox:latest
# Author: Dr. Peter
MAINTAINER Dr. Peter <[email protected]>
# Create a data volume at /var/log/apache2, which is
# same as the log directory PATH set for the apache image
VOLUME /var/log/apache2
# Execute command true
CMD ["/bin/true"]

Since this Dockerfile is crafted to launch data-only containers, the default execution command is set to /bin/true.

  1. We will continue to build a Docker image with the apache2log name from the preceding Dockerfile using docker build, as presented here:
      $ sudo docker build -t apache2log .
Sending build context to Docker daemon 2.56 kB
Sending build context to Docker daemon
Step 0 : FROM busybox:latest
... TRUNCATED OUTPUT ...
  1. Launch a data-only container from the apache2log image using the docker run subcommand and name the resulting container log_vol, using the --name option:
      $ sudo docker run --name log_vol apache2log

Acting on the preceding command, the container will create a data volume in /var/log/apache2 and move it to a stop state.

  1. Meanwhile, you can run the docker ps subcommand with the -a option to verify the container's state:
      $ sudo docker ps -a
CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS
NAMES
40332e5fa0ae apache2log:latest "/bin/true"
2 minutes ago Exited (0) 2 minutes ago
log_vol

As per the output, the container exits with the 0 exit value.

  1. Launch the Apache2 HTTP service using the docker run subcommand. Here, we are reusing the apache2 image we crafted in Chapter 6, Running Services in a Container. Besides, in this container, we will mount the /var/log/apache2 data volume from log_vol, the data-only container that we launched in step 3, using the --volumes-from option:
      $ sudo docker run -d -p 80:80 
--volumes-from log_vol
apache2
7dfbf87e341c320a12c1baae14bff2840e64afcd082dda3094e7cb0a0023cf42

With the successful launch of the Apache2 HTTP service with the /var/log/apache2 data volume mounted from log_vol, we can access the log files using transient containers.

  1. Here, we are listing the files stored by the Apache2 HTTP service, using a transient container. This transient container is spun off by mounting the /var/log/apache2 data volume from log_vol, and the files in /var/log/apache2 are listed using the ls command. Further, the --rm option of the docker run subcommand is used to remove the container once it is done executing the ls command:
      $ sudo docker run --rm 
--volumes-from log_vol
busybox:latest ls -l /var/log/apache2
total 4
-rw-r--r-- 1 root root 0 Dec 5 15:27
access.log
-rw-r--r-- 1 root root 461 Dec 5 15:27
error.log
-rw-r--r-- 1 root root 0 Dec 5 15:27
other_vhosts_access.log
  1. Finally, the error log produced by the Apache2 HTTP service is accessed using the tail command, as highlighted in the following command:
      $ sudo docker run --rm 
--volumes-from log_vol
ubuntu:16.04
tail /var/log/apache2/error.log
AH00558: apache2: Could not reliably determine the
server's fully qualified domain name, using 172.17.0.24.
Set the 'ServerName' directive globally to suppress this
message
[Fri Dec 05 17:28:12.358034 2014] [mpm_event:notice]
[pid 18:tid 140689145714560] AH00489: Apache/2.4.7
(Ubuntu) configured -- resuming normal operations
[Fri Dec 05 17:28:12.358306 2014] [core:notice]
[pid 18:tid 140689145714560] AH00094: Command line:
'/usr/sbin/apache2 -D FOREGROUND'
..................Content has been hidden....................

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