Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Pipe Log Output to STDOUT with Docker

    Mark ShustMark Shust

    Since Docker filesystems are ephemeral, it's important to take into account anything that writes or appends to disk, such as log files. It is standard practice when running Docker containers to pipe log file output to the console, to allow for simple diagnostic and to avoid append-only files from consuming disk space.

    In this lesson, we’ll show you how to append output to STDOUT, and how to view log output from your running containers.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: Anything written to append-only files within containers should be piped to standard out. This makes it easy to diagnose log files, and ensure that append-only files don't consume disk space.

    Some widely used Docker images such as Nginx already write to standard out. However, sometimes you have an application-specific log that you want to apply the same methodology to.

    Say we have a simple app that overrides the console.log function of Node. Console.log already writes to standard out, so this is a really nonsensical example, but we will use this to get our point across on how to redirect writes from log files to standard out.

    In this case, we will write all calls to console.log to a file named debug.log. If we run this script for a few moments and then check the output of debug.log, we will see that the script is running and correctly writing to debug.log.

    There's a pretty simple way we can accomplish this change within our Docker file. We will add a new instruction before our command instruction that is a run.

    What we will do here is use ln -sf to create a symlink to /dev/standardout from our debug.log file. This simple line will take whatever would normally be written to our debug.log file and pipe it right to standard out.

    Let's build our image, and then run it in Daemon mode. If we use Docker logs with a -f function to tail the output of our container, we can see that the output normally written to our debug.log file is definitely piping to standard out.

    We can also confirm that no output is further writing to debug.log within our container, so we don't have to worry about this container accumulating disk space over time.