Reference resources: https://www.jb51.net/article/115422.htm
1) operate in the CentOS7 environment, create the folder mymy, and create a new file Dockerfile, setup.sh, schema.sql, privileges.sql
mkdir mymy cd mymy touch Dockerfile touch setup.sh touch schema.sql touch privileges.sql
Dockerfile:
FROM mysql:5.7 ENV MYSQL_ALLOW_EMPTY_PASSWORD yes COPY setup.sh /mysql/setup.sh COPY schema.sql /mysql/schema.sql COPY privileges.sql /mysql/privileges.sql CMD ["sh", "/mysql/setup.sh"]
setup.sh:
#!/bin/bash echo 'checking mysql status.' service mysql status echo '1.start mysql....' service mysql start sleep 3 service mysql status echo '2.start importing data....' mysql < /mysql/schema.sql echo '3.end importing data....' sleep 3 service mysql status echo '4.start changing password....' mysql < /mysql/privileges.sql echo '5.end changing password....' sleep 3 service mysql status echo 'mysql is ready' tail -f /dev/null
schema.sql: (note the use of quotation marks, the use of quotation marks for database name, table name and field name, and the use of string value ')
-- Create database create database `docker_mysql`; use docker_mysql; -- Building tables DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` bigint(20) NOT NULL, `created_at` bigint(40) DEFAULT NULL, `last_modified` bigint(40) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, `first_name` varchar(255) DEFAULT NULL, `last_name` varchar(255) DEFAULT NULL, `username` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- insert data INSERT INTO `user` (`id`, `created_at`, `last_modified`, `email`, `first_name`, `last_name`, `username`) VALUES (0,1490257904,1490257904,'john.doe@example.com','John','Doe','user');
privileges.sql:
use mysql; select host, user from user; create user docker identified by '123456'; grant all on docker_mysql.* to docker@'%' identified by '123456' with grant option; flush privileges;
Build image:
docker build -t zkong/mymy .
To run a mirror:
docker run -d -p 13306:3306 zkong/mymy
After the operation is successful, you can enter the container to view the table creation:
In container, file status:
vi and other tools cannot be used in the container. You can install it if necessary:
apt-get update apt-get install vim
After installation, you can directly use vi to modify setup.sh and other files to debug.