Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build Docker image using Ansible Playbook (with multi-target Dockerfile)

Is there any option how to implement Docker command (--target parameter)

docker build --target backend -t project/backend:latest

in Ansible Playbook with docker_image module? Right now in playbook:

- hosts: localhost
  tasks:
    - name: Build backend image
      docker_image:
        path: /var/lib/workspace/project/backend
        name: project/backend:latest
like image 661
artificialis_me Avatar asked Oct 20 '25 13:10

artificialis_me


2 Answers

This worked for me.

---
- name: Copy Dockerfile
  copy: src=Dockerfile dest=/tmp/path/
- name: Build osquery image
    docker_image:
      path: /tmp/path/
      name: imagename
      tag: v1
like image 121
sudheerchamarthi Avatar answered Oct 22 '25 03:10

sudheerchamarthi


In the current version of Ansible it is possible to define the target in docker_image.

Ansible Documentation - community.docker.docker_image

I think this should work for you

- hosts: localhost
  tasks:
    - name: Build backend image
      docker_image:
        path: /var/lib/workspace/project/backend
        name: project/backend:latest
        target: backend

When the Dockerfile is something like

FROM for-example-php-image AS baseimage 
# Common things

FROM baseimage AS backend
# Things needed only in backend
like image 22
Gemmu Avatar answered Oct 22 '25 04:10

Gemmu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!