Python logging class in Docker: logs gone
<p>For several years now I've used Python's logging class in the same way:</p>

<pre><code>def get_module_logger(mod_name):
    """
    To use this, do logger = get_module_logger(__name__)
    """
    logger = logging.getLogger(mod_name)
    handler = logging.StreamHandler()
    formatter = logging.Formatter(
        '%(asctime)s [%(name)-12s] %(levelname)-8s %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.setLevel(logging.DEBUG)
    return logger
</code></pre>

<p>Then in some module,</p>

<pre><code>logger = get_module_logger(__name__)
</code></pre>

<p>Now, I am running a Python app that uses this inside of a Docker container. I am running the container with <code>-d -i -t</code>. When I am inside the container after a <code>docker exec -it containername /bin/bash</code>, I can see the logs if I execute a command in my python script that produces logs. However, from outside, <code>docker logs containername</code> never shows anything. I have tried running my container with <code>PYTHONUNBUFFERED=0</code> per a few web posts and that did not help either. Tailing with <code>docker logs -f containername</code> never shows anything either. So all my logs, both stderr and stdout are empty. I have also tried <code>logging.StreamHandler(sys.stdout)</code> but to no avail. </p>

<p>What is wrong? Do I need to change something in the handler?</p>

<p>EDIT: my Dockerfile is very simple:</p>

<pre><code>FROM python:3.5.1
MAINTAINER tommy@...
ADD . /tmp

#need pip &gt; 8 to have internal pypi repo in requirements.txt
RUN pip install --upgrade pip 
#do the install
RUN pip install /tmp/py/

CMD myservice 
</code></pre>

<p>EDIT2:</p>

<pre><code>~ docker --version
Docker version 1.11.0, build 4dc5990
</code></pre>

<p>This is an issue with docker, not python. With docker, the file system seen inside an instance is unique to that instance. Any changes made are not reflected on the host machine. This is a feature of the union file system that docker uses.</p>

<p>You can use "<a href="https://docs.docker.com/engine/tutorials/dockervolumes/" rel="nofollow">data volumes</a>" to mount a directory on the host machine inside your docker instance. You can either copy your logs into the at directory, or (in the future) log directly to that directory.</p>

<p>On Unix:</p>

<pre><code>docker run -v /Users/&lt;path&gt;:/&lt;container path&gt; ...
</code></pre>

<p>On Windows:</p>

<pre><code>docker run -v /c/Users/&lt;path&gt;:/&lt;container path&gt; ...
</code></pre>

<p>This directory will exist outside of the union file system, so you lose the ability to shapshot the directory or roll back changes.</p>

python docker