Use a database container in lan instead of local

Hi,

I have two rpi 4b with the same docker(compose) setups. With profiles i enable disable services on the two rpi’s (with bullseye) and they operate on the same lan.

What i want (and need some help with) is to have my mysql service on RPI/CONFIG B and not on RPI A and B!

I included parts of my current two docker-compose files, an ask for some advise/help how i can config my RPI A to use the the mysql service on CONFIG B (RPI B).

Both RPIs and local docker env’s are working great, but i want to take the next step to share services among my lan. Step to go to SWARMS is to big for me i think, because all my compose files and dockerfiles are generated and not swarm compatible?

CONFIG A (like to use MYSQL from CONFIG B)

version: '3.6'

services:
  mysql:
    container_name: mysql
    profiles: ["mysql"]
    image: "${MYHUB_mysql}"
    restart: always
    volumes:
      - "${MYPATH_volumes}/mysql/data:/var/lib/mysql"
      - "/var/run/docker.sock:/var/run/docker.sock"
    ports:
      - '6603:3306'
    environment:
      MYSQL_ROOT_PASSWORD: "xxx"
  phpmyadmin:
    container_name: phpmyadmin
    profiles: ["phpmyadmin"]
    image: "${MYHUB_phpmyadmin}"
    restart: always
    depends_on:
      - mysql
    ports:
      - '8084:80'
    environment:
      PMA_HOST: mysql
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
  joomla:
    image: "${MYHUB_joomla}"
    profiles: ["joomla"]
    container_name: joomla
    restart: unless-stopped
    links:
      - mysql:mysql
    ports:
      - 89:80
    volumes:
      - "${MYPATH_volumes}/joomla/data:/var/www/html"
      - "/var/run/docker.sock:/var/run/docker.sock"
    environment:
      JOOMLA_DB_HOST: mysql
      JOOMLA_DB_USER: root
      JOOMLA_DB_PASSWORD: xxx
      JOOMLA_DB_NAME: joomla_db

CONFIG B

version: '3.6'

services:

  mysql:
    container_name: mysql
    profiles: ["mysql"]
    image: "${MYHUB_mysql}"
    restart: always
    volumes:
    - "${MYPATH_volumes}/mysql/data:/var/lib/mysql"
    - "/var/run/docker.sock:/var/run/docker.sock"
    ports:
    - '6603:3306'
    environment:
      MYSQL_ROOT_PASSWORD: "xxx"

  phpmyadmin:
    container_name: phpmyadmin
    profiles: ["phpmyadmin"]
    image: "${MYHUB_phpmyadmin}"
    restart: always
    depends_on:
      - mysql
    ports:
      - '8084:80'
    environment:
      PMA_HOST: mysql
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"

  nodered:
    container_name: nodered
    profiles: ["nodered"]
    build: 
      context: ${MYPATH_services}/nodered/.
      args:
        MYHUB: "${MYHUB_nodered}"
        MYPATH_services: "${MYPATH_services}"
        MYPATH_volumes: "${MYPATH_volumes}"
    restart: unless-stopped
    image: ${MYHUB_nodered}
    depends_on:
      - mysql
      - mosquitto
      - apache
    user: "0"
    privileged: true
    ports:
      - "1880:1880"
      - "443:443"
    volumes:
      - "${MYPATH_volumes}/nodered/data:/data"
      - "${MYPATH_volumes}/usbdata:/mnt/usbdata"
      - "${MYPATH_volumes}/nasdata:/mnt/mntnas"
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "/var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket"
    devices:
      - "/dev/gpiomem:/dev/gpiomem"
      - "/dev/vcio:/dev/vcio"
    environment:
      - TZ=Etc/UTC
      - IOTSTATUS=Domoticz KAST
    network_mode: host
    logging:
      options:
        max-size: 5m
        max-file: "3"

First things first:

  • running a swarm cluster with two nodes is not recommended. If you run both nodes as master node, the outage of either one of both will render the cluster headless. If you just make one the master, your cluster will be headless if the master is unhealthy. Headless means containers on healty nodes will keep on working, but changes won’t be possible: no self-healing if a container dies, and no way to create, update, remove anything.

  • swarm mode does not support build (needs to be done externaly), profile (afaik: not supported) , links (legacy, use dns-based service discovery instead), privileged and requires the restart configuration item to be replaced by deploy.restart_policy.

So yes, the way you configured your services can’t be directly translated into a swarm stack configuration. You would need to build your images outside the stack with docker build and you would need to figure out what capabilites the “privileged” container realy needs and add them - from what I remember cap_add works, even though the docs claim it doesn’t (I could be mistaken though).

Since both docker nodes run standalone, accessing containers running on the other node doesn’t differ from accessing non dockerized services. Just call them by the node name or ip and published host-port. In your case, on the joomla service on node a, you would configure JOOMLA_DB_HOST: {hostname or ip of node B}:6603.

Thanks, so resume

  • swarm isnt the way to go in my situation
  • i can access mysql on another host by using ip-adress instead of hostname in joomla_db_host