简体   繁体   English

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

[英]Image is not running after setting a new docker image and then trying to run the project

I'm new to Docker and I'm trying to encapsulate my pytest python project into docker image, I've written the python code using pytest library in order to run the program. I'm new to Docker and I'm trying to encapsulate my pytest python project into docker image, I've written the python code using pytest library in order to run the program.

I've created the 'requirement.txt' file for docker.我为 docker 创建了“requirement.txt”文件。 This is how my 'dockerfile' looks like right now:这就是我的“dockerfile”现在的样子:

FROM python:3.8.3

ADD tests/test_class.py /

COPY requirements.txt ./

RUN pip install -r requirements.txt

But when I run the docker as following: docker run -it <image_id> py.test -s -v但是当我运行 docker 如下: docker run -it <image_id> py.test -s -v

It produces the following response:它产生以下响应:

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 ...

and it got stuck on the collecting... without running the test它卡在collecting...没有运行测试

when I go to the dashboard I notice that my image is not running当我 go 到仪表板时,我注意到我的图像没有运行

Use something like this:使用这样的东西:

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" ]

In the folder with your Python project code, create a Dockerfile similar to this:在包含 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 . ./

Then, run your tests in the container like this:然后,像这样在容器中运行测试:

docker run -it <image_id> pytest

First create a Dockerfile in the root directory as below首先在根目录下创建一个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

then build the image然后构建图像

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

Once this is done, take the image id:完成后,获取图像 ID:

docker images | grep my-pytest

then execute the command然后执行命令

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

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

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