Original link: Docker create MySQL Container
The purpose of this article is to create an image of MySQL and automatically start the MySQL service in the newly created container to accept external connections
Steps:
1. First create a directory and create a dockerfile in the directory, the file content is as follows
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. Run the build command in the directory where the Dockerfile is located to generate the image file, where Mysql_server is used as the image filename
sudo docker build-t mysql_server.
After you run the build command, 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 you start the container, you can use sudo docker PS to see the Ports column as "0.0.0.0:49153->3306/tcp", and the container's port 3306 will be mapped to port 49153 of the host machine. This allows us to connect via the 49153 port of the host machine, for example:
Mysql-h < host machine >-u root-pletmein-p 49153
3.2 You can also use the following command when you run the container
sudo docker run--name=mysqlserver-d-P 3306:3306 mysql_server
At this point, port 3306 of the container is mapped to the 3306 port of the host machine, so that we can access MySQL through the 3306 port of the host machine.
Mysql-h < host machine >-u root-pletmein
3.3 Another situation is that for security reasons, I only want the current host machine to have access to the MySQL service, at which point we can
sudo docker run--name=mysqlserver-d-P 127.0.0.1:3306:3306 mysql_server