1

I need to look up into docker logs for some days ago and checking by docker service logs SERVICE | grep WHAT_I_NEED takes forever so I want to download the container logs from docker swarm and check those locally. I found that the container logs in Swarm can be found by:

docker inspect --format='{{.LogPath}}' $INSTANCE_ID

but I can't find a way to download the log from the location.

Doing: docker cp CONTAINER_ID:/var/lib/docker/containers/ABC/ABC-json.log ./ tells me that the path is not present. I understand that this path is in Swarm but then how to get the log from the container itself? Or is there another way to copy this file directly to a local file?

Noah Martin
  • 1,708
  • 9
  • 35
  • 73

1 Answers1

6

Try running this one from your terminal:

docker logs your_container_name 2> file.log

This will redirect the container logs to the local file file.log

  • Thanks for your answer but isn't it possible to get the file directly as it is in docker? – Noah Martin Sep 24 '18 at 20:20
  • 2
    Yes the log file actually exists locally in your machine where docker is running and doing a "docker inspect container_name" will fetch you the logpath. Using that logic I tried the following and was able to retrieve the file you referred: **`cp \`docker inspect container_name | jq -r .[].LogPath\` .`** this will copy the log file as it is in your current location (referred by '.' at the end of the command). You may give your choice of loaction also. **Note**: since the log file exists locally, no need to use docker cp. Simple cp will do the trick. :-) – Susmita Bhattacharjee Sep 25 '18 at 06:50
  • I get a permission denied doing cp directly, maybe I don't have rights. However I am marking your answer as right as it really does the trick – Noah Martin Sep 25 '18 at 07:18
  • Thanks. Try using the command with **sudo** if you don't have direct permission. – Susmita Bhattacharjee Sep 25 '18 at 08:21
  • Exactly what I've looked for! `2>` did the trick :) – Alex Belyaev Nov 02 '20 at 18:49
  • file.log has been created but 0 byte – Сергей Mar 10 '23 at 10:24