gpt4 book ai didi

bash - 检查非守护进程 Docker 容器,在它完成运行后

转载 作者:行者123 更新时间:2023-12-02 18:15:51 25 4
gpt4 key购买 nike

我有一个我创建的容器

# start
docker build -t cdt-tests .
docker run -it --name cdt-tests cdt-tests
# end => I want to inspect the container filesystem after it's done

因为它不是在分离模式下运行,我该如何检查容器?我想要做的是防止容器“自动关闭”,这样我就可以在容器完成后检查它的文件系统。

cdt-tests 的 Dockerfile 如下所示:

FROM node:6

RUN apt-get update && \
apt-get -y install sudo

RUN sudo apt-get -y update
RUN sudo apt-get -y upgrade
RUN sudo apt-get install -y sqlite3 libsqlite3-dev

USER root

RUN mkdir -p /tmp/test-deps
RUN mkdir -p /usr/local/cdt-tests
WORKDIR /usr/local/cdt-tests

ENV SUMAN_POSTINSTALL_IS_DAEMON no

RUN rm -rf node_modules

RUN npm set progress=false
RUN npm config set loglevel=warn
RUN npm set loglevel=warn

COPY package.json .

RUN npm install --no-optional > /dev/null 2>&1
RUN npm install bower > /dev/null 2>&1

COPY . .

RUN ./node_modules/.bin/bower install --config.interactive=false --allow-root > /dev/null 2>&1

ENTRYPOINT ["/bin/bash", "/usr/local/cdt-tests/@run-tests.sh"]

我知道使用技巧来覆盖入口点并检查容器,如下所示:

docker run -it --entrypoint /bin/bash --name cdt-tests cdt-tests

但是,这对我当前的用例不起作用,因为我想在 @run-tests.sh 完成后 检查容器!

所以我有两个问题:

  1. 如何在非守护进程容器完成运行后检查其文件系统?
  2. 如何获取为非守护程序容器创建的容器的容器 ID(不使用 $(docker ps))。

如果我执行 docker ps -a,我会看到:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
08740a432d1c cdt-tests "/bin/bash /usr/lo..." 24 seconds ago Exited (130) 7 seconds ago cdt-tests
f27a302b1d8f cdt-server "/bin/bash /usr/lo..." 38 seconds ago Up 36 seconds cdt-server
b854506e75df cisco-selenium "/opt/bin/entry_po..." 41 seconds ago Up 39 seconds 4444/tcp cdt-selenium
a37cab33b293 mongo "docker-entrypoint..." 43 seconds ago Up 41 seconds 27017/tcp cdt-mongo

所以我们可以看到 cdt-tests 在那里,即使它不是守护进程。

所以我们尝试检查它,像这样:

docker exec cdt-tests /bin/bash

但是我们得到一个错误:

Error response from daemon: Container 08740a432d1c8f014bc138c82706de1e9682a052c088531d60b33c6acbbd5559 is not running

怎么办?

最佳答案

How can I inspect the filesystem of a non-daemon container after it has completed running?

使用 docker cp。查看docs

The docker cp utility copies the contents of SRC_PATH to the DEST_PATH. You can copy from the container’s file system to the local machine or the reverse, from the local filesystem to the container. (...) The CONTAINER can be a running or stopped container. The SRC_PATH or DEST_PATH can be a file or directory.

另一种选择是将容器具体化为新图像并在其上运行 bash:

docker commit <stopped-container> new_image_name
docker run -it --entrypoint /bin/bash new_image_name

How can I get the container id for the container that's created for a non-daemon container (without using $(docker ps)).

1) 当您执行 docker run -d 时,输出是已创建的容器 ID,因此您可以保存该信息:

container_id=$(docker run -d .......)

2) 这将向您显示已停止的测试容器:

docker ps -a --filter ancestor=cdt-tests

然后将最后停止的测试容器放入 var 中:

container_id=$(docker ps -q -a --filter ancestor=cdt-tests | head -n1)

每种情况都有许多其他变体。


编辑。使用卷版本方法,您可以将单个文件绑定(bind)为卷:

docker run -v ./tests.log:/path/to/logs/file.log -it --name cdt-tests cdt-tests

关于bash - 检查非守护进程 Docker 容器,在它完成运行后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44213563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com