SoFunction
Updated on 2025-03-08

Docker Dockerfile builds MySQL and initializes data

Dockerfile build MySQL

Writing Dockerfile

# Use MySQL 5.7 image as the basisFROM mysql:5.7

# Copy the initialization SQL script into the containerCOPY *.sql //

# Set environment variables (root password)ENV MYSQL_ROOT_PASSWORD=123456

ENV LANG=-8

EXPOSE 3306

In the above Dockerfile

We used the MySQL5.7 official image as the base image and copied the initialization SQL script to the // directory of the container.

This way, the MySQL container will automatically execute the SQL script when it is started.

Build and run Docker containers

Run the following command to build the Docker image:

docker build -t mysql-container .

After the build is complete, run the following command to start the MySQL container:

docker run -d --name mysql-instance -v /Users/acton_zhang/docker/mysql/data:/var/lib/mysql -p 3306:3306  mysql-container

This will start a MySQL container called mysql-instance in the background

If all goes well, you will see that the MySQL container was successfully started and the initialization SQL script was executed

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.