简体   繁体   English

docker-compose 调试服务在运行时显示“pwd”和“ls -l”?

[英]docker-compose debugging service show `pwd` and `ls -l` at run?

I have a docker-compose file with a service called 'app'.我有一个名为“app”的服务的 docker-compose 文件。 When I try to run my docker file I don't see the service with docker ps but I do with docker ps -a .当我尝试运行我的 docker 文件时,我看不到docker ps的服务,但我看到的是docker ps -a

I looked at the logs:我查看了日志:

docker logs my_app_1
python: can't open file '//apps/index.py': [Errno 2] No such file or directory

In order to debug I wanted to be able to see the home directory and the files and dirs contained there when the app attempts to run.为了调试,我希望能够在应用程序尝试运行时查看主目录以及其中包含的文件和目录。

Is there a command I can add to docker-compose that would show me the pwd and ls -l of the container when it attempts to run index.py ?有没有可以添加到 docker-compose 的命令,当它尝试运行index.py时会显示容器的pwdls -l

My Dockerfile:我的 Dockerfile:

FROM python:3
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "apps/index.py"]

My docker-compose.yaml:我的 docker-compose.yaml:

version: '3.1'

services:
  app:
    build:
      context: ./app
      dockerfile: ./Dockerfile
    depends_on:
      - db
    ports:
      - 8050:8050

My directory structure:我的目录结构:

my_app:
      * docker-compose.yaml
      * app
        * Dockerfile
        * apps
          * index.py

You can add a RUN statement in the application Dockerfile to run these commands.您可以在应用程序 Dockerfile 中添加 RUN 语句来运行这些命令。 Example:例子:

FROM python:3
COPY . .
RUN pip install -r requirements.txt

# Run your commands
RUN pwd && ls -l

CMD ["python", "apps/index.py"]

Then you chan check the logs of the build process and view the results.然后您可以检查构建过程的日志并查看结果。 I hope this answer helps you.我希望这个答案对你有所帮助。

If you're just trying to debug an image you've already built, you can docker-compose run an alternate command:如果你只是想调试你已经构建的镜像,你可以在 docker-compose 运行另一个命令:

docker-compose run apps \
  ls -l ./apps

You don't need to modify anything in your Dockerfile to be able to do this (assuming it uses CMD correctly; see below).您无需修改 Dockerfile 中的任何内容即可执行此操作(假设它正确使用CMD ;见下文)。

If you need to do more intensive debugging, you can docker-compose run apps sh (or, if your image has it, bash ) to get an interactive shell.如果您需要进行更深入的调试,您可以docker-compose run apps sh (或者,如果您的图像有它, bash )以获得交互式 shell。 The container will include any mounted volumes and be on the same Docker network as the named container, but won't have published ports.该容器将包含任何已安装的卷,并与命名容器位于同一 Docker 网络上,但不会发布端口。

Note that the command here replaces the CMD in the Dockerfile.请注意,此处的命令替换了CMD中的 CMD。 If your image uses ENTRYPOINT for its main command, or if it has a complete command split between ENTRYPOINT and CMD (especially, if you have ENTRYPOINT ["python"] ), these need to be combined into a single CMD for this to work.如果您的图像使用ENTRYPOINT作为其主要命令,或者如果它在ENTRYPOINTCMD之间有完整的命令拆分(特别是,如果您有ENTRYPOINT ["python"] ),则需要将这些组合成一个CMD才能正常工作。 If your ENTRYPOINT is a wrapper script that does some first-time setup and then runs the CMD , this approach will work fine;如果您的ENTRYPOINT是一个包装脚本,它会进行一些首次设置,然后运行CMD ,那么这种方法可以正常工作; the debugging ls or sh will run after the first-time setup happens.调试lssh将在首次设置发生后运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM