'Setting relative paths in Dockerfile when building with ansible

I am trying to build some docker files using ansible, but it is not clear what is used as the build directory.

directory structure

.
├── build_images.yml
└── deploy
    ├── app_files
    │   └── requirements.txt
    └── dockerfiles
        └── app
            └── Dockerfile

I am running the build_images.yml playbook as follows (it picks up the correct path)

The build_images.yml file

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

However, my Dockerfile contains

FROM base_image:latest

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

I have tried a few variations of the directory, but it keeps coming back with:

no such file or directory

What is the appropriate base docker build directory to use for relative paths in the Dockerfile?



Solution 1:[1]

Extracts from the docker_image documentation

path: Use with state 'present' to build an image. Will be the path to a directory containing the context and Dockerfile for building an image.

dockerfile - added in 2.0: Use with state present to provide an alternate name for the Dockerfile to use when building an image.

I would then try

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

And in your Dockerfile:

FROM base_image:latest

COPY app_files/requirements.txt requirements.txt

Note that your original path was wrong anyway (pointing to dockerfiles rather than app_files).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1