Docker create MySQL Container

Source: Internet
Author: User
Tags docker ps docker run

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.