Docker data management and mirroring

Docker’s data management and mirroring

    • 1. Docker data management
      • 1. Data volume
      • 2. Data volume container
      • 3. Port mapping
      • 4. Container interconnection (using centos image)
    • 2. Creation of Docker image
      • 1. Create based on existing image
      • 2. Create based on local template
      • 3. Create based on Dockerfile
    • 3. Commonly used instructions for Dockerfile operations:
      • 1. FROM image
      • 2. MAINTAINER name
      • 3. RUN command
      • 4.ENTRYPOINT [“Program to run”, “Parameter 1”, “Parameter 2”]
      • 5.CMD [“Program to run”, “Parameter 1”, “Parameter 2”]
      • 6. EXPOSE port number
      • 7.ENV environment variable variable value
      • 8.ADD source file/directory target file/directory
      • 9.COPY source file/directory target file/directory
      • 10. VOLUME [“directory”]
      • 11. USER username/UID
      • 12. WORKDIR path /home
      • 13. ONBUILD command
      • 14. HEALTHCHECK health check
    • 4. Dockerfile case
      • 1. Create a working directory
      • 2. Base image based on
      • 3. Test
    • 5. Expansion
      • 1. Build SSH image
      • 2. Systemctl image
      • 3. nginx mirror
      • 4. tomcat image
      • 5.mysql mirror

1. Docker data management

There are two main ways to manage data in Docker containers: Data Volumes and DataVolumes Containers.

1. Data volume

A data volume is a special directory used by containers, located within the container. The host’s directory can be mounted to the data volume. Modifications to the data volume are immediately visible, and updated data will not affect the mirror, thus enabling data migration between the host and the container. The use of data volumes is similar to the mount operation on directories under Linux.

docker pull centos:7

#The host directory /var/www is mounted to /data1 in the container.
Note: The path of the host's local directory must be an absolute path. If the path does not exist, Docker will automatically create the corresponding path.
docker run -v /var/www:/data1 --name web1 -it centos:7 /bin/bash #-v option can create a data volume in the container
ls
echo "this is web1" > /data1/abc.txt
exit

#Return to the host for viewing
cat /var/www/abc.txt

2. Data volume container

If you need to share some data between containers, the easiest way is to use a data volume container. The data volume container is an ordinary container that provides data volumes for other containers to mount and use.
#Create a container as a data volume container

docker run --name web2 -v /data1 -v /data2 -it centos:7 /bin/bash
echo "this is web2" > /data1/abc.txt
echo "THIS IS WEB2" > /data2/ABC.txt

#Use --volumes-from to mount the data volumes in the web2 container to the new container
docker run -it --volumes-from web2 --name web3 centos:7 /bin/bash
cat /data1/abc.txt
cat /data2/ABC.txt

3.Port mapping

When starting a container, if you do not specify the corresponding port, the services in the container cannot be accessed through the network outside the container. The port mapping mechanism provides services in the container for external network access. In essence, it maps the host’s port to the container, so that the external network can access the services in the container by accessing the host’s port.

docker run -d --name test1 -P nginx #Randomly map ports (starting from 32768)

docker run -d --name test2 -p 43000:80 nginx #Specify the mapped port

docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d3c04f57a68 nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:43000->80/tcp test2
b04895f870e5 nginx "/docker-entrypoint.…" 17 seconds ago Up 15 seconds 0.0.0.0:49170->80/tcp test1

Browser access: http://192.168.174.15:43000, http://192.168.174.15:49170

4. Container interconnection (using centos image)

Container interconnection is to establish a dedicated network communication tunnel between containers through the name of the container. To put it simply, a tunnel is established between the source container and the receiving container, and the receiving container can see the information specified by the source container.

#Create and run the source container named web1
docker run -itd -P --name web1 centos:7 /bin/bash
\t
#Create and run the receiving container named web2, use the --link option to specify the connection container to achieve container interconnection
docker run -itd -P --name web2 --link web1:web1 centos:7 /bin/bash #--link container name: alias of the connection

#Enter the web2 container, ping web1
docker exec -it web2 bash
ping web1

2. Creation of Docker image

There are three ways to create an image, namely creating based on an existing image, creating based on a local template, and creating based on a Dockerfile.

1. Create based on an existing image

(1) First start an image and make modifications in the container

docker create -it centos:7 /bin/bash

docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
000550eb36da centos:7 "/bin/bash" 3 seconds ago Created gracious_bassi

(2) Then submit the modified container as a new image, and you need to use the ID number of the container to create a new image

