簡體   English   中英

Docker 和 Django:Pytest 不運行

[英]Docker & Django: Pytest doesn't run

我正在運行 Docker 以在本地加載我的 Django 項目。 Pytest 在沒有 Docker 的情況下也能完美運行,但由於我使用的是 Docker,在我的容器 bash 中運行命令會帶來很多錯誤。

我的 docker-compose

version: '3'

    services:
      db:
        image: postgres
        ports:
          - "5432:5432"
      web:
        build: .
        env_file: .env
        volumes:
          - .:/code
        ports:
          - "8000:8000"
        depends_on:
          - db
        container_name: local

文件

# Pull base image
FROM python:3

# Set environment varibles
ENV PYTHONUNBUFFERED 1

# Set work directory
RUN mkdir /code
WORKDIR /code

# Install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
COPY ./Pipfile /code/Pipfile
RUN pipenv install --deploy --system --skip-lock --dev

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

# Copy project
COPY . /code/

我最初的想法是 docker 不讓 pytest 創建測試數據庫。 你有沒有遇到過這個問題並且可以告訴我如何解決它?

有故障的控制台日志 >

您已經省略了異常中最重要的部分,但是根據錯誤,我猜您還沒有運行數據庫遷移,因為 Postgres 數據庫沒有測試所需的表。

您使用什么容器映像來運行 python 代碼? (您的撰寫文件中似乎缺少它)。

根據棄用警告,您似乎調用了一個不存在的方法(即 callable 為 None)。 您的容器中是否安裝了所有依賴項?

此外,您是否有可用於安裝所有依賴項的 DockerFile?

就像是:

# Use an official Python runtime as a parent image
FROM python:3.6-slim

# Set the working directory to /app
WORKDIR /app

# copy requirements.txt
ADD ./requirements.txt /app/requirements.txt

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Copy the current directory contents into the container at /app
ADD . /app

然后在撰寫文件中,您可以像這樣構建它:

web:
  build:
    context: ./
    dockerfile: /path/to/Dockerfile
  command: cd /app/path/to/where/pytest/should/be/executed && pytest

請注意,此答案不依賴於主機安裝的卷,而是將您的代碼復制到容器中,並創建一個可以在任何地方使用的映像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM