How to link local docker app with azure database for mysql ?

BassamBALLAJI 376 Reputation points
2021-06-13T13:52:36.143+00:00

Hello,

I'm running docker app matomo with docker compose. As of today, the app is using local mysql server to host its databases.

Now, I want to link my local app with Azure database for mysql, I want to know how to substitute necessary variables to keep the app working with the new architecture.

The database variables are defined in 2 files :

 - **FILE 1 : docker-compose.yml :**

    version: "3"
    services:
      db:
        image: mariadb
        command: --max-allowed-packet=64MB
        restart: always
        volumes:
          - ./db:/var/lib/mysql
        environment:
          - MYSQL_ROOT_PASSWORD=XXXX
        env_file:
          - ./db.env
        ports:
          - 3306:3306
      app:
        image: matomo
        restart: always
        volumes:
    #     - ./config:/var/www/html/config
    #     - ./logs:/var/www/html/logs
          - ./matomo:/var/www/html
        environment:
          - MATOMO_DATABASE_HOST=db
        env_file:
          - ./db.env
        ports:
          - 8080:80
    volumes:
      db:
      matomo:

 - **FILE 2 : db.env :**

    MYSQL_PASSWORD=XXXX
    MYSQL_DATABASE=matomo
    MYSQL_USER=matomo
    MATOMO_DATABASE_ADAPTER=mysql

My guess maybe change db volume and MATOMO_DATABASE_HOST=db to declare remote azure instance ?

Rgds,

Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
986 questions
{count} votes

Accepted answer
  1. Navtej Singh Saini 4,226 Reputation points Microsoft Employee Moderator
    2021-06-17T19:45:14.113+00:00

    @BassamBALLAJI

    We have got response from our team and they have conveyed more info regarding this. If that doesn't resolve the issue, please raise a support issue.

    1) From the docker compose file we gather below:
    a. Matomo is using a docker container to run MySQL database, instead of connecting to a MySQL DB installation on prem or to a MySQL DB on cloud.
    b. Hence the docker compose file (File 1) has a “db” container whipped up too (from a mariadb docker image) – as below. Here, the “volume” is just where the containerized MySQL DB’s data persists.

    5.         db:  
    6.           image: mariadb  
    7.           command: --max-allowed-packet=64MB  
    8.           restart: always  
    9.           volumes:  
    10.            - ./db:/var/lib/mysql  
    11.          environment:  
    12.            - MYSQL_ROOT_PASSWORD=XXXX  
    13.          env_file:  
    14.            - ./db.env  
    15.          ports:  
    16.            - 3306:3306  
    

    c. The app then connects to this db : as specified in “environment” – as below.

    24.          environment:  
    25.            - MATOMO_DATABASE_HOST=db  
    26.          env_file:  
    27.            - ./db.env  
    28.          ports:  
    29.            - 8080:80  
    

    2) Now, if Cx wants to connect to Azure DB for MySQL instead of this local MySQL DB Container instance,
    a. We do not need a MySQL DB container at all, hence won’t need to whip the container from a docker image, and in turn won’t need a docker compose at all (docker compose is for multi-container scenario.)
    b. Instead, what you can do is pass “MATOMO_DATABASE_HOST=mysqldemosvr.mysql.database.azure.com” env variable while running the docker using docker run itself.
    Of course, instead of directly specifying the credentials this way, you can store all credentials like MATOMO_DATABASE_HOST, MATOMO_DATABASE_PORT, MATOMO_DATABASE_USERNAME, MATOMO_DATABASE_PASSWORD, in an .env file and “docker run” by passing this file.
    c. Cx can refer to : How to run Matomo in a Docker container while passing database credentials as environment variables - DEV Community. And plug in their Azure DB for MySQL credentials.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.