【问题标题】:Command to get to the Container Prompt After Entrypoint入口点后进入容器提示的命令
【发布时间】:2019-05-03 04:06:27
【问题描述】:

可以在 Dockerfile 底部添加什么命令以在 ENNTRYPOINT 之后进入容器提示符? Dockerfile 运行正常。只是它会回到执行它的提示符。

 # Pull base image
From ubuntu:18.04
LABEL maintainer="tester@gmail.com"

# Install dependencies
RUN apt-get update -y
RUN apt-get install -y build-essential python3.6 python3.6-dev python3-pip python3.6-venv
RUN apt-get install -y vim
RUN python3.6 -m pip install pip --upgrade
RUN pip3 install pytest pytest-cache
RUN pip3 install pylint
RUN pip3 install requests

# Create working directory
RUN mkdir /testsuite

# Copy project
COPY comments_categories_api  /testsuite/comments_categories_api
COPY comments_posts_api  /testsuite/comments_posts_api/
RUN chmod -R a+rwX testsuite/
# Set working directory

WORKDIR /testsuite
# Set Python version
RUN echo alias python='/usr/bin/python3' >> ~/.bashrc
# RUN echo cd testsuite/ >> ~/.bashrc

# Define ENTRYPOINT
COPY ./docker-entrypoint.sh /testsuite/docker-entrypoint.sh
RUN ["chmod", "+x", "/testsuite/docker-entrypoint.sh"]
ENTRYPOINT ["sh", "/testsuite/docker-entrypoint.sh"] 

【问题讨论】:

  • 您可以发布用于运行容器的 docker run 命令吗?
  • docker run -it vip_app:v0.1 /bin/bash

标签: python-3.x docker docker-entrypoint


【解决方案1】:

以“$@”结束您的 docker-entrypoint.sh。这是一个例子:

#!/bin/bash

echo Hello

$@

=== 更新

根据您的评论,该文件应为:

#!/bin/bash

pytest -v

$@

【讨论】:

  • 我在 .sh 文件中有 pytest -v 。你是说在第二行加上 $@ 吗?
  • 你在windows上... :(((你的文件有Windows结束行字符。你应该把它保存为Linux格式然后它就可以工作了。或者在容器中安装“dos2unix”并在 ENTRYPOINT 之前运行“dos2unix /testsuite/docker-entrypoint.sh”作为构建的一部分
  • 您忘记安装该实用程序(我提到了安装和运行)。添加“RUN apt-get install -y dos2unix”以及安装其余软件包的其他命令。
  • 工作。我误解了。这做到了。 COPY ./docker-entrypoint.sh /testsuite/docker-entrypoint.sh RUN dos2unix /testsuite/docker-entrypoint.sh && apt-get --purge remove -y dos2unix
  • @RSSregex 您可以将此标记为已接受的答案吗?否则它看起来仍然是一个公开讨论
【解决方案2】:

在 ENTRYPOINT 之后,定义上,容器退出。

听起来你的意思是你想要一个容器,它首先运行一些测试,然后启动一个交互式外壳。您需要制作一个执行此操作的 shell 脚本

#!/bin/sh
pytest -v
sh

然后将该脚本作为镜像的主进程。


我这里有两种风格的 cmets,阅读其他 cmets 可能对你也很重要。您提到尝试使用

运行交互式shell
docker run -it vip_app:v0.1 /bin/bash

如果您使用 CMD 声明进程命令,您的 /bin/bash 命令将替换 CMD,您将获得一个交互式 shell。如果您使用 ENTRYPOINT 声明它,/bin/bash 将作为参数传递给 ENTRYPOINT(并且可能会被完全忽略)。如果我不明确需要两者,我倾向于更喜欢 CMD 而不是 ENTRYPOINT。

您还尝试使用.bashrc 文件更改默认的python 命令。 .bashrc 在许多常见情况下都不会被阅读。例如,如果您

docker run --rm vip_app:v0.1 python myapp.py

.bashrc 不会被读取,您将运行/usr/bin/python(可能是 Python 2.7)。我根本不会在图像中安装这样的“便利”东西。

【讨论】:

  • 我有这个我的 python 文件。 #!/usr/bin/python3.我可以从 Dockerfile 中删除它吗 - RUN echo alias python='/usr/bin/python3' >> ~/.bashrc。 ?
  • 既然你提到了CMD,我该如何替换Dockerfile中的ENTRYPOINT?在 Dockerfile 的末尾,我希望 pytest 在指定的文件夹中运行,并在容器提示符处。
  • 字面上只是用 CMD 替换 ENTRYPOINT。它们的语法相同。
猜你喜欢
  • 2012-07-21
  • 1970-01-01
  • 2023-03-14
  • 2021-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-22
相关资源
最近更新 更多