Plugin in action
In Part I we explained the basics of a docker log driver. In this second part we will see this in action with a sample server running in a docker container. you will need to clone the logdriver repository locally to follow along.
Build and Install the plugin
Once you have cloned the repository, cd
to plugin directory and run make all
. This should build, install and enable a plugin , you will something like below
### enable plugin monmohan/tldriver:v1
docker plugin enable monmohan/tldriver:v1
monmohan/tldriver:v1
Verify the plugin and note the ID 1d9148d984e8
in below example
ID NAME DESCRIPTION ENABLED
1d9148d984e8 monmohan/tldriver:v1 Simple Log Driver as plugin true
The plugin simply writes any log to its STDOUT
. Where is the plugin stdout?
Docker creates it in the /var/docker/plugins/<pluginid>
. Doing ls -al
in the directory shows three files, plugin stdout
, stderr
and the unix
socket used for communicating with the plugin (this is how docker sends http request to start, stop logging to the plugin http handler)
drwx------ 2 root root 100 Mar 13 20:50 .
drwx------ 4 root root 80 Mar 13 20:50 ..
prwx------ 1 root root 0 Mar 13 20:50 init-stderr
prwx------ 1 root root 0 Mar 13 20:50 init-stdout
srw-rw---- 1 root root 0 Mar 13 20:50 mylogdriver.sock
Since our sample plugin simply writes to its stdout, whatever it reads from the FIFO set by Docker, lets setup a reader in this window on the stdout — cat <init-stdout
Build the Test Server
cd
to testserver
directory and build the docker image
docker build -t testserverimg .
The Test Server starts an HTTP
server at a given port which simply writes the headers and request URI to STDOUT.
Run the Test Server with Plugin
Run the test server in detached mode, mapped to port 9003
docker run --log-driver monmohan/tldriver:v1 -d -p 9003:9003 -e HTTP_PORT='9003' testserverimg
This is what you would see in the terminal window
Start logging request was called for the container : {"File":"/run/docker/logging/10417f2cd63ea34a2928ea4816b6f912e95ffd6683cfcccb399b1489e618cca1","Info":{"Config":{},"ContainerID":"fd21ad91c0c1681ad9157042b2fe6a5515f0460204086eb71071ffa3999beff1","ContainerName":"/eager_blackburn","ContainerEntrypoint":"/usr/bin/testserver","ContainerArgs":[],"ContainerImageID":"sha256:b664862cf96941acc6e2320a6e7c0aaa40e3dc0953849415ee7459ec3855fc42","ContainerImageName":"testserverimg","ContainerCreated":"2019-03-13T13:28:09.281249181Z","ContainerEnv":["HTTP_PORT=9003","PATH=/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","GOLANG_VERSION=1.8.7","GOPATH=/go"],"ContainerLabels":{},"LogPath":"","DaemonName":"docker"}}
New Container added for logging : >
/run/docker/logging/10417f2cd63ea34a2928ea4816b6f912e95ffd6683cfcccb399b1489e618cca1 = fd21ad91c0c1681ad9157042b2fe6a5515f0460204086eb71071ffa3999beff1
fd21ad91c0c1681ad9157042b2fe6a5515f0460204086eb71071ffa3999beff1: [stderr] [1552483689861635661] 2019/03/13 13:28:09 Starting server on host localhost and port 9003
You can now call this echo server to see more output by the plugin
curl http://localhost:9003/foobar
fd21ad91c0c1681ad9157042b2fe6a5515f0460204086eb71071ffa3999beff1: [stdout] [1552483875130451247] Receieved request: /foobar
fd21ad91c0c1681ad9157042b2fe6a5515f0460204086eb71071ffa3999beff1: [stdout] [1552483875130606896] Accept=[*/*]
fd21ad91c0c1681ad9157042b2fe6a5515f0460204086eb71071ffa3999beff1: [stdout] [1552483875130615011] User-Agent=[curl/7.58.0]
What happened here is this -
TestServer → Write to its
STDOUT
→ which Docker reads →pipes to FIFO → which Plugin reads → writes to itsSTDOUT
The last step is totally up to the plugin. It could be posting this message to another server or writing to some file or whatever it wants to do with the logs.
So now we have seen the plugin in action. So feel free to play with the sample plugin code and experiment more !