docker commit -m "new" -a "centos" 000550eb36da centos:test
#Common options:
-m description information;
-a author information;
-p stops the container from running during the build process.

docker images

2. Create based on local template

The image can be generated by importing the operating system template file. The template can be downloaded from the OPENVZ open source project. The download address is http://openvz.org/Download/template/precreated

wget http://download.openvz.org/template/precreated/debian-7.0-x86-minimal.tar.gz

#Import as image
cat debian-7.0-x86-minimal.tar.gz | docker import - debian:test

3. Create based on Dockerfile

(1) Union File System (UnionFS)

  • UnionFS (Union File System): Union File System (UnionFS) is a hierarchical, lightweight and high-performance file system. It supports modifications to the file system to be superimposed layer by layer as one submission, and can also combine different Directories are mounted to the same virtual file system. AUFS, OverlayFS, and Devicemapper are all types of UnionFS.
  • The Union file system is the basis of Docker images. Images can be inherited through layering. Based on the base image (without a parent image), various specific application images can be produced.
  • Features: Load multiple file systems at the same time, but from the outside, only one file system can be seen. Joint loading will superimpose each layer of file systems, so that the final file system will contain all underlying files and directories.

(2) Image loading principle

  • Docker’s image is actually composed of layer-by-layer file systems, and this layer of file systems is UnionFS.
  • Bootfs mainly includes bootloader and kernel. The bootloader is mainly used to guide and load the kernel. When Linux starts, it will load the bootfs file system.
  • At the bottom of the Docker image is bootfs, which is the same as our typical Linux/Unix system, including the boot loader and kernel. When the boot loading is completed, the entire kernel is in the memory. At this time, the right to use the memory has been transferred from bootfs to the kernel. At this time, the system will also uninstall bootfs.
  • rootfs, on top of bootfs. It contains standard directories and files such as /dev, /proc, /bin, /etc in a typical Linux system. Rootfs refers to various operating system distributions, such as Ubuntu, Centos, etc.

(3) Why is the size of centos in Docker only 200M?

Because for a streamlined OS, the rootfs can be very small and only needs to contain the most basic commands, tools and libraries. Because the bottom layer directly uses the host’s kernel, you only need

Just provide rootfs. It can be seen that for different Linux distributions, bootfs is basically the same, but rootfs will be different, so different distributions can share bootfs.

(4)Dockerfile

  • The Docker image is a special file system. In addition to providing the programs, libraries, resources, configuration and other files required by the container runtime, it also contains some configuration parameters prepared for runtime (such as anonymous volumes, environment variables, users, etc. ). The image does not contain any dynamic data, and its content will not be changed after it is built.

  • Mirror customization is actually customizing the configuration and files added to each layer. If we can write the commands of modification, installation, construction, and operation of each layer into a script, and use this script to build and customize the image, then the problems of transparency and volume of image construction will be solved. This script is the Dockerfile.

  • Dockerfile is a text file, which contains a series of instructions (Instruction), each instruction builds a layer, so the content of each instruction is to describe how the layer should be built. With Dockerfile, when we need to customize our own additional requirements, we only need to add or modify instructions on the Dockerfile and regenerate the image, saving the trouble of typing commands.

  • In addition to manually generating Docker images, you can use Dockerfile to automatically generate images. A Dockerfile is a file composed of multiple instructions, each of which corresponds to a command in Linux, and the Docker program will read the instructions in the Dockerfile to generate a specified image.

  • The Dockerfile structure is roughly divided into four parts: basic image information, maintainer information, image operation instructions, and execution instructions when the container starts. Dockerfile supports one instruction per line, each instruction can carry multiple parameters, and supports comments starting with “#”.

(5) Layering of Docker image structure
An image is not a single file, but consists of multiple layers. The container actually adds a read-write layer on top of the image. Any file changes made in the running container will be written to this read-write layer. If the container is deleted, its top read-write layer is also deleted, and file changes are lost. Docker uses storage drivers to manage the content of each layer of the image and the container layer of the read-write layer.

(1) Each instruction in the Dockerfile will create a new image layer;
(2) The image layer will be cached and reused;
(3) When the instructions of the Dockerfile are modified, the copied file changes, or the variables specified when building the image are different, the corresponding image layer cache will become invalid;
(4) If the mirror cache of a certain layer is invalid, the cache of the mirror layer after it will be invalid;
(5) The image layer is immutable. If you add a file in a certain layer and then delete it in the next layer, the file will still be included in the image, but the file will not be visible in the Docker container.

