How to direct Docker standard output and error output at the same time

Sponsored links

How to check log that Docker container writes

docker logs [container name or ID]

If the container name is my_app, it looks like this.

docker logs my_app

If you want to check only recent entries, you can add options like --tail 100 or -fn=100.

docker logs my_app --tail 100

This command shows the log entries that are the most recent 100 lines. The session is immediately closed.

docker logs my_app -fn=100

This command shows the most recent 100 lines and keeps the session. If the container writes additional logs, those are also shown.

Sponsored links

How to check output the log file

There are some cases where we want to copy the log files of a container that is running on a server.
If the server can be connnected via ssh, the following command write the log content to the host machine.

ssh user@<IP_Address> docker logs my_app -fn=100 > D:\output\my_app.log 2>&1

What this command does is

  1. connect to a machine
  2. create a file D:\output\my_app.log on the host
  3. writes the most recent 100 lines of log to D:\output\my_app.log
  4. keep writing the incoming logs

The last command 2>&1 is important to write the error output too.

If you execute the command without it, the error logs are shown on the console but not written to the destination file.

Comments

Copied title and URL