设置新的docker映像然后尝试运行项目后,映像未运行

时间:2020-06-27 18:57:30

标签: python docker pytest

我是Docker的新手,正在尝试将pytest python项目封装到docker映像中,我已经使用pytest库编写了python代码以运行该程序。

我已经为docker创建了“ requirement.txt”文件。 这就是我的“ dockerfile”现在的样子:

FROM python:3.8.3

ADD tests/test_class.py /

COPY requirements.txt ./

RUN pip install -r requirements.txt

但是当我按以下方式运行docker时: docker run -it <image_id> py.test -s -v

它会产生以下响应:

platform linux -- Python 3.8.3, pytest-5.4.3, py-1.8.2, pluggy-0.13.1 -- /usr/local/bin/python
cachedir: .pytest_cache
metadata: {'Python': '3.8.3', 'Platform': 'Linux-4.19.76-linuxkit-x86_64-with-glibc2.2.5', 'Packages': {'pytest': '5.4.3', 'py': '1.8.2', 'pluggy': '0.13.1'}, 'Plugins': {'html': '2.1.1', 'metadata': '1.10.0'}}
rootdir: /
plugins: html-2.1.1, metadata-1.10.0
collecting ...

它卡在collecting ...上而没有运行测试

当我转到仪表板时,我注意到我的图像未运行

3 个答案:

答案 0 :(得分:0)

使用类似这样的内容:

FROM python:3.6

# Create app directory
WORKDIR /app

# Install app dependencies
COPY src/requirements.txt ./

RUN pip install -r requirements.txt

# Bundle app source
COPY src /app

EXPOSE 8080
CMD [ "python", "server.py" ]

答案 1 :(得分:0)

在包含Python项目代码的文件夹中,创建类似于以下内容的Dockerfile:

# Starting from Python 3 base image
FROM python:3

# Set the WORKDIR to /app so all following commands run in /app
WORKDIR /app

# Adding (dev-)requirements files before installing requirements
COPY requirements.txt dev-requirements.txt ./

# Install requirements and dev requirements through pip. Those should include
# nostest, pytest or any other test framework you use
RUN pip install -r requirements.txt -r dev-requirements.txt

# Adding the rest of your project code to the image
COPY . ./

然后,像这样在容器中运行测试:

docker run -it <image_id> pytest

答案 2 :(得分:0)

首先在根目录中创建一个Dockerfile,如下所示:

FROM python:3.6

# Create app directory
WORKDIR /app

# copy the requirements file to the working directory 
COPY requirements.txt ./

# then install the requirements before running the app
RUN pip install --no-cache-dir -r requirements.txt

# copy the rest of the file
COPY . /app

然后创建图像

docker build -t my-pytest . # here . indicates the path to your Dockerfile

完成后,获取图像ID:

docker images | grep my-pytest

然后执行命令

docker run -it <IMAGE ID> py.test - s -v