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:
docker run --name docker-nginx -p 8080:80 -d nginx
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
Docker will immediately print out all of the log file information it has (Figure A).
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
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:
docker logs docker-nginx --follow
This will continue outputting information, so you can watch the logs being written in real time (Figure B).
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
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:
docker logs docker-nginx --until=60m
Finally, you might want to add a few extra details to your log output, which is done with the –details option:
docker logs docker-nginx --details
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:
docker ps -a
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:
docker logs 118b --details
And that’s all there is to viewing the logs of your Docker containers.