Background:
Today, no matter what services and applications are basically able to run in Docker, but in my personal impression, like the database of the more important and data vulnerable to the application is not suitable for running in Docker. But there are also a lot of people who try to run MySQL and other databases in Docker, so try it out. (Well, the point is the leader likes ~ ~)
To get the image:
The image of MySQL can be made by itself with Dockerfile, or directly to the official Docker Image Library, this article uses the official image.
# docker pull mysql# docker imagesrepository Tag image ID CREATED SIZE docker.io/mysql latest d9124e6c552f 12 d AYS AGO         383.4 MB
To run the container:
1: normal operation.
Start container: # docker run --name cmh-mysql -e mysql_root_password=my-secret-pw -d docker.io/mysql into the container: # docker-enter cmh-mysql into the mysql:[email protected]:~# mysql -u root -pmy-secret-pwmysql: [warning] using a password on the Command line interface can be insecure. Welcome to the mysql monitor. commands end with ; or \ g.your mysql connection id is 2server version: 5.7.16 mysql community server (GPL) copyright (c) 2000, 2016, oracle and/or its affiliates. all rights reserved. oracle is a registered trademark of oracle corporation and/or Itsaffiliates. other names may be trademarks of their respectiveowners . type ' help; ' or ' \h ' for help. Type ' \c ' to clear the current input statement.mysql>
The above creates a MySQL Docker container that can be seen in version 5.7.16. However, there are two problems in creating the container, one is that after the container is deleted, the data is lost, and the second is to access the database, you must enter into the container.
2: Persistent data, mapping open MySQL port
Create host Data directory: # mkdir -p /opt/data/mysql boot container: # docker run --name cmh-mysql -v /opt/data/mysql/:/var/lib/mysql -p 3306:3306 -e mysql_root_password=123456 -d docker.io/mysqlc38f50a540ff4d5ecf1a5ec49fb721335a8e1b79dec58229cf5e00553f988e44 Viewing the container:# docker ps container id image command created status PORTS NAMESc38f50a540ff docker.io/mysql "docker-entrypoint.sh" 9 seconds ago up 8 seconds 0.0.0.0:3306->3306/tcp cmh-mysql View Port:# netstat -ntplactive internet connections (only servers) proto recv-q send-q local address Foreign Address state pid/program name tcp6 0 0 :::3306 :::* LISTEN 28657/docker-proxy view MySQL data on host: # cd /opt/data/mysql# lltotal 188452- Rw-r-----. 1 systemd-bus-proxy ssh_keys 56 dec 6 16:01 auto.cnf-rw-r-----. 1 systemd-bus-proxy ssh_keys 1325 dec 6 16:01 ib_buffer_pool-rw-r-----. 1 Systemd-bus-proxy ssh_keys 79691776 dec 6 17:16 ibdata1-rw-r-----. 1 systemd-bus-proxy ssh_keys 50331648 dec 6 17:16 ib_ Logfile0-rw-r-----. 1 systemd-bus-proxy ssh_keys 50331648 dec 6 16:01 ib_logfile1-rw-r-----. 1 systemd-bus-Proxy ssh_keys 12582912 dec 6 17:16 ibtmp1drwxr-x---. 2 systemd-bus-proxy ssh_keys 4096 dec 6 16:01 Mysqldrwxr-x---. 2 systemd-bus-proxy ssh_keys 8192 Dec 6 16:01 performance_schemadrwxr-x---. 2 systemd-bus-proxy ssh_keys 8192 dec 6 16:01 sys
the-p 3,306:3,306 maps the container's MySQL port 3306 to the host's 3306 port so that you can access MySQL directly to the 3306 port of the host.
-v/opt/data/mysql:/var/lib/mysql, which maps the host/opt/data/mysql/directory to the/var/lib/mysql directory of the container.
Precautions:
1: When mapping a directory using the-v option, the host needs to turn off SELinux:
# Setenforce 0
Or add the relevant SELinux permissions to the Data directory:
# Chcon-rt Svirt_sandbox_file_t/my/own/datadir
The 2:-V option originally mapped the host directory into a container, but in this article, it was reversed. That is, the directory in the container is mapped out to the host, because the official image is used in the production of the VOLUME /var/lib/mysql option. This makes/var/lib/mysql a separate volume group in the container, which can be mapped out to the host when using the Mount option-V.
Refer to the official MySQL image dockerfile:
Https://github.com/docker-library/mysql/blob/4dd33136c4739667a223d39b6f829beb27b235cf/5.7/Dockerfile
This article from the "Play God Clown" blog, reproduced please contact the author!
Running MySQL service on Docker