Deploying Nextcloud on Docker with Ansible and give it a static IP address

Hey,

i’m on writing an Ansible Script to automate Deploying Docker with a static IP Address. So far it working fine. But i have problems connecting to the Database.

I don’t know what to write into the MYSQL_HOST Variable. if i give the MariaDB own IP Adress it is working but i don’t want that because it uses to much Ip addresses i guess. If i give just doing it with ports on the docker machine its working 2.

anyone an idear how i do it with one IP address and maybe a hint how i can activate ssh?


---
- hosts: docker
  vars:
    db_volume: mariadb
    nextcloud: nextcloud
  tasks:
    - name: Deploy MariaDB server
      docker_container:
        image: mariadb
        name: mariadb
        volumes:
          - "{{ db_volume }}:/var/lib/mysql"
        env:
          MYSQL_ROOT_PASSWORD: somerootpassword
          MYSQL_PASSWORD: somemysqlpassword
          MYSQL_DATABASE: db
          MYSQL_USER: mysqluser

    - name: Deploy Nextcloud
      docker_container:
        image: nextcloud
        name: nextcloud
        restart_policy: always
        networks:
        - name: virtualswitch
          ipv4_address: "10.0.100.102"
        links:
          - "mariadb:{{ db_volume }}"
        volumes:
          - "{{ nextcloud }}:/var/www/html"
        env:
          MYSQL_PASSWORD: somemysqlpassword
          MYSQL_DATABASE: db
          MYSQL_USER: mysqluser
          MYSQL_HOST: "10.0.100.6"

hi @naseweis welcome to the forum :handshake:

In general Docker is not designed to work with fixed IP addresses it uses integrated DNS service for inter-container communication. I would recommend you connect the services using container name or service name if you use docker-compose. this is a “Docker way”…

Hey, docker is designed to use fixed ip addresses. It can used via ports or layer 2 bridge and has even embedded layer 3 options . All explained here: https://www.youtube.com/watch?v=bKFMS5C4CG0

But i go it working . Heres the Ansible Playbook deploying Nextcloud in one click with a static ip address:

---
- hosts: docker
  vars:
    db_volume: mariadb
    nextcloud: nextcloud
  tasks:
    - name: Deploy MariaDB server
      docker_container:
        image: mariadb
        name: mariadb
        networks:
        - name: virtualswitch
        volumes:
          - "{{ db_volume }}:/var/lib/mysql"
        env:
          MYSQL_ROOT_PASSWORD: somerootpassword
          MYSQL_PASSWORD: somemysqlpassword
          MYSQL_DATABASE: db
          MYSQL_USER: mysqluser

    - name: Deploy Nextcloud
      docker_container:
        image: nextcloud
        name: nextcloud
        restart_policy: always
        networks:
        - name: virtualswitch
          ipv4_address: "10.0.100.102"
        links:
          - "mariadb:{{ db_volume }}"
        volumes:
          - "{{ nextcloud }}:/var/www/html"
        env:
          MYSQL_PASSWORD: somemysqlpassword
          MYSQL_DATABASE: db
          MYSQL_USER: mysqluser
          MYSQL_HOST: mariadb`