如何通过提供外部文件作为参数来运行Docker容器

时间:2020-05-10 16:37:43

标签: python docker pytest

所以我有一个docker项目,它是某种Python pytest,它在可执行文件上运行子进程作为黑箱测试。我想构建容器,然后每次通过将可执行文件复制到容器WORKDIR内的专用文件夹(例如exec /)中来运行它。但是我不确定该怎么做。

当前,我必须首先将可执行文件包含在文件夹中,然后构建容器。

当前的结构如下:

my_test_repo
|   |-- exec
|   |   |-- my_executable
|   |-- tests
|   |   |-- test_the_executable.py
|   |-- Dockerfile

我跳过了其他一些设置,例如。

在Dockerfile中,我执行以下操作:

FROM python:3.7.7-buster


WORKDIR /app    
COPY . /app

RUN pip install --trusted-host pypi.python.org .
RUN pip install pytest

ENV NAME "Docker"

RUN pytest ./tests/ --executable=./exec/my_executable

最后一次,我设置了pytest固定装置以接受可执行文件的路径。

我可以通过构建测试来进行测试:

docker build --tag testproject:1.0 .

如何编辑它,以便容器仅包含所有测试文件。并且它与用户进行交互,这样我就可以将可执行文件从本地目录cp到容器中,然后运行测试?

非常感谢。

2 个答案:

答案 0 :(得分:0)

编辑容器是什么意思?

您可以使用docker cp命令将可执行文件从本地目录复制到容器。但一次只能复制一个文件。

docker cp path_of_executable/file_name docker_container_name:path_to_be_copy/

答案 1 :(得分:0)

如果我正确理解了您的问题,请将您的Dockerfile更改如下:

FROM python:3.7.7-buster


WORKDIR /app    
COPY tests/ /app

RUN pip install --trusted-host pypi.python.org .
RUN pip install pytest

ENV NAME "Docker"

ENTRYPOINT ["pytest", "./tests/"]
CMD []

然后入口点将在运行时(不是构建时)与传入的任何参数一起执行(由CMD处理。)

您可以像这样运行它(按照您的指示构建它之后):

docker testproject --executable=<path to executable>

here上有ENTRYPOINTCMD的文档。