Original link: Docker create MySQL Container
The purpose of this is to create a MySQL image that comes out of the container itself and actively initiates the MySQL service to accept external connections
Steps:
1. First create a folder and create a dockerfile under the folder, the file contents such as the following
From Centos:centos6maintainer fanbin Kong "[email protected]" RUN yum install-y mysql-server MySQL run/etc/init.d/mysqld Start && mysql-e "Grant all privileges on * * to ' root ' @ '% ' identified by ' letmein ';" && MYSQL-E "Grant all privileges on * * to ' root ' @ ' localhost ' identified by ' letmein ';" && mysql-u root-pletmein-e "show databases;" EXPOSE 3306 CMD ["/usr/bin/mysqld_safe"]
2. Execute the build command under the Dockerfile folder to generate the image file, where Mysql_server is used as the image file name
sudo docker build-t mysql_server.
After the build command is executed, you can use sudo docker images to view it.
3. Start the container
3.1 First Use the following command to start the container
sudo docker run--name=mysqlserver-d-P mysql_server
After the container is started. Can be viewed using "sudo docker PS". At this point the Ports column is "0.0.0.0:49153->3306/tcp", and the 3306port of the container is mapped to the 49153port of the host machine, so that we can connect to the 49153port of the host machine, for example:
Mysql-h < host machine >-u root-pletmein-p 49153
3.2 In addition to the execution of the container, you can also use the following command
sudo docker run--name=mysqlserver-d-P 3306:3306 mysql_server
At this point the 3306port of the container is mapped to the 3306port of the host machine, so that we can access MySQL from the host machine's 3306port
Mysql-h < host machine >-u root-pletmein
3.3 Another situation is for security reasons. I just want the current host machine to be able to access the MySQL service, at which point we can
sudo docker run--name=mysqlserver-d-P 127.0.0.1:3306:3306 mysql_server
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Docker create MySQL Container