Connect with us

Hi, what are you looking for?


How to view Docker logs to troubleshoot containers

    If you deploy containers with Docker, at some point you'll need to troubleshoot. Jack Wallen shows you how to do so with the logs command.
</p><div data-component="lazyloadImages">

                    <figure class="image pull-none image-large"><span class="img aspect-set " style="padding-bottom: 75%"><img src="" class="" alt="docker-with-waves.jpg" width="770"/></span><figcaption><p>

                                        Illustration: Lisa Hornung/TechRepublic
                                    </p></figcaption></figure><p>Containers can be a tricky solution to deploy and manage. Given there are so many moving pieces to this puzzle, it's often very challenging to even know where to begin when trouble strikes. Is it the host? What about the network? Maybe it's the provider? Or maybe it's just a container that's gone awry.  

After you’ve found nothing wrong with your host, network, or provider, it’s time to look at what is probably most often the root cause of the problem…the container itself.

Although containers have become the darling of the IT ball, they are far from perfect. Things go wrong. And given how complicated containers are, they often go wrong at the very heart of your deployments.

SEE: Checklist: Server inventory (TechRepublic Premium)

So what do you do? How do you troubleshoot a single container? Fortunately, the developers thought about that and added a command that allows you to check the logs of a container. Let’s find out how to use this handy tool.

What you’ll need

The only thing you’ll need to view Docker container logs is Docker deployed to a machine. It doesn’t matter what the platform is, so long as it supports Docker. I’ll be demonstrating on Ubuntu Server 20.04.

With that single requisite out of the way, let’s view those logs.

How to use the Docker log command

I’m going to deploy an NGINX container to demonstrate how container logs are viewed. So log into your Docker host and deploy the NGINX container with the command:

Advertisement. Scroll to continue reading.
docker run --name docker-nginx -p 8080:80 -d nginx
<p>Give it a second and you should have a new NGINX container running, named docker-nginx. Open a web browser and point it to http://SERVER:8080 (Where SERVER is the IP address of the hosting server) and you should see the NGINX welcome page.  

But let’s say that page doesn’t appear? What gives? Why is my container not running? To find out, we’d issue the command:

docker logs docker-nginx
<p>Docker will immediately print out all of the log file information it has (<strong>Figure A</strong>).  

Figure A

The Docker logs of our newly deployed NGINX container.

Of course, our container doesn’t have any errors, because it’s running just fine. Even so, that’s a lot of output to comb through. Let’s say you only want to view the last five lines from the log file. For that you could issue the command:

docker logs docker-nginx --tail 5
<p>Or maybe you want to view the logs as they are written (so you can more easily troubleshoot a container as it sends and receives data). For that you will use the follow option like so:  </p><pre class="code">docker logs docker-nginx --follow</pre>
<p>This will continue outputting information, so you can watch the logs being written in real time (<strong>Figure B</strong>).  

Figure B

Following the Docker log file for our running docker-nginx container.

Let’s say you know something went wrong within the last hour and you want to only view the logs that have been written within that timeframe. For that, issue the command:

docker logs docker-nginx --since=60m
<p>Or maybe you know something went wrong prior to an hour ago. You can use the until option to display every log file written prior with the command:  </p><pre class="code">docker logs docker-nginx --until=60m</pre>
<p>Finally, you might want to add a few extra details to your log output, which is done with the --details option:  </p><pre class="code">docker logs docker-nginx --details</pre>
<p>Between these commands, you should have everything you need to troubleshoot a specific Docker container. One thing to note is that you can substitute the Container ID for the Name (as I've used above). To find the associated Container ID, issue the command:  </p><pre class="code">docker ps -a</pre>
<p>When you run the docker logs command using the Container ID, you only need to use the first four characters of the ID, as in:  </p><pre class="code">docker logs 118b --details</pre>
<p>And that's all there is to viewing the logs of your Docker containers.  </p><div data-component="newsletterSubscription" data-newsletter-subscription-options="{" urlsubs="">
                                                                            <div class="newsletter-shortcode">
                                                    <div class="thumbnail">
                            <img src="" alt="" width="70" height="70"/>
                        <div class="item-content">
                            <h3>Developer Essentials Newsletter</h3>

                                From the hottest programming languages to the jobs with the highest salaries, get the developer news and tips you need to know.

                            <a id="nlse094" data-newsletter-id="e094" data-newsletter-type="subs" data-newsletter-position="newsletter-button" data-authentication-position="newsletter-button">
                                    <svg class=" envelope-alt">
    <use aria-hidden="false" aria-labelledby="envelope-alt" xlink:href=""/>

Sign up today

Also see

Source link

Advertisement. Scroll to continue reading.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *


You May Also Like


Introductions get a lot of attention. I’ve explored the topic of how to write them even though as a reader, I always skip them....

Online Business Success

The internet is now our nervous system. We are constantly streaming and buying and watching and liking, our brains locked into the global information...

SEO Guide

There are all kinds of pictures of the world on the internet, but to find one of these specific pictures that you want to...

Online Business Success

You can think of link building in many ways. I like to call it tedious, painful, and a test of patience. It’s also necessary...