从父目录构建 Dockerfile
Dockerfile build from parent directory
我有一个 python 应用程序,它使用我想要 dockerise 的 celery worker。不幸的是,python 和 Celery 都使用相同的代码库。尽管它们必须是单独的容器以便于扩展。如果我单独构建它们和 运行 容器,它工作正常。当我引入 docker-compose 时问题就开始了,因此我无法将代码文件夹中的 docker 文件换出,并且需要从文件夹外部构建容器。文件夹结构如下:
/application
/application-build
Dockerfile
/celery-build
Dockerfile
/code
application.py
otherfiles.py
我已经研究了一段时间,不幸的是,据我所知,无法将父目录中的文件复制到 docker 文件中。尽管我想将相同的代码复制到两个容器中。
(请注意以下 docker 文件与我使用的文件不完全相同。)
Dockerfile1:
FROM python:3.6
ADD /code .
COPY requirements.txt requirements.txt
RUN python3.6 -m pip install -r requirements.txt
CMD ["python3.6", "application.py"]
Dockerfile2:
FROM python:3.6
ADD /code .
COPY /code/requirements.txt .
RUN python3.6 -m pip install -r requirements.txt
CMD ["celery","-A application.celery", "worker","work1@host"]
我看到的一个解决方案是重新组织文件夹,使代码目录始终是 docker 文件目录的子目录,如下所示:
/application
/application-build
Dockerfile
/celery-build
Dockerfile
/code
application.py
otherfiles.py
虽然看起来一点也不聪明
没有问题。在 Dockerfile
中,您无法退出 构建上下文 的(访问父文件夹),而不是 Dockerfile's
文件夹。
保持结构不变,并明确指定 Dockerfile
:
的路径
docker build -t app -f application-build/Dockerfile .
docker build -t celery -f celery-build/Dockerfile .
在你的 Dockerfiles
中记住路径是 /application
。这样你就可以轻松复制:
Dockerfile
...
COPY code /code
...
用等同于 docker build
命令的 Docker Compose 补充葡萄的回答:
# docker-compose.yml
version: '3.7'
services:
application:
build:
context: .
dockerfile: application-build/Dockerfile
celery:
build:
context: .
dockerfile: celery-build/Dockerfile
# Run this command in the directory of your `docker-compose.yml` file
# (Which is this question's case would be /application/docker-compose.yml)
docker-compose build
构建每个 Docker 文件时,它将使用 /application
作为其构建上下文。
我有一个 python 应用程序,它使用我想要 dockerise 的 celery worker。不幸的是,python 和 Celery 都使用相同的代码库。尽管它们必须是单独的容器以便于扩展。如果我单独构建它们和 运行 容器,它工作正常。当我引入 docker-compose 时问题就开始了,因此我无法将代码文件夹中的 docker 文件换出,并且需要从文件夹外部构建容器。文件夹结构如下:
/application
/application-build
Dockerfile
/celery-build
Dockerfile
/code
application.py
otherfiles.py
我已经研究了一段时间,不幸的是,据我所知,无法将父目录中的文件复制到 docker 文件中。尽管我想将相同的代码复制到两个容器中。 (请注意以下 docker 文件与我使用的文件不完全相同。)
Dockerfile1:
FROM python:3.6
ADD /code .
COPY requirements.txt requirements.txt
RUN python3.6 -m pip install -r requirements.txt
CMD ["python3.6", "application.py"]
Dockerfile2:
FROM python:3.6
ADD /code .
COPY /code/requirements.txt .
RUN python3.6 -m pip install -r requirements.txt
CMD ["celery","-A application.celery", "worker","work1@host"]
我看到的一个解决方案是重新组织文件夹,使代码目录始终是 docker 文件目录的子目录,如下所示:
/application
/application-build
Dockerfile
/celery-build
Dockerfile
/code
application.py
otherfiles.py
虽然看起来一点也不聪明
没有问题。在 Dockerfile
中,您无法退出 构建上下文 的(访问父文件夹),而不是 Dockerfile's
文件夹。
保持结构不变,并明确指定 Dockerfile
:
docker build -t app -f application-build/Dockerfile .
docker build -t celery -f celery-build/Dockerfile .
在你的 Dockerfiles
中记住路径是 /application
。这样你就可以轻松复制:
Dockerfile
...
COPY code /code
...
用等同于 docker build
命令的 Docker Compose 补充葡萄的回答:
# docker-compose.yml
version: '3.7'
services:
application:
build:
context: .
dockerfile: application-build/Dockerfile
celery:
build:
context: .
dockerfile: celery-build/Dockerfile
# Run this command in the directory of your `docker-compose.yml` file
# (Which is this question's case would be /application/docker-compose.yml)
docker-compose build
构建每个 Docker 文件时,它将使用 /application
作为其构建上下文。