首页 > 解决方案 > 使用 ansible 构建时在 Dockerfile 中设置相对路径

问题描述

我正在尝试使用 ansible 构建一些 docker 文件,但不清楚用作构建目录的内容。

目录结构

我正在运行他的 build_images.yml 文件,如下所示(它选择了正确的路径)

build_images.yml 文件

 ---                                                                                                                                                                                                                                       
 - hosts: localhost
   connection: local

   tasks:
     - name: Build container
        docker_image:
        name: app
        path: deploy/dockerfiles/app
        pull: no
        state: build

但是,我的 Dockerfile 包含

FROM base_image:latest

COPY ./deploy/app_files/requirements.txt requirements.txt

我已经尝试了该目录的一些变体,但它不断返回“没有这样的文件或目录”

用于 Dockerfile 中的相对路径的适当的基本 docker 构建目录是什么?

标签: ansibledockerfile

解决方案


来自docker_image 文档的摘录

路径:与状态“存在”一起使用来构建图像。将是包含用于构建映像的上下文和 Dockerfile 的目录的路径。

dockerfile - 在 2.0 中添加:与 state present 一起使用,为构建映像时使用的 Dockerfile 提供备用名称。

然后我会尝试

- name: Build container
  docker_image:
    name: app
    path: deploy
    dockerfile: dockerfiles/app/Dockerfile
    pull: no
    state: build

在你的 Dockerfile 中:

FROM base_image:latest

COPY app_files/requirements.txt requirements.txt

请注意,您的原始路径无论如何都是错误的(指向dockerfiles而不是app_files)。


推荐阅读