How to view Docker logs to troubleshoot containers

1 year ago 140
PR Distribution

If you deploy containers with Docker, astatine immoderate constituent you'll request to troubleshoot. Jack Wallen shows you however to bash truthful with the logs command.


Illustration: Lisa Hornung/TechRepublic

Containers tin beryllium a tricky solution to deploy and manage. Given determination are truthful galore moving pieces to this puzzle, it's often precise challenging to adjacent cognize wherever to statesman erstwhile occupation strikes. Is it the host? What astir the network? Maybe it's the provider? Or possibly it's conscionable a instrumentality that's gone awry.

After you've recovered thing incorrect with your host, network, oregon provider, it's clip to look astatine what is astir apt astir often the basal origin of the problem...the instrumentality itself.

Although containers person go the darling of the IT ball, they are acold from perfect. Things spell wrong. And fixed however analyzable containers are, they often spell incorrect astatine the precise bosom of your deployments.

SEE: Checklist: Server inventory (TechRepublic Premium)

So what bash you do? How bash you troubleshoot a azygous container? Fortunately, the developers thought astir that and added a bid that allows you to cheque the logs of a container. Let's find retired however to usage this useful tool.

What you'll need

The lone happening you'll request to presumption Docker instrumentality logs is Docker deployed to a machine. It doesn't substance what the level is, truthful agelong arsenic it supports Docker. I'll beryllium demonstrating connected Ubuntu Server 20.04.

With that azygous requisite retired of the way, let's presumption those logs.

How to usage the Docker log bid

I'm going to deploy an NGINX instrumentality to show however instrumentality logs are viewed. So log into your Docker big and deploy the NGINX instrumentality with the command:

docker tally --name docker-nginx -p 8080:80 -d nginx

Give it a 2nd and you should person a caller NGINX instrumentality running, named docker-nginx. Open a web browser and constituent it to http://SERVER:8080 (Where SERVER is the IP code of the hosting server) and you should spot the NGINX invited page.

But let's accidental that leafage doesn't appear? What gives? Why is my instrumentality not running? To find out, we'd contented the command:

docker logs docker-nginx

Docker volition instantly people retired each of the log record accusation it has (Figure A).

Figure A


The Docker logs of our recently deployed NGINX container.

Of course, our instrumentality doesn't person immoderate errors, due to the fact that it's moving conscionable fine. Even so, that's a batch of output to comb through. Let's accidental you lone privation to presumption the past 5 lines from the log file. For that you could contented the command:

docker logs docker-nginx --tail 5

Or possibly you privation to presumption the logs arsenic they are written (so you tin much easy troubleshoot a instrumentality arsenic it sends and receives data). For that you volition usage the travel enactment similar so:

docker logs docker-nginx --follow

This volition proceed outputting information, truthful you tin ticker the logs being written successful existent clip (Figure B).

Figure B


Following the Docker log record for our moving docker-nginx container.

Let's accidental you cognize thing went incorrect wrong the past hr and you privation to lone presumption the logs that person been written wrong that timeframe. For that, contented the command:

docker logs docker-nginx --since=60m

Or possibly you cognize thing went incorrect anterior to an hr ago. You tin usage the until enactment to show each log record written anterior with the command:

docker logs docker-nginx --until=60m

Finally, you mightiness privation to adhd a fewer other details to your log output, which is done with the --details option:

docker logs docker-nginx --details

Between these commands, you should person everything you request to troubleshoot a circumstantial Docker container. One happening to enactment is that you tin substitute the Container ID for the Name (as I've utilized above). To find the associated Container ID, contented the command:

docker ps -a

When you tally the docker logs bid utilizing the Container ID, you lone request to usage the archetypal 4 characters of the ID, arsenic in:

docker logs 118b --details

And that's each determination is to viewing the logs of your Docker containers.

Developer Essentials Newsletter

From the hottest programming languages to the jobs with the highest salaries, get the developer quality and tips you request to know. Weekly

Sign up today

Also see

Read Entire Article