0

How do I copy some log files from container to the host while running docker build from commands under Dockerfile? As soon as the build fails the building container disappear.

One way is to after every RUN command swallow the non-zero exit code, output the logs to the STDOUT and then re-push the original exit code. But it doesn't seem to scale up, like if we want to copy a whole directory, we won't be zipping and outputting that to console :P

Is there any possible potential solution? Maybe connecting a file from host to container or mounting a directory under build process?

Animesh Sahu
  • 5,281
  • 2
  • 7
  • 33
  • You may find it easier to use a chroot and [build `FROM scratch`](https://stackoverflow.com/a/47374705/4541045) – ti7 Apr 27 '21 at 07:43
  • @ti7 can you please elaborate a bit about chroot, can it connect docker fs with host fs? And what's the purpose of the scratch image? – Animesh Sahu Apr 27 '21 at 07:47
  • You can do work before adding things to a container and so have a more minimal image. If they're so problematic, building in a chroot will allow you to directly retrieve the logs of failed builds at any stage before you make a container from it. – ti7 Apr 27 '21 at 07:55

2 Answers2

0

If you push the logs to the stdout you can get the logs by using these commands:

LOG_PATH=$(docker inspect --format='{{.LogPath}}' <the-name-of-your-docker>)
echo ================= Docker log start ===========================
sudo cat $LOG_PATH
echo ================= Docker log end =============================
Dharman
  • 21,838
  • 18
  • 57
  • 107
AAber
  • 887
  • 6
  • 9
  • Actually this still doesn't account for automated exports. The logs will be along with the other output of the commands, also will not be easy to export a directory for example as in questions 2nd paragraph. I guess a bash script can be used to extract the logs into file and rest of command output to console by some prefix and suffix, but I was really hoping we can clone as it is from build container to host . – Animesh Sahu Apr 27 '21 at 12:11
-1

If you want container data to persist after the container exits (e.g. the logs, databases or other stuff) then you should mount a local drive/folder into the container and write whatever you need to persist to the mounted location. In your case, mount a local folder (on the host) into the container and write the build logs to that folder.

Also see answer to this question for more options Docker: Copying files from Docker container to host

JohnXF
  • 462
  • 8
  • 12
  • Any automation tips, like automatically do that once the build fails in a CI? Also, I actually already knew that I could do `docker cp` into the intermediate image made after each RUN but that only exists if the build succeed at that RUN step, otherwise the state of build container gets vanished. – Animesh Sahu Apr 27 '21 at 08:47
  • I'm not sure what you mean by your comment? If the build fails then the build logs will usually contain enough info to debug the problem. The build tool will usually capture everything output to stdout so ensure all build steps/tasks/commands write output there (as well as to files if you need/want to persist that information) so that you can diagnose problems just using the captured output of the build job. – JohnXF Apr 27 '21 at 08:51
  • In my case log files are generated in the build directory inside the container, it is large and doesn't print out to the STDOUT. As I stated in second paragraph of the question . – Animesh Sahu Apr 27 '21 at 08:55
  • If the log is large, perhaps you just need to write the last X lines to stdout - using the tail command for example? You could also copy the log to a mounted volume on build failure. – JohnXF Apr 27 '21 at 09:04
  • 1
    You can't mount volumes during an image build. – David Maze Apr 27 '21 at 10:52