Tutum is a great way to manage your Docker deployments. It has support for stacks of Docker-based infrastructure, zero-downtime deployments, and many other features that make it easy to move Docker from your laptop to production.
One of my favorite features about Tutum is the ability to view logs of a container through the Tutum web UI. When you’re having trouble debugging a deployment, the logs are the first place to check.
However, sometimes you need a more powerful log tool for your webapp. There’s been a request for better log draining support, so I thought I would show how I’ve handled this issue for my app.
What you need
- A Tutum node that is ready to deploy services
- A Docker service to play with that logs to
STDERR(you can use my sample app below if you don’t have your own).
- A server to forward syslogs. I use Papertrail, which is really easy to set up.
Docker logging basics
Currently, the Docker logs API has access to output from
STDERR. As such, you may need to rethink how your app handles logging. If you want to log to
syslog or another utility within your container, this tutorial won’t work for you. However, if you’re loving the logs displayed by the Tutum web UI, this will be perfect for you, as the Tutum UI is displaying logs from the Docker daemon.
If your app doesn’t log to
STDERR but you’re still interested in trying this out, you’re in luck. I’ve made a sample Flask app with two endpoints that logs simple messages to
STDOUT. You can use this to follow along.
The examples below will assume you’re using my sample Flask app, but it will apply similarly to anything you use.
Using logspout to foward Docker logs
To handle log forwarding, we’ll be using the logspout container from GilderLabs. The logspout container hooks into the Docker daemon and forwards it to a remote syslog.
First, you’ll want to deploy a logspout service on Tutum. At the Tutum Services UI, click on “Create Service”. Under “Public Images”, search the Docker Hub for “logspout”. Select
gilderlabs/logspout as your service.
On the Service Configuration page, click the “Advanced Options” button to explore more options. In the
Run Command option, enter the address to your remote syslog server. For example, if using Papertrail, you’ll put something like:
You can choose to publish port
8000 on the Service Configuration page to enable streaming of logs and other cool features. This is not required but is a cool additional feature.
After confirming Service Configuration and the Environment Variables page, you will be at the Volumes page. Add a new Volume with a container path of
/tmp/docker.sock and a host path of
/var/run/docker.sock. This allows the logspout container to listen for Docker logs from the Docker daemon.
Once you’re done, hit
Create and deploy. You’re now ready to deploy some apps for logging!
Logging events from containers
Now that your logspout service is deployed, you’re ready to test out some logs.
Back at the Tutum web UI, click ‘Create Service’ again. If you want to use the demo app, search Public Images on the Docker Hub for ‘tutum-log’. You should see ‘alexdebrie/tutum-log’ as an option. Select this image and move to image configuration.
When configuring this image, the only thing you need to do is publish port 8000 on the container to port 80 (or any other port you choose) on your node. This will allow you to access the app via your web browser.
Once your app is deployed, visit your node’s IP address. You should see ‘Hello, Tutum!’ in your browser. Next, go to
<YourIPAddress>/time. You should see “Today is <Today’s date>” in your browser. I know you’re blown away by this app, but keep your focus — it’s not the point of this tutorial.
Viewing your logs
Now it’s time to check out your logs from your Docker-ized apps. If you’re using Papertrail, check out your logs page. You should see logs from when you visited your app. If you used the Flask demo app, you should see “You just logged a request from Tutum!” for each time you visited your ‘/’ page, and “Somebody visited at ” for each time you visited your “/time” page.
In addition to these logs for each request, you should see logs related to Docker containers, such as when your Flask app was created.
- Remember that each container you want to log needs to log to
STDERR. This can be hard for certain services. HAProxy, for example, doesn’t support logging to STDOUT. I’ve had success with Nginx, by adding
access_log /dev/stdout;in your
nginx.conffor logging config.
- If you have a multi-node setup, you’ll need to deploy a logspout container on each node. To do this, you can select the ‘Every node’ deployment strategy. You can read more on deployment strategies here.
- Consider filtering your logs to avoid Tutum-specific logs. Tutum sends out certain metrics and other messages related to Weave, a Docker networking tool. I used Papertrail’s log filtering to filter out messages that I don’t need. For example, I entered “weave|influxdb” in my Papertrail filter.
- Logspout has some really cool additional features. You can inspect log streams using curl or create custom routes, which allows you to do container-specific logging to different syslog servers. If there’s community interest in how these advanced features work, I can write a follow-up post to walk through these.
Any questions on logging with Tutum or logspout? Feel free to leave comments, and I’ll try to answer as best as I can!