title: Running local ansible to create local docker Image
body: <p>Directory Structure</p>
  <pre><code>build
  ├── docker-compose.dev.yml
  ├── playbooks
  │   ├── deploy-deps.yml
  │   └── setup-local-registry.yml
  ├── postgres
  │   └── DockerFile
  └── rabbitmq
      ├── DockerFile
      └── init.sh
  deploy-scripts
  ├── db-schema
  │   └── global
  │       ├── 01-init.sql
  │       ├── 02-tables.sql
  │       ├── 03-grant.sql
  │       └── 04-index.sql
  ├── rabbitmq
  │   └── global
  │       └── prepare-queues.sh
  </code></pre>
  <p>I'm trying to create docker image using ansible. Here is my playbook yml</p>
  <pre class="lang-yaml prettyprint-override"><code>- hosts: localhost
    vars:
      registry_host: &quot;localhost&quot;
      registry_port: 5000
      i_postgres_image: &quot;orderpostgres&quot;
    tasks:
      - name: &quot;Create &amp; Push Postgres Image&quot;
        docker_image:
          name: &quot;{{ i_postgres_image  }}&quot;
          build:
            path: &quot;../../build/postgres&quot;
          source: build
          state: present
        register: &quot;out&quot;
      - name: Show test output
        debug:
          msg: &quot;{{ out }}&quot;
  </code></pre>
  <p>My Postgres DockerFile</p>
  <pre><code>FROM postgres:10.17-buster
  COPY ../deploy-scripts/db-schema/global /docker-entrypoint-initdb.d/
  </code></pre>
  <p>When i execute ansible-playbook from the project root directory, this is result of the execution.</p>
  <pre><code>[WARNING]: No inventory was parsed, only implicit localhost is available
  [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
  
  PLAY [localhost] ***********************************************************************************************************************************************************************************************
  
  TASK [Gathering Facts] *****************************************************************************************************************************************************************************************
  ok: [localhost]
  
  TASK [Create &amp; Push Postgres Image] ****************************************************************************************************************************************************************************
  changed: [localhost]
  
  TASK [Show test output] ****************************************************************************************************************************************************************************************
  ok: [localhost] =&gt; {
      &quot;msg&quot;: {
          &quot;actions&quot;: [
              &quot;Built image orderpostgres:latest from ../../build/postgres&quot;
          ],
          &quot;changed&quot;: true,
          &quot;failed&quot;: false,
          &quot;image&quot;: null,
          &quot;stdout&quot;: &quot;&quot;,
          &quot;stdout_lines&quot;: []
      }
  }
  
  PLAY RECAP *****************************************************************************************************************************************************************************************************
  localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  </code></pre>
  <p>Based on the output I expected the docker image to be built and ready to push to local registry. But when I execute, <code>docker images</code> I'm not seeing that image at all.</p>
  <pre><code>docker images
  REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
  registry     2         b2cb11db9d3d   3 days ago   26.2MB
  </code></pre>
  <p>My analysis until now:</p>
  <p>My DockerFile has to copy some scripts from the root path of the project that is the reason it has &quot;../scripts/db-schema&quot;. I cannot change this path because the same DockerFile is being used by my <code>docker-compose.dev.yml</code> file inside the build directly for creating dev containers.</p>
  <p>I am guessing docker image creation is failing, but ansible module does not throw an error and provides output as success.</p>
  <p>Second, ansible docker module does not have config to customize the build context like <code>docker-compose</code> allows in the yml.</p>
  <p>Here is my <code>docker-compose.dev.yml</code> for reference</p>
  <pre class="lang-yaml prettyprint-override"><code>version: '3'
  services:
      
    redis:
      image: redis:5.0
      container_name: 'cache'
      ports: 
        - 6379:6379
      volumes: 
        - ~/.docker-volume/redis/data/:/var/lib/redis
  
    postgres:
      build:
        context: ../
        dockerfile: ./build/postgres/DockerFile
      container_name: 'database'
      restart: always
      environment:
        - POSTGRES_USER=haha
        - POSTGRES_PASSWORD=haha
        - POSTGRES_DB=haha
      logging:
        options: 
          max-size: 10m
          max-file: '3'
      ports: 
        - 5432:5432
      volumes: 
        - ~/.docker-volume/postgres/data/:/var/lib/postgresql/data
  </code></pre>
  <p>My Questions:</p>
  <ol>
  <li>How can i solve this problem ? Without rearranging possible and not changing the existing DockerFile</li>
  <li>How to configure ansible docker module to the custom context ?</li>
  <li>Why is ansible docker module is not throwing error ?</li>
  </ol>
  <p>Repro Repo: <a href="https://github.com/sathishsoundharajan/ansible-repro" rel="nofollow noreferrer">https://github.com/sathishsoundharajan/ansible-repro</a></p>
  <p>It has files required to reproduce the issue in anyone local</p>