skip to Main Content

I have a Docker container which has crashed due to a bug in the containerized application.

The restart policy was set to unless-stopped, which was causing the container to start, immediatly crash, and then restart again.

I changes the restart policy using docker container update --restart=no <container-name>.

I would like to download (copy) the log file from the container to the host machine so that I can inspect it to find out what went wrong.

If the container were running, I would simply be able to run docker exec -it <container-name> bash to start a bash session within the container. I would then be able to navigate the file system, find the log file, and either inspect it within the container or copy it from the container with docker cp.

However, the container is not running, and cannot be started because it will immediatly crash.

What are my options in this situation?

3

Answers


  1. a) In your dockerfile, you could change your entrypoint to:

    CMD ["tail", "-f", "/dev/null"]
    

    b) Or

    Commit the stopped container:

    docker commit $STOPPED_CONTAINER user/test_image
    

    Start/run with a different entry point:

    docker run -it --entrypoint=sh user/test_image
    

    c) Or you could try docker debug: https://docs.docker.com/reference/cli/docker/debug/

    Login or Signup to reply.
  2. There are multiple solutions for that:

    • docker logs CONTAINER shows what was going on inside of the container.

    • change the entry point to sh or bash and debug it:

      docker run -it --rm --entrypoint bash CONTAINER_IMAGE
      
    Login or Signup to reply.
    1. You can copy files directly from a stopped container to your host system using the docker cp command. The container’s filesystem is still available even if it is not running.
    docker cp <container-name>:/path/to/logfile /path/to/host/directory
    

    Replace path/to/logfile with the actual path to the log file inside the container and path/to/host/directory with the destination on your host machine.

    1. You can also try committing the stopped container to an image.
    docker commit <container-name> <new-image-name>
    

    Run the image by:

    docker run -it <new-image-name> bash
    
    1. You can also inspect container logs using Docker’s Log Driver. The default is json-file log driver. For container ID
    docker inspect --format="{{.Id}}" <container-name>
    

    To copy or view logs:

    cp /var/lib/docker/containers/<container-id>/<container-id>-json.log /path/to/host/directory
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search