3. Commonly used instructions for Dockerfile operations:

1.FROM image

Specifies the base image on which the new image is based. The first instruction must be a FROM instruction, and a FROM instruction is required for each image created

2. MAINTAINER name

Describe the maintainer information of the new image

3.RUN command

Execute commands on the base image and commit to the new image

4.ENTRYPOINT [“program to run”, “parameter 1”, “parameter 2”]

Set the command and its parameters to be run first when the container starts.
The content of the ENTRYPOINT directive in the image can be overridden by using the command docker run –entrypoint.

ENTRYPOINT ["rm", "-rf", "/*"]

5.CMD [“program to run”, “parameter 1”, “parameter 2”]

The above is exec form, shell form: CMD command parameter 1 parameter 2
The command or script executed by default when starting the container, Dockerfile can only have one CMD command. If multiple commands are specified, only the last command is executed.
If a command is specified during docker run or there is an ENTRYPOINT in the image, then CMD will be overwritten.
CMD can provide default parameters for the ENTRYPOINT command.

ENTRYPOINT ["rm"]
CMD ["cp" ,"-rf","*"]

java -jar xxxxxxx.jar 8090

The command specified by docker run —-“ENTRYPOINT-“CMD

6.EXPOSE port number

Specifies the ports to open when new images are loaded into Docker

EXPOSE 8090

7.ENV environment variable variable value

Set the value of an environment variable, which will be used by subsequent RUN

linxu PATH=$PATH:/opt
ENV PATH $PATH:/opt

8.ADD source file/directory target file/directory

Copy the source file to the image. The source file must be in the same directory as the Dockerfile, or it must be a URL
There are following things to note:

  • If the source path is a file and the target path ends with /, docker will treat the target path as a directory and copy the source file to this directory.
    If the target path does not exist, it will be automatically created.
    /home/test/winwin.txt /home/test/

  • If the source path is a file and the target path does not end with /, docker will treat the target path as a file.
    If the target path does not exist, a file will be created with the name of the target path and the content will be from the same source as the file;
    If the target file is an existing file, it will be overwritten with the source file. Of course, only the content will be overwritten, and the file name will still be the target file name.
    If the target file actually exists in an existing directory, the source file will be copied to that directory. Note that in this case it is best to end the display with / to avoid confusion.
    A B
    /home/test /home/test

  • If the source path is a directory and the target path does not exist, docker will automatically create a directory with the target path and copy the files in the source path directory.
    If the target path is an existing directory, docker will copy the files in the source path directory to the directory.

  • If the source file is an archive file (compressed file), docker will automatically decompress it. The URL download and decompression features cannot be used together. Any compressed file copied via URL will not be automatically decompressed.

9.COPY source file/directory target file/directory

Only copy the files/directories on the local host to the target location. The source files/directories must be in the same directory as the Dockerfile.

10.VOLUME [“directory”]

Create a mount point in the container

11.USER username/UID

Specify the user when running the container

12.WORKDIR path /home

Specify the working directory for subsequent RUN, CMD, ENTRYPOINT

13.ONBUILD command

Specify the command to be run when the generated image is used as a base image.
When the ONBUILD instruction is added to a Dockerfile, the instruction will not have a substantial impact on using the Dockerfile to build an image (such as an A image).
But when writing a new Dockerfile to build an image based on the A image (for example, the B image), the ONBUILD instruction in the Dockerfile that constructs the A image will take effect. In the process of building the B image, it will first be executed. Only the instructions specified by the ONBUILD instruction will be executed before other instructions.

