Molecule: Docker networks usage

Created on 27 Feb 2018  ·  7Comments  ·  Source: ansible-community/molecule

Issue Type

  • Bug report

Molecule and Ansible details

ansible 2.4.3.0
molecule, version 2.9.0
  • Molecule installation method: pip
  • Ansible installation method: pip

Desired Behaviour

I need run two or more containers. One of them is a server and the others are agents. The agents need connect to the server.

I configure a molecule.yml file like this:

dependency:
  name: galaxy
driver:
  name: docker
lint:
  name: yamllint
platforms:
  - name: molecule_rancher_server
    image: solita/ubuntu-systemd
    privileged: True
    required: True
    exposed_ports:
      - "80:80"
      - "8088:8080"
    volumes:
      - "/sys/fs/cgroup:/sys/fs/cgroup:ro"
    command: /lib/systemd/systemd
    networks:
      - name: rancher
    groups:
      - server
  - name: molecule_rancher_agent_1
    image: solita/ubuntu-systemd
    privileged: True
    required: True
    volumes:
      - "/sys/fs/cgroup:/sys/fs/cgroup:ro"
    command: /lib/systemd/systemd
    networks:
      - name: rancher
    groups:
      - node
provisioner:
  name: ansible
  lint:
    name: ansible-lint
scenario:
  name: hsap
verifier:
  name: testinfra
  lint:
    name: flake8

I need all containers on the same network for connectivity.

Actual Behaviour (Bug report only)

I need create the Docker network before launch molecule create:

docker network create rancher

If the network doesn't exists when I execute molecule --debug create I get the error:

"msg": "Parameter error: network named rancher could not be found. Does it exist?"

When I destroy the instances I need to remove the networks parameters from molecule.yml
If I don't remove the networks parameters I get the next error:

 docker.errors.NotFound: 404 Client Error: Not Found ("{"message":"network rancher not found"}")
upstream-bug

All 7 comments

You need to update your create and destroy.yml playbooks. Moleule added this functionality in v2.8 and usage.

I did it and it doesn't works.

I'm trying figure out what is happening. I isolate the problem, but I'm no sure if it's a molecule issue or ansible issue.

On my computer I have this Docker networks:

NETWORK ID          NAME                    DRIVER              SCOPE
088d04bbdb94        andres_default          bridge              local
8afe941d4c04        andres_system_traefik   bridge              local
b6b77006548c        bridge                  bridge              local
3c9a49b4ef69        host                    host                local
8d9ff1c4089b        none                    null                local
f41fb7cb4d6e        rancherserver_default   bridge              local

Molecule doesn't works if I used rancher or andres as network name. If I use another name it works.

I can't reproduce.

[jodewey:~/git/molecule_2/test/scenarios/driver/docker] [molecule2-env-2.7.13] vagrant-configuration-changes(+2/-2)+ ± docker network create rancher
b7ce429859a592d17055469c19e714d8d3bf2ff03c94f4682d0bb0af2d941f51

[jodewey:~/git/molecule_2/test/scenarios/driver/docker] [molecule2-env-2.7.13] vagrant-configuration-changes(+2/-2)+ 130 ± molecule create
--> Test matrix

└── default
    ├── create
    └── prepare

--> Scenario: 'default'
--> Action: 'create'

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

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

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

    TASK [Build an Ansible compatible image] ***************************************
    changed: [localhost] => (item=None)

    TASK [Create docker network(s)] ************************************************
    ok: [localhost] => (item=None)

    TASK [Create molecule instance(s)] *********************************************
    changed: [localhost] => (item=None)

    TASK [Wait for instance(s) creation to complete] *******************************
    changed: [localhost] => (item=None)

    PLAY RECAP *********************************************************************
    localhost                  : ok=6    changed=3    unreachable=0    failed=0


--> Scenario: 'default'
--> Action: 'prepare'

    PLAY [Prepare] *****************************************************************

    PLAY RECAP *********************************************************************


[jodewey:~/git/molecule_2/test/scenarios/driver/docker] [molecule2-env-2.7.13] vagrant-configuration-changes(+2/-2)+ ± docker network list
NETWORK ID          NAME                DRIVER              SCOPE
25dc9b90cf0d        bridge              bridge              local
36c27c5bd6ae        host                host                local
4b992129e016        none                null                local
b7ce429859a5        rancher             bridge              local
[jodewey:~/git/molecule_2/test/scenarios/driver/docker] [molecule2-env-2.7.13] vagrant-configuration-changes(+2/-2)+ ± docker inspect --format='{{json .NetworkSettings.Networks}}' 31ab5906e65c
{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"25dc9b90cf0d5989c1538cab823fb6390bf51cf15dd15c4bb9e7cee90054423d","EndpointID":"9a92fa07238cfff3ffb899c8b79a79f711421331cdfe032720cc480af3f60091","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02","DriverOpts":null},"rancher":{"IPAMConfig":null,"Links":null,"Aliases":["31ab5906e65c"],"NetworkID":"b7ce429859a592d17055469c19e714d8d3bf2ff03c94f4682d0bb0af2d941f51","EndpointID":"3eaabe2864058f01823fa57672c4dd6c36a219ead4c556ff86adbfcf5e87d9b9","Gateway":"172.18.0.1","IPAddress":"172.18.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:12:00:02","DriverOpts":null}}

I can reproduce with this steps:

  • create first a similar network:
sudo docker network create ranchersomestring
  • Run molecule:
molecule create

You're going to need to share more info. Lets see your molecule.yml and create.yml.

molecule.yml

dependency:
  name: galaxy
driver:
  name: docker
lint:
  name: yamllint
platforms:
  - name: molecule_rancher_server
    image: solita/ubuntu-systemd
    privileged: True
    required: True
    exposed_ports:
      - "8080"
    published_ports:
      - "8088:8080"
    volumes:
      - "/sys/fs/cgroup:/sys/fs/cgroup:ro"
    command: /lib/systemd/systemd
    networks:
      - name: rancher
    groups:
      - server
  - name: molecule_rancher_agent_1
    image: solita/ubuntu-systemd
    privileged: True
    required: True
    volumes:
      - "/sys/fs/cgroup:/sys/fs/cgroup:ro"
    command: /lib/systemd/systemd
    networks:
      - name: rancher
    groups:
      - node
provisioner:
  name: ansible
  lint:
    name: ansible-lint
scenario:
  name: default
verifier:
  name: testinfra
  lint:
    name: flake8

create.yml

- name: Create
  hosts: localhost
  connection: local
  gather_facts: false
  no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
  vars:
    molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
    molecule_ephemeral_directory: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}"
    molecule_scenario_directory: "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}"
    molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
  tasks:
    - name: Create Dockerfiles from image names
      template:
        src: "{{ molecule_scenario_directory }}/Dockerfile.j2"
        dest: "{{ molecule_ephemeral_directory }}/Dockerfile_{{ item.image | regex_replace('[^a-zA-Z0-9_]', '_') }}"
      with_items: "{{ molecule_yml.platforms }}"
      register: platforms

    - name: Discover local Docker images
      docker_image_facts:
        name: "molecule_local/{{ item.item.name }}"
      with_items: "{{ platforms.results }}"
      register: docker_images

    - name: Build an Ansible compatible image
      docker_image:
        path: "{{ molecule_ephemeral_directory }}"
        name: "molecule_local/{{ item.item.image }}"
        dockerfile: "{{ item.item.dockerfile | default(item.invocation.module_args.dest) }}"
        force: "{{ item.item.force | default(true) }}"
      with_items: "{{ platforms.results }}"
      when: platforms.changed or docker_images.results | map(attribute='images') | select('equalto', []) | list | count >= 0

    - name: Create docker network(s)
      docker_network:
        name: "{{ item }}"
        state: present
      with_items: "{{ molecule_yml.platforms | molecule_get_docker_networks }}"

    - name: Create molecule instance(s)
      docker_container:
        name: "{{ item.name }}"
        hostname: "{{ item.name }}"
        image: "molecule_local/{{ 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 }}"

@asg1612 looks like you're running into an Ansible bug.

Was this page helpful?
0 / 5 - 0 ratings