python - 防止docker-compose在使用构建镜像时重新安装requirements.txt

标签 python python-3.x docker docker-compose dockerfile

我有一个应用程序ABC,我想将它放在docker环境中。我构建了一个Dockerfile并获得了在docker-compose.yml中使用的图像abcd1234
但是在尝试构建docker-compose时,将重新安装所有requirements.txt文件。它不能使用已经存在的镜像并妨碍重新安装它吗?

我是docker的新手,正在尝试了解所有参数。另外,“上下文”是否正确?在docker-compose.yml中还是应该包含Image中的路径?

PS,我的docker-compose.yml不在项目的同一目录中,因为我将使用多个图像公开更多端口。

docker-compose.yml:


services:
  app:
    build:
      context: /Users/user/Desktop/ABC/
    ports:
      - "8000:8000"
    image: abcd1234
    command: >
      sh -c "python manage.py migrate &&
             python manage.py runserver 0.0.0.0:8000"
    environment:
      - PROJECT_ENV=development

Dockerfile:
FROM python:3.6-slim-buster AS build
MAINTAINER ABC

ENV PYTHONUNBUFFERED 1

RUN python3 -m venv /venv
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y git && \
    apt-get install -y build-essential && \
    apt-get install -y awscli && \
    apt-get install -y unzip && \
    apt-get install -y nano

RUN apt-get install -y libsm6 libxext6 libxrender-dev

COPY . /ABC/

RUN apt-cache search mysql-server
RUN apt-cache search libmysqlclient-dev
RUN apt-get install -y libpq-dev
RUN apt-get install -y postgresql
RUN apt-cache search postgresql-server-dev-9.5


RUN pip install --upgrade awscli==1.14.5 s3cmd==2.0.1 python-magic
RUN pip install -r /ABC/requirements.txt

WORKDIR .

请指导我如何解决这两种情况。谢谢!

最佳答案

context:目录是主机系统上包含Dockerfile的目录。这是您传递给docker build的目录,并且通常只是当前目录.

在Dockerfile中,Docker可以缓存单独的构建步骤,以使其不再重复,而仅在达到某些更改为止。该“内容”可以是已更改的RUN行,但是在COPY的点上,如果本地源代码树中的任何文件发生了任何更改,这也会使缓存中的所有内容失效。

因此,典型的Dockerfile具有两个“阶段”。您也可以用其他语言重复此模式。您可以按以下顺序重组Dockerfile:

# 1. Base information; this almost never changes
FROM python:3.6-slim-buster AS build
MAINTAINER ABC
ENV PYTHONUNBUFFERED 1
WORKDIR /ABC

# 2. Install OS packages.  Doesn't depend on your source tree.
# Frequently just one RUN line (but could be more if you need
# packages that aren't in the default OS package repository).
RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get upgrade -y && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y \
      build-essential unzip libxrender-dev libpq-dev

# 3. Copy _only_ the file that declares language-level dependencies.
# Repeat starting from here only if this file changes.
COPY requirements.txt .
RUN pip install -r requirements.txt

# 4. Copy the rest of the application in.  In a compiled language
# (Javascript/Webpack, Typescript, Java, Go, ...) build it.
COPY . .

# 5. Explain how to run the application.
EXPOSE 8000
CMD python manage.py migrate && \
    python manage.py runserver 0.0.0.0:8000

关于python - 防止docker-compose在使用构建镜像时重新安装requirements.txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60757498/

相关文章:

python - Elasticsearch批量插入失败时回滚

python - 如何使用cron调度运行python程序

python - 如何使用 importlib 在 python 中动态导入模块?

python - "from __future__ import annotations"在 VSCode 中产生 "annotations is not defined"

docker - 如何在我的私有(private) Docker 注册表中查看图像标签名称

python - 在 docker python 上安装 libLAS - 没有这样的文件或目录

python - 以大写字母拆分字符串

在不知道其名称的情况下获得第一个通过的 kwarg 的 Pythonic 方法?

python - 将文本从 Word 复制到 Python 不需要转义字符,为什么?

mysql - 无法从 Sequelize.js 连接,可以从 MySQL 客户端连接