How can I run mysql router in Docker with compose file with bootstrap mode

1k Views Asked by At

MySql Router has a command to bootstrap itself

mysqlrouter --bootstrap [email protected]:3306  --directory /tmp/router

After bootstrap, Router will exit, and we should run it again with config file generated by bootstrap since I will modify this file

mysqlrouter --config /tmp/router/mysqlrouter.conf

It's work ok in Linux pure environment, but not in docker, below is my docker compose file

version: '2'
services:

common: &baseDefine
    environment:
        MYSQL_HOST: "192.168.213.6"
        MYSQL_PORT: 3306
        MYSQL_USER: 'root'
        MYSQL_PASSWORD: 'urpwd.root'
        MYSQL_INNODB_CLUSTER_MEMBERS: 3
        MYSQL_CREATE_ROUTER_USER: 0
    image: "docker.io/mysql/mysql-router:latest"

    volumes:
        - "./conf:/tmp/myrouter"

    network_mode: "host"

boot:
    container_name: "mysql_router_boot"

    command: ["mysqlrouter","--bootstrap","[email protected]:3306","--directory","/tmp/myrouter","--conf-use-sockets","--conf-skip-tcp",
                    "--force","--strict","--user","mysqlrouter","--account","sqlrouter","--account-create","if-not-exists"]

    <<: *baseDefine


run:
    container_name: "mysql_router"
    restart: always

    command: ["mysqlrouter","--config","/tmp/myrouter/mysqlrouter.conf"]

    <<: *baseDefine

First, I will call the boot service for bootstrap, and generate configure to specified dir

docker-compose run --rm boot

after this command, the configure file is generated ok, then I execute

docker-compose run --name mysql_router run

It will work, but not work like what I supposed, Without docker, the second step to run mysqlrouter only use config and without bootstrap But with docker and those commands, the second step will bootstrap again. I know this is since 2 service in 2 containers. Is there any ideas to make this flow more suitable? Such as run 2 service in one container? Or run a service in an existed container?

1

There are 1 best solutions below

0
On

It's ok with below:

modify yml's command of run service

run:
    command: /bin/bash -c "mysqlrouter --config /tmp/myrouter/mysqlrouter.conf"

use bash to run mysqlrouter delay for recognizing the existed conf file; it works