Docker create MySQL Container

Source: Internet
Author: User
Tags docker ps docker run

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

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.