Molecule: Using a local Docker image

Created on 6 Apr 2018  路  5Comments  路  Source: ansible-community/molecule

Issue Type

  • Question

Molecule and Ansible details

Molecule 2.12.1
Ansible 2.5.0

Molecule installation method:

  • pip

Ansible installation method:

  • OS package

Question

I'm using Docker to run molecule.

I'm trying to run tests using a local image, but I seemingly can't. I have created an image "my-image".

$ docker images
REPOSITORY      TAG       IMAGE ID            CREATED             SIZE
... 
my-image        latest    b75e17a94136        About an hour ago   217MB
...

And in molecule.yml I have the following platforms (everything else is default):

platforms:
  - name: my-container
    image: my-image:latest

When I then run molecule test I get the error:

--> Action: 'create'

    PLAY [Create] ******************************************************************

    TASK [Log into a Docker registry] **********************************************
    skipping: [localhost] => (item=None) 

    TASK [Create Dockerfiles from image names] *************************************
    changed: [localhost] => (item=None)

    TASK [Discover local Docker images] ********************************************
    ok: [localhost] => (item=None)

    TASK [Build an Ansible compatible image] ***************************************
    failed: [localhost] (item=None) => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": false}

    PLAY RECAP *********************************************************************
    localhost                  : ok=2    changed=1    unreachable=0    failed=1   

Running molecule --debug test provides a long error including "msg": "Error building molecule_local/my-image - code: None, message: pull access denied for my-image, repository does not exist or may require 'docker login', logs: [u'Step 1/2 : FROM my-image:latest', u'\\n']"

So seemingly it is trying to download the image, instead of using the local image. However, I can make it work this way: I go into create.yml, remove all tasks except for Create molecule instance(s) and Wait for instance(s) creation to complete, where I replace image: "molecule_local/{{ item.image }}" with image: "{{ item.image }}". This results in the following create.yml:

- name: Create
  hosts: localhost
  connection: local
  gather_facts: false
  no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
  tasks:
    - name: Create molecule instance(s)
      docker_container:
        name: "{{ item.name }}"
        hostname: "{{ item.name }}"
        image: "{{ item.image }}"
        state: started
        recreate: false
        log_driver: json-file
        command: "{{ item.command | default('bash -c \"while true; do sleep 10000; done\"') }}"
        privileged: "{{ item.privileged | default(omit) }}"
        volumes: "{{ item.volumes | default(omit) }}"
        capabilities: "{{ item.capabilities | default(omit) }}"
        exposed_ports: "{{ item.exposed_ports | default(omit) }}"
        published_ports: "{{ item.published_ports | default(omit) }}"
        ulimits: "{{ item.ulimits | default(omit) }}"
        networks: "{{ item.networks | default(omit) }}"
        dns_servers: "{{ item.dns_servers | default(omit) }}"
      register: server
      with_items: "{{ molecule_yml.platforms }}"
      async: 7200
      poll: 0

    - name: Wait for instance(s) creation to complete
      async_status:
        jid: "{{ item.ansible_job_id }}"
      register: docker_jobs
      until: docker_jobs.finished
      retries: 300
      with_items: "{{ server.results }}"

Now I can run molecule test and it works as intended: It creates an instance, runs the role, runs tests, and exits without errors.

It seems to me that it's supposed to work without modifying the create.yml in the way I did, so I wonder what I did wrong. How was I supposed to use a local Docker image?

Most helpful comment

I was running into the same issue and it appears to be related to a parameter in the "Build Ansible Compatible Image" task.
Specifically, that task is using the Ansible docker_image module which has the pull parameter set to true by default, which says: "When building an image downloads any updates to the FROM image in Dockerfile."
I haven't been using molecule for long so this was the first time I had run it with a local image and hit this issue, but that pull parameter has been in that Ansible module for several versions, so not sure if it just didn't show up before or what.

TLDR;
In any case, adding pull: false to the "Build an Ansible compatible image task" in the create.yml file solves the issue.

All 5 comments

I was running into the same issue and it appears to be related to a parameter in the "Build Ansible Compatible Image" task.
Specifically, that task is using the Ansible docker_image module which has the pull parameter set to true by default, which says: "When building an image downloads any updates to the FROM image in Dockerfile."
I haven't been using molecule for long so this was the first time I had run it with a local image and hit this issue, but that pull parameter has been in that Ansible module for several versions, so not sure if it just didn't show up before or what.

TLDR;
In any case, adding pull: false to the "Build an Ansible compatible image task" in the create.yml file solves the issue.

Thanks @rollerd. @Uwila please give @rollerd's suggestion a try.

Sorry for the late response. I tried it and it seems to work the way I wanted. Thanks @rollerd!

Adding pull: false fixed an issue in my CI environment. Thanks @rollerd!

Thanks @tdgroot ! Just had this issue, fixed on my local copy, and I see you proposed a patch 4 hours ago. Incredible! :+1:

Was this page helpful?
0 / 5 - 0 ratings