Home > Database > Mysql Tutorial > How to Initialize a MySQL Database with a Predefined Schema in a Docker Container?

How to Initialize a MySQL Database with a Predefined Schema in a Docker Container?

Mary-Kate Olsen
Release: 2024-12-09 06:39:07
Original
294 people have browsed it

How to Initialize a MySQL Database with a Predefined Schema in a Docker Container?

Initializing a MySQL Database with Schema in Docker

Issue

Docker users encounter difficulties initializing a MySQL database with a predefined schema using a Dockerfile. Despite following documentation guidelines, the CMD command intended to run the schema script fails, preventing a successful container creation.

Solution

The solution involves a sequential approach:

  1. Dump MySQL Schema to File:
    Export the database schema to a file using the mysqldump command.
  2. ADD Schema File to Docker Container:
    In the Dockerfile, use the ADD command to copy the schema.sql file to the /docker-entrypoint-initdb.d directory. This directory allows for automatic execution of scripts on database initialization.
  3. Start Docker MySQL Instance:
    Initiate the Docker container using the specified MySQL image and the updated Dockerfile.

Example Dockerfile:

FROM mysql:5.7.15

MAINTAINER me

ENV MYSQL_DATABASE=<schema_name> \
    MYSQL_ROOT_PASSWORD=<password>

ADD schema.sql /docker-entrypoint-initdb.d

EXPOSE 3306
Copy after login

Additional Notes:

  • The docker-entrypoint.sh script will automatically execute all files with a ".sql" extension in the /docker-entrypoint-initdb.d directory.
  • This method allows for initializing a MySQL database with a schema upon container creation, eliminating the need for manual intervention.

The above is the detailed content of How to Initialize a MySQL Database with a Predefined Schema in a Docker Container?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template