从父目录构建 Dockerfile

Dan*_*cze 3 python celery docker dockerfile

我有一个 python 应用程序,它使用我想要 dockerise 的 celery 工人。不幸的是,python 和 Celery 都使用相同的代码库。尽管它们必须是单独的容器以便于扩展。如果我单独构建它们并运行容器它工作正常。当我引入 docker-compose 时,问题就开始了,因此我无法在代码文件夹中交换 dockerfile,并且需要从文件夹外部构建容器。文件夹结构如下:

/application
    /application-build
       Dockerfile
    /celery-build
       Dockerfile
    /code
       application.py
       otherfiles.py
Run Code Online (Sandbox Code Playgroud)

我现在一直在挖掘,不幸的是,据我所知,父目录中的文件无法复制到 dockerfile 中。虽然我想将相同的代码复制到两个容器中。 (请注意,以下 dockerfiles 不是我使用的确切文件。)

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"]
Run Code Online (Sandbox Code Playgroud)

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"]
Run Code Online (Sandbox Code Playgroud)

我看到的一种解决方案是重新组织文件夹,以便代码目录始终是 dockerfile 目录的子目录,如下所示:

/application
   /application-build
      Dockerfile
      /celery-build
         Dockerfile
         /code
            application.py
            otherfiles.py
Run Code Online (Sandbox Code Playgroud)

虽然看起来一点都不聪明。

gra*_*pes 13

没有问题。在Dockerfile您无法退出(访问父文件夹)构建上下文,而不是Dockerfile's文件夹。

保持结构不变,并明确指定路径Dockerfile

docker build -t app -f application-build/Dockerfile . 
docker build -t celery -f celery-build/Dockerfile . 
Run Code Online (Sandbox Code Playgroud)

在你的Dockerfiles记忆中,路径是/application. 因此,您可以轻松复制:

文件

...
COPY code /code
...
Run Code Online (Sandbox Code Playgroud)


Jam*_*rch 8

使用相当于以下命令的 Docker Compose 来补充葡萄的答案docker build

# docker-compose.yml
version: '3.7'

services:
  application:
    build:
      context: .
      dockerfile: application-build/Dockerfile

  celery:
    build:
      context: .
      dockerfile: celery-build/Dockerfile
Run Code Online (Sandbox Code Playgroud)
# docker-compose.yml
version: '3.7'

services:
  application:
    build:
      context: .
      dockerfile: application-build/Dockerfile

  celery:
    build:
      context: .
      dockerfile: celery-build/Dockerfile
Run Code Online (Sandbox Code Playgroud)

当每个 Dockerfile 构建时,它将用作/application其构建上下文。