我有一个应用程序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/