How to list files in a stopped Docker container

前端 未结 6 1265
深忆病人
深忆病人 2021-02-03 20:25

This question shows how to copy files out of a stopped container. This requires that I know the full path to the file including its file name. I know the directory I want to c

相关标签:
6条回答
  • 2021-02-03 20:59

    Try using container-diff with the --type=file option. This will compare two images and report the files added, deleted and modified.

    If a file did not change since the container was started, then you would have to know the contents of the original image that started the container. So, this answer is not ideal but avoids creating an image and running it.

    This tool requires that you first create an image of the stopped Docker container with docker commit.

    Here is the command to install it:

    curl -LO https://storage.googleapis.com/container-diff/latest/container-diff-linux-amd64 \
        && chmod +x container-diff-linux-amd64 \
        && mkdir -p $HOME/bin \
        && export PATH=$PATH:$HOME/bin \
        && mv container-diff-linux-amd64 $HOME/bin/container-diff
    

    Here is the command to use the utility:

    container-diff analyze $IMAGE --type=file
    
    0 讨论(0)
  • 2021-02-03 21:03

    The command docker diff *CONTAINER* will list the files added, deleted and changed since the Container started.

    If a file did not change since the container was started, then you would have to know the contents of the original image that started the container. So, this answer is not ideal but avoids creating an image and running it.

    Unlike container-diff, this command does not require first creating a Docker image.

    0 讨论(0)
  • 2021-02-03 21:05

    This answer to another question shows how to start a stopped container with another command. Here are the commands to list files in a stopped container.

    1. Commit the stopped container to a new image: test_image.
      • docker commit $CONTAINER_ID test_image
    2. Run the new image in a new container with a shell.
      • docker run -ti --entrypoint=sh test_image
    3. Run the list file command in the new container.
      • docker exec --privileged $NEW_CONTAINER_ID ls -1 /var/log
    0 讨论(0)
  • 2021-02-03 21:13

    When starting the container is not an option, you can always export your image (a bit overkill but..) and list its contents:

    docker export -o dump.tar <container id>
    
    tar -tvf dump.tar
    

    Reference: Baeldung - Exploring a Docker Container’s Filesystem

    0 讨论(0)
  • 2021-02-03 21:15
    docker container cp <STOPPED_CONTAINER_ID>:<PATH_TO_FILE> -
    

    Notice the "-" at the end of the command.

    It actually "copies" the specified file from the stopped container into "stdout". In other words, it just prints the file contents.

    Thanks @azat-khadiev for your direction (I don't know why you got "-1 for that answer...)

    0 讨论(0)
  • 2021-02-03 21:24

    If you want to see a certain file content, I would suggest using docker container cp command. Here is the doc. It works on stopped container. Example:

    docker container cp 02b1ef7de80a:/etc/nginx/conf.d/default.conf ./
    

    This way I got the config file that was generated by templating engine during start.

    0 讨论(0)
提交回复
热议问题