加载构建上下文时 Docker“无法解决:已取消:上下文已取消”

Vic*_*r B 7 docker dockerfile docker-compose

运行时docker-compose up --build我不断收到此错误。

 => [web internal] load build definition from Dockerfile                                                           0.0s
 => => transferring dockerfile: 1.58kB                                                                             0.0s
 => [web internal] load .dockerignore                                                                              0.0s
 => => transferring context: 279B                                                                                  0.0s
 => [web internal] load metadata for docker.io/library/python:3.8                                                  0.8s
 => CANCELED [web  1/14] FROM docker.io/library/python:3.8@sha256:7a82536f5a2895b70416ccaffc49e6469d11ed8d9bf6bcf  0.3s
 => => resolve docker.io/library/python:3.8@sha256:7a82536f5a2895b70416ccaffc49e6469d11ed8d9bf6bcfc52328faeae7c77  0.2s
 => => sha256:795c73a8d985b6d1b7e5730dd2eece7f316ee2607544b0f91841d4c4142d9448 7.56kB / 7.56kB                     0.0s
 => => sha256:7a82536f5a2895b70416ccaffc49e6469d11ed8d9bf6bcfc52328faeae7c7710 1.86kB / 1.86kB                     0.0s
 => => sha256:129534c722d189b3baf69f6e3289b799caf45f75da37035c854100852edcbd7d 2.01kB / 2.01kB                     0.0s
 => CANCELED [web internal] load build context                                                                     0.1s
 => => transferring context: 26.00kB                                                                               0.0s
failed to solve: Canceled: context canceled
Run Code Online (Sandbox Code Playgroud)

这种情况从昨天开始就发生了,在我的图像构建完成并且我的应用程序运行良好之前。

  1. 尝试识别任何干扰 docker 构建过程的进程/应用程序
  2. 完全重新安装 Docker Desktop
  3. 尝试更改我的 Dockerfile 中的 Python 版本

我很迷茫,不知道出了什么问题,我以前用过python3.8python3.9没有问题。下面是我的 dockerfile 和 docker-compose。

# Use an official Python runtime as the base image
FROM python:3.8

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV NODE_ENV production

# Create and set the working directory
WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
        default-mysql-client \
        software-properties-common \
        curl \
        gnupg \
    && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor -o /usr/share/keyrings/nodesource.gpg \
    && echo "deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x bullseye main" | tee /etc/apt/sources.list.d/nodesource.list \
    && apt-get update && apt-get install -y nodejs \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Verify that Node.js and npm are installed
RUN node --version
RUN npm --version

# Install Python dependencies
COPY requirements.txt .
RUN pip install --upgrade pip && \
    pip install -r requirements.txt

# Install Tailwind CSS and its peer dependencies
COPY package.json package-lock.json ./
RUN npm install

# Copy the current directory contents into the container
COPY . .

# Build the Tailwind CSS
RUN npm run build:css

RUN npm run watch:css

COPY entrypoint.sh ./entrypoint.sh
RUN chmod +x ./entrypoint.sh

ENTRYPOINT ["./entrypoint.sh"]

# The main command to run when the container starts
CMD ["gunicorn", "--bind", ":8000", "bmlabs.wsgi:application"]
Run Code Online (Sandbox Code Playgroud)

Docker-compose


services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 
      MYSQL_DATABASE:
      MYSQL_USER: 
      MYSQL_PASSWORD:
    networks:
      - backend

  web:
    build: .
    volumes:
      - .:/app
      - static_volume:/app/staticfiles
      - media_volume:/app/mediafiles
    depends_on:
      - db
    networks:
      - backend
    expose:
      - "8000"

  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/bmlabs.conf:/etc/nginx/conf.d/bmlabs.conf:ro
      - static_volume:/app/staticfiles
      - media_volume:/app/mediafiles
    depends_on:
      - web
    networks:
      - backend

networks:
  backend:

volumes:
  db_data:
  cache_data:
  static_volume:
  media_volume:
Run Code Online (Sandbox Code Playgroud)

我使用的是 Windows 10。

Vic*_*r B 12

尽管降级 docker 有效,但实际问题是我没有在 Dockerignore 中排除 node_modules。我已经运行容器很长时间了。

清除所有容器和图像并将该node_modules/行添加到我的容器和图像后.dockerignore,它修复了它。

我猜测该错误与某些目录内的文件数量有关。

  • 在 python 项目中,我发现了类似的解决方案:将 `venv/` 目录添加到 `.dockerignore` 有所帮助。 (3认同)