Home > Software design >  Create database on docker startup
Create database on docker startup

Time:03-10

I want to create mysql database on docker-compose startup from database.sql script. My database.sql script is on location: src/main/java/com/project_name/resources/db/database.sql. How should I wrote that in my docker-compose.yml file? Right now neither works.

    volumes:
    - ./database.sql:/data/application/database.sql

or something like:

        volumes:
    - ./database.sql:/src/main/java/com/project_name/resources/db/database.sql

CodePudding user response:

Try like this:

    volumes:
      - ./src/main/java/com/project_name/resources/db/:/docker-entrypoint-initdb.d/database.sql

Or just use a database migration tool like Flyway or Liquibase.

CodePudding user response:

You can mount the schema and data in Volumes as I demonstrated below, make sure the backup file have proper access permissions and verify the path in your machine.

version: '3.8'
services:
  db:
    image: mysql:8.0
    restart: always
    environment:
      - MYSQL_DATABASE=DB_NAME
      - MYSQL_USER: DB_USER
      - MYSQL_ROOT_PASSWORD=DB_PASSEOD
    ports:
      - '3306:3306'
    volumes:
      - db:/var/lib/mysql
      - ./db/init.sql:/src/main/java/com/project_name/resources/db/database.sql
volumes:
  db:
    driver: local
  • Related