OBuild rm - rf /*

Note: If you have other dockerfiles in production, please study and read them by yourself, otherwise you will have to pay for the consequences.

14.HEALTHCHECK health check

When writing a Dockerfile, there is a strict format to follow:
(1) The first line must use the FROM instruction to specify the name of the image it is based on;

(2) Then use the MAINTAINER instruction to describe the user information that maintains the image;

(3) Then there are instructions related to mirroring operations, such as RUN instructions. Every time an instruction is run, a new layer is added to the base image.

(4) Finally, use the CMD command to specify the command operation to be run when the container is started.

4. Dockerfile case

1. Create a working directory

mkdir /opt/apache
cd /opt/apache

2. Based on the base image

vim Dockerfile
FROM centos:7
#Maintain user information of the image
MAINTAINER this is apache image <exo>
#Mirror operation instructions to install apache software
RUN yum -y update
RUN yum -y install httpd
#Open port 80
EXPOSE 80
#Copy website home page file
ADD index.html /var/www/html/index.html

(1) Method 1: Copy the execution script to the image

ADD run.sh /run.sh
RUN chmod 755 /run.sh
# Execute the script when starting the container
CMD ["/run.sh"]

(2) Method 2:

ENTRYPOINT [ "/usr/sbin/apachectl" ]
CMD ["-D", "FOREGROUND"]
//Prepare to execute script
vim run.sh
#!/bin/bash
rm -rf /run/httpd/* #Clear httpd cache
/usr/sbin/apachectl -D FOREGROUND #Specify to run in the foreground
#Because the Docker container will only keep running when its process No. 1 (PID is 1) is running. If process No. 1 exits, the Docker container will also exit.

//Prepare website page
echo "this is test web" > index.html

//generate image
docker build -t httpd:centos . #Be careful not to forget the "." at the end

//New image running container
docker run -d -p 40330:80 httpd:centos

3. Test

http://192.168.174.15:40330/

  • If there is a network error message

    [Warning] IPv4 forwarding is disabled. Networking will not work.
    
  • Solution:

vim /etc/sysctl.conf
net.ipv4.ip_forward=1

sysctl-p
systemctl restart network
systemctl restart docker

5. Extension

1. Build SSH mirror

(1) Create

mkdir /opt/sshd

cd /opt/sshd

vimDockerfile
#The first line must indicate the base image it is based on
FROM centos:7
#author information
MAINTAINER this is ssh image <exo>
#Mirror operation instructions
RUN yum -y update
RUN yum -y install openssh* net-tools lsof telnet passwd
RUN echo 'abc123' | passwd --stdin root
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config #Do not use PAM authentication
RUN sed -ri '/^session\s + required\s + pam_loginuid.so/ s/^/#/' /etc/pam.d/sshd #Cancel pam restrictions
RUN ssh-keygen -t rsa -A #Generate key authentication file
RUN mkdir -p /root/.ssh & amp; & amp; chown root.root /root & amp; & amp; chmod 700 /root/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd" , "-D"] #/usr/sbin/sshd -D is used to start the sshd service in the foreground

(2) Generate image

docker build -t sshd:centos .

(3) Start the container and modify the root password

docker run -d -P sshd:centos
[root@docker sshd]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c0fa893e27ae sshd:centos "/usr/sbin/sshd -D" 8 seconds ago Up 8 seconds 0.0.0.0:32768->22/tcp, :::32768->22/tcp sad_mclaren
386b414ba410 httpd:centos "/usr/sbin/apachectl…" 11 minutes ago Up 11 minutes 0.0.0.0:40330->80/tcp, :::40330->80/tcp admiring_lehmann
[root@docker sshd]# ssh localhost -p 32768

2.Systemctl image

(1) Create

mkdir /opt/systemctl
cd /opt/systemctl

vimDockerfile

FROM sshd:centos
MAINTAINER this is systemctl image <hmj>
ENV container docker
#Except systemd-tmpfiles-setup.service, delete all other files
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*; \
rm -f /etc/systemd/system/*.wants/*; \
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
#CMD ["/usr/sbin/init"]

(2) Generate image

docker build -t systemd:centos .

(3) Start the container, mount the host directory into the container, and initialize it

docker run --privileged -d -P -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:centos /sbin/init
#--privileged: Make root in the container have real root permissions. Otherwise, the root within the container is just an ordinary user outside the container.

docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
956e2486c3c7 systemd:centos "/sbin/init" 8 seconds ago Up 7 seconds 0.0.0.0:32769->22/tcp, :::32769->22/tcp sad_elbakyan
c0fa893e27ae sshd:centos "/usr/sbin/sshd -D" 3 minutes ago Up 3 minutes 0.0.0.0:32768->22/tcp, :::32768->22/tcp sad_mclaren
386b414ba410 httpd:centos "/usr/sbin/apachectl…" 14 minutes ago Up 14 minutes 0.0.0.0:40330->80/tcp, :::40330->80/tcp admiring_lehmann

(4) Enter the container

docker exec -it 956e2486c3c7 bash

systemctl status sshd
  • Method Two:

    docker run --privileged -it -P -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:centos /sbin/init & amp;
    

3.nginx mirror

(1) Create

mkdir /opt/nginx
cd /opt/nginx/
cp /opt/nginx/nginx-1.12.0.tar.gz /opt/nginx

vimDockerfile

#Based on base image
FROM centos:7
#User Info
MAINTAINER this is nginx image <exo>
#Add environment package
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
#Upload the nginx software compressed package and decompress it
ADD nginx-1.12.0.tar.gz /opt/
#Specify the working directory
WORKDIR /opt/nginx-1.12.0
RUN ./configure\
--prefix=/usr/local/nginx\
--user=nginx \
--group=nginx \
--with-http_stub_status_module & amp; & amp; make & amp; & amp; make install
ENV PATH /usr/local/nginx/sbin:$PATH
#Specify http and https ports
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf #Close nginx running in the background
#Add run.sh in the host to the container
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
#CMD ["/usr/local/sbin/nginx", "-g", "daemon off;"]

vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx

(2) Create a new image

docker build -t nginx:centos .

docker run -d -P nginx:centos

docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e48a80913ab9 nginx:centos "/run.sh" 14 seconds ago Up 12 seconds 0.0.0.0:32772->80/tcp, :::32772->80/tcp, 0.0.0.0:32771->443/tcp, :::32771->443/tcp musing_mayer
2cc3724ffa75 systemd:centos "/sbin/init" 6 minutes ago Up 6 minutes 0.0.0.0:32770->22/tcp, :::32770->22/tcp hardcore_yalow
956e2486c3c7 systemd:centos "/sbin/init" 7 minutes ago Up 7 minutes 0.0.0.0:32769->22/tcp, :::32769->22/tcp sad_elbakyan
c0fa893e27ae sshd:centos "/usr/sbin/sshd -D" 10 minutes ago Up 10 minutes 0.0.0.0:32768->22/tcp, :::32768->22/tcp sad_mclaren
386b414ba410 httpd:centos "/usr/sbin/apachectl…" 21 minutes ago Up 21 minutes 0.0.0.0:40330->80/tcp, :::40330->80/tcp admiring_lehmann
  • Use your browser to access http://192.168.174.15:32772

4.tomcat mirror

(1) Create

mkdir /opt/tomcat
cd /opt/tomcat
cp /opt/jdk-8u91-linux-x64.tar.gz /opt/tomcat
cp /opt/apache-tomcat-8.5.16.tar.gz /opt/tomcat

vimDockerfile

FROM centos:7
MAINTAINER this is tomcat image <exo>
ADD jdk-8u91-linux-x64.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv jdk1.8.0_91 /usr/local/java
ENV JAVA_HOME /usr/local/java
ENV JRE_HOME ${JAVA_HOME}/jre
ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
ENV PATH $JAVA_HOME/bin:$PATH
ADD apache-tomcat-8.5.16.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv apache-tomcat-8.5.16 /usr/local/tomcat
EXPOSE 8080
#CMD ["/usr/local/tomcat/bin/catalina.sh","run"]
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]

(2) Create a new image

docker build -t tomcat:centos .

docker run -d --name tomcat01 -p 1216:8080 tomcat:centos

(3) Browser access http://192.168.174.15:1216

5.mysql mirror

(1) Create

mkdir /opt/mysqld
cd /opt/mysqld

vimDockerfile

FROM centos:7
MAINTAINER this is mysql image <exo>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
RUN useradd -M -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 & amp; & amp; make & amp; & amp; make install
RUN chown -R mysql:mysql /usr/local/mysql/
RUN rm -rf /etc/my.cnf
ADD my.cnf /etc/
RUN chown mysql:mysql /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
WORKDIR /usr/local/mysql/
RUN bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
EXPOSE 3306
ADD run.sh /usr/local/src
RUN chmod 755 /usr/local/src/run.sh
RUN sh /usr/local/src/run.sh
#CMD ["/usr/sbin/init"]


vim my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user=mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id=1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES


vim run.sh
#!/bin/bash
/usr/local/mysql/bin/mysqld
systemctl enable mysqld

(2) Create a new image

docker build -t mysql:centos .

(3) Start the container and initialize it

docker run --name=mysql_server -d -P --privileged mysql:centos /usr/sbin/init

(4) Enter the container to give permission

docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f9a4d8f6c65f mysql:centos "/usr/sbin/init" 17 seconds ago Up 16 seconds 0.0.0.0:49153->3306/tcp mysql_server

(5) Enter the container and authorize the remote connection to mysql

docker exec -it f9a4d8f6c65f /bin/bash

mysql -u root -p
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
grant all privileges on *.* to 'root'@'localhost' identified by 'abc123';
flush privileges;

(6) Connect to the mysql container on the client side

mysql -h 192.168.174.15 -u root -P 49153 -pabc123