Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker Bridge IP - Conflict with default 172.17.0.1 #37

Open
jifox opened this issue Apr 23, 2020 · 0 comments
Open

Docker Bridge IP - Conflict with default 172.17.0.1 #37

jifox opened this issue Apr 23, 2020 · 0 comments

Comments

@jifox
Copy link

jifox commented Apr 23, 2020

Hello,

I'm focussing a confilict with docker installation. The bridge ip is in conflict with an existing network.

I'm currently using following playbook for testing with ubuntu 20 server. In my playbook I'm using the last octet of the IP address ansible_default_ipv4.address and append it to docker_bridge_network to prevent duplicate IPs.

Whats missing in this playbook is the management of an already existing /etc/docker/daemon.json file, because the daemon.json entry { "bip": "{{ bip_ipaddr }}" } must be present before docker installation. Otherwise the docker daemon is starting with the conflicting default ip address.

What do you think?

  • Will it be better to manually define the bridge ips for all nodes. Or stick to the last octeet and accept the restriction of max 253 nodes in cluster

  • Should I open a pull request?

kr
Josef

Defined in group_vars:

# Bridge Network IPv4 used as "bip" in /docker/daemon.json
# The default value if not defined is 172.17.0.0/16 
docker_bridge_network: 172.140.0.0/24

Included tasks to install docker on ubuntu 20 focal:

# Install docker
#
# Parameters:
#    docker_bridge_network (optional) - ipv4/cidr e.g. 172.140.0.0/24

##########################################################################
# Initialization - prepare docker bridge network
##########################################################################

- name: Calculate Docker Bridge IP (bip_addr)
  block:
    - name: Ensure valid IP/CIDR specified in docker_bridge_network
      assert:
        msg: |
          'docker_bridge_network' must contain a valid ipv4/cidr network address!
                                  e.g. docker_bridge_network: 172.140.0.0/24
        that:
          - docker_bridge_network | ipaddr(False)
    - name: Prepare calculation arguments
      set_fact:
        ip_last_octett: "{{ ansible_default_ipv4.address.split('.')[3].split('/') | first }}"
        bip_parts: "{{ docker_bridge_network.split('.') }}"
    - name: Calculate docker bridge ip
      set_fact:
        bip_ipaddr: "{{ bip_parts[0] ~ '.' ~
                        bip_parts[1]  ~ '.' ~
                        bip_parts[2]  ~ '.' ~
                        ip_last_octett  ~ '/' ~
                        bip_parts[3].split('/')[1]
                        }}"
    - name: Check that bridge ip is a valid IP/CIDR addr
      assert:
        msg: |
          ERROR: 'bip_ipaddr' is not a valid ipv4/cidr address!
        that:
          - bip_ipaddr | ipaddr(False)
    - debug:
        msg: "Docker Bridge Address: {{ bip_ipaddr }}"
  when:
    - docker_bridge_network is defined

- name: Setup /etc/docker/daemon.json file if needed
  block:
    - name: Define filename for docker daemon initialization
      set_fact:
        daemon_filename: "/etc/docker/daemon.json"

    - name: Ensure docker directory exists
      file:
        path: /etc/docker
        state: directory
      become: true

    - name: Check that sudoers file exists
      stat:
        path: "{{ daemon_filename }}"
      register: res_daemon_filename
      become: true

    - name: Set flag daemon_file_missing
      set_fact:
        daemon_file_missing: "{{ not (res_daemon_filename.stat.isreg | default(false)) | bool }}"
    - name: Setup bridge ip for docker daemon
      copy:
        dest: "{{ daemon_filename }}"
        content: |
          { "bip": "{{ bip_ipaddr }}" }
      when:
        - daemon_file_missing
      become: true
  when:
    - docker_bridge_network is defined


##########################################################################
# Install docker-ce repository
##########################################################################

- name: Add Docker GPG key
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
    state: present

# //TODO: Add focal repository when available

- name: Install docker and requirements
  apt:
    pkg:
      - apt-transport-https
      - ca-certificates
      - curl
      - software-properties-common
      - docker.io
      - docker-compose
    state: present
    update_cache: true
  become: true

- name: Enable and start service docker
  service:
    name: docker
    enabled: true
    state: started
  become: true

##########################################################################
# Verify docker installation
##########################################################################

- name: Verify installation
  shell: 
    cmd: "{{ item }}"
  become: true
  loop:
    - docker version
    - docker info
    - docker network ls
    - ip link
    - bridge link
    - docker run --rm hello-world
    - docker run --rm alpine cat /etc/resolv.conf
    - docker run --rm alpine ping -c1 8.8.8.8
  changed_when: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant