Load initial schema and data to MySQL docker container on start up

To load initial schema and data to a MySQL Docker container on start-up, you can use the following steps:

  1. Create a directory to store your SQL scripts, for example /path/to/sql.
  2. Write your SQL scripts to create the schema and load the data into separate files and place them in the /path/to/sql directory. For example, you might have 1-schema.sql and 2-data.sql files.
  3. The scripts will be executed in alphabetical order, so you may want to prefix the files with numbers or letters to control the order in which they are executed (as shown in step 2)
  4. Create a docker-compose.yml file and define a service for your MySQL database. The service definition should include a volume mapping that maps the /path/to/sql directory on the host to the /docker-entrypoint-initdb.d directory in the container. For example:
version: '3'
    image: mysql
      MYSQL_ROOT_PASSWORD: test12345
      - /path/to/sql:/docker-entrypoint-initdb.d

5. Start the services defined in the docker-compose.yml file using the following command:

$ docker-compose up -d

5. The MySQL Docker image includes a script, /docker-entrypoint.sh, that is executed on container start-up. This script will run any SQL files in the /docker-entrypoint-initdb.d directory

These steps will allow you to initialize a MySQL database with a schema and data of your choice every time the container starts using Docker Compose.

Pavan Kumar Jadda
Pavan Kumar Jadda
Articles: 36

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.