Friday, 26 September 2025

Docker Compose MYSQL init, MYSQL port 3306

 docker mysql service has to use port 3306 to start multiple :

version: '3.8'services: mysql_db1: image: mysql:8 environment: MYSQL_ROOT_PASSWORD: password1 MYSQL_DATABASE: db1 ports: - "3306:3306" # Maps host port 3306 to container port 3306 volumes: - db_data1:/var/lib/mysql mysql_db2: image: mysql:8 environment: MYSQL_ROOT_PASSWORD: password2 MYSQL_DATABASE: db2 ports: - "3307:3306" # Maps host port 3307 to container port 3306 volumes: - db_data2:/var/lib/mysqlvolumes: db_data1: db_data2:


-----------------------------------------------------------------------------------------

To add an init.sql script to your MySQL Docker Compose setup to create the gitea and keycloak databases, follow these steps: Create the init.sql file.
Create a file named init.sql (or any other name ending in .sql.sh, or .sql.gz) in a directory accessible to your docker-compose.yml file. For example, create a subdirectory named mysql-init and place init.sql inside it.
Code
    -- mysql-init/init.sql    CREATE DATABASE IF NOT EXISTS `gitea`;    CREATE DATABASE IF NOT EXISTS `keycloak`;    -- You might also want to create dedicated users and grant privileges here    -- For example:    -- CREATE USER 'gitea_user'@'%' IDENTIFIED BY 'gitea_password';    -- GRANT ALL PRIVILEGES ON `gitea`.* TO 'gitea_user'@'%';    -- CREATE USER 'keycloak_user'@'%' IDENTIFIED BY 'keycloak_password';    -- GRANT ALL PRIVILEGES ON `keycloak`.* TO 'keycloak_user'@'%';    -- FLUSH PRIVILEGES;
Modify your docker-compose.yml.
In your docker-compose.yml file, add a volumes entry to your MySQL service to mount your mysql-init directory into the /docker-entrypoint-initdb.d directory within the MySQL container.
Code
    version: '3.8'    services:      mysql:        image: mysql:8.0 # Or your desired MySQL version        environment:          MYSQL_ROOT_PASSWORD: your_root_password          # Optional: Define default database, user, and password for initial setup          # MYSQL_DATABASE: default_database          # MYSQL_USER: default_user          # MYSQL_PASSWORD: default_password        volumes:          - ./mysql-init:/docker-entrypoint-initdb.d          - mysql_data:/var/lib/mysql # For persistent data        ports:          - "3306:3306"        networks:          - my_network      gitea:        image: gitea/gitea:latest        environment:          DB_TYPE: mysql          DB_HOST: mysql:3306          DB_NAME: gitea          DB_USER: gitea_user # If you created a dedicated user          DB_PASSWD: gitea_password # If you created a dedicated user          # ... other Gitea configurations        depends_on:          - mysql        networks:          - my_network      keycloak:        image: quay.io/keycloak/keycloak:latest        environment:          KC_DB: mysql          KC_DB_URL: jdbc:mysql://mysql:3306/keycloak          KC_DB_USERNAME: keycloak_user # If you created a dedicated user          KC_DB_PASSWORD: keycloak_password # If you created a dedicated user          # ... other Keycloak configurations        depends_on:          - mysql        networks:          - my_network    volumes:      mysql_data:    networks:      my_network:
Run Docker Compose.
Navigate to the directory containing your docker-compose.yml file and run:
Code
    docker-compose up -d
The init.sql script will execute automatically when the MySQL container starts for the first time (when the /var/lib/mysql data directory is empty), creating the gitea and keycloak databases. Subsequent restarts will not re-run the init.sql script unless the mysql_data volume is cleared.




No comments:

Post a Comment