Docker data management and mirroring

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 directory of the host machine can be mounted on the data volume, and the modification operation on the data volume can be seen immediately, and updating the data will not affect the image, so as to realize the migration of data between the host machine and the container. The use of data volumes is similar to the mount operation on directories under Linux.

docker pull centos:7

#宿主机目录/var/www 挂载到容器中的/data1。
注意:宿主机本地目录的路径必须是使用绝对路径。如果路径不存在,Docker会自动创建相应的路径。
docker run -v /var/www:/data1 --name web1 -it centos:7 /bin/bash			#-v 选项可以在容器内创建数据卷
ls
echo "this is web1" > /data1/abc.txt
exit

#返回宿主机进行查看
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

#使用 --volumes-from 来挂载 web2 容器中的数据卷到新的容器
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 the container, if the corresponding port is not specified, the service inside the container cannot be accessed through the network outside the container. The port mapping mechanism provides services in the container to external network access. In essence, it maps the port of the host to the container, so that the external network can access the service in the container by accessing the port of the host.

docker run -d --name test1 -P nginx					#随机映射端口(从32768开始)

docker run -d --name test2 -p 43000:80 nginx		#指定映射端口

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

浏览器访问: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 container names. To put it simply, a tunnel will be established between the source container and the receiving container, and the receiving container can see the information specified by the source container.

#创建并运行源容器取名web1
docker run -itd -P --name web1 centos:7 /bin/bash	
	
#创建并运行接收容器取名web2,使用--link选项指定连接容器以实现容器互联
docker run -itd -P --name web2 --link web1:web1 centos:7 /bin/bash			#--link 容器名:连接的别名

#进web2 容器, ping web1
docker exec -it web2 bash
ping web1

2. Creation of Docker image

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

1. Create based on an existing image

(1) First start a mirror and make changes 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
#常用选项:
-m 说明信息;
-a 作者信息;
-p 生成过程中停止容器的运行。

docker images

2. Create based on local template

通过导入操作系统模板文件可以生成镜像,模板可以从 OPENVZ 开源项目下载,下载地址为http://openvz.org/Download/template/precreated

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

#导入为镜像
cat debian-7.0-x86-minimal.tar.gz | docker import - debian:test

3. Created based on Dockerfile

(1) Union File System (UnionFS)

  • UnionFS (Union File System): Union File System (UnionFS) is a layered, lightweight and high-performance file system that supports modifications to the file system as a single submission to superimpose layer by layer, while different The directory is mounted under the same virtual file system. AUFS, OverlayFS, and Devicemapper are all types of UnionFS.
  • The Union filesystem is the basis for Docker images. Images can be inherited through layers. Based on the base image (without a parent image), various specific application images can be made.
  • Features: Load multiple file systems at the same time, but from the outside, only one file system can be seen. Joint loading will superimpose the file systems of all layers, 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 complete, the entire kernel is in the memory. At this time, the right to use the memory has been transferred from the bootfs to the kernel. At this time, the system will also uninstall the bootfs.
  • rootfs, on top of bootfs. It contains standard directories and files such as /dev, /proc, /bin, /etc in a typical Linux system. Rootfs is a variety of different operating system distributions, such as Ubuntu, Centos and so on.

(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 program libraries. Because the underlying layer directly uses the host's kernel, you only need to

Provide rootfs on it. It can be seen that for different Linux distributions, the bootfs is basically the same, and the rootfs will be different, so different distributions can share the 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. ). Images do not contain any dynamic data, and their contents are not changed after they are built.

  • The customization of the image is actually to customize the configuration and files added by 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 the use of comments starting with "#".

(5) Layering of the Docker image structure
The image is not a single file, but consists of multiple layers. The container actually adds a read-write layer on top of the image, and 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 readable and writable 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 specified when building the image If the variable is different, the corresponding mirror layer cache will be invalid;
(4) If the mirror cache of a certain layer is invalid, the mirror layer cache after it will be invalid;
(5) The mirror layer is immutable, if in a certain layer Adding a file and then deleting it in the next layer will still contain the file 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

Indicates the maintainer information for 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

Specify the port to open when the new image is 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 should be located in the same directory as the Dockerfile, or a URL
with the following precautions:

  • 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 created automatically.
    /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 is the same as the source file;
    if the target file exists, it will be overwritten with the source file, of course, only the content is overwritten, and the file name is still the target file name.
    If the target file is actually an existing directory, the source file will be copied to that directory. Note that in this case it is better to end the display with a / to avoid confusion.
    AB
    /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 this 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 files copied by 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 should 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

Specifies the command to 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 a mirror based on the A mirror (such as a B mirror), the ONBUILD command in the Dockerfile that constructs the A mirror will take effect at this time. In the process of building the B mirror, it will first execute The instructions specified by the ONBUILD instruction are executed before other instructions are executed.

OBuild rm - rf /*

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

14. HEALTHCHECK health check

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

(2) Then use the MAINTAINER command to describe the user information for maintaining 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. Base image based on

vim Dockerfile
FROM centos:7
#维护镜像的用户信息
MAINTAINER this is apache image <exo>
#镜像操作指令安装apache软件
RUN yum -y update
RUN yum -y install httpd
#开启 80 端口
EXPOSE 80
#复制网站首页文件
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
#启动容器时执行脚本
CMD ["/run.sh"]

(2) Method 2:

ENTRYPOINT [ "/usr/sbin/apachectl" ]
CMD ["-D", "FOREGROUND"]
//准备执行脚本
vim run.sh
#!/bin/bash
rm -rf /run/httpd/*							#清理httpd的缓存
/usr/sbin/apachectl -D FOREGROUND			#指定为前台运行
#因为Docker容器仅在它的1号进程(PID为1)运行时,会保持运行。如果1号进程退出了,Docker容器也就退出了。

//准备网站页面
echo "this is test web" > index.html

//生成镜像
docker build -t httpd:centos .   		#注意别忘了末尾有"."

//新镜像运行容器
docker run -d -p 40330:80 httpd:centos

3. Test

http://192.168.174.15:40330/

insert image description here

  • 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. Expansion

1. Build SSH image

(1) create

mkdir /opt/sshd

cd /opt/sshd

vim Dockerfile
#第一行必须指明基于的基础镜像
FROM centos:7
#作者信息
MAINTAINER this is ssh image <exo>
#镜像的操作指令
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						#不使用PAM认证
RUN sed -ri '/^session\s+required\s+pam_loginuid.so/ s/^/#/' /etc/pam.d/sshd	#取消pam限制
RUN ssh-keygen -t rsa -A														#生成密钥认证文件
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd" , "-D"]			#/usr/sbin/sshd -D 用于前台启动sshd服务

(2) Generate a mirror 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

vim Dockerfile

FROM sshd:centos
MAINTAINER this is systemctl image <hmj>
ENV container docker
#除了systemd-tmpfiles-setup.service,删除其它所有文件
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 a mirror image

docker build -t systemd:centos .

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

docker run --privileged -d -P -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:centos /sbin/init
#--privileged:使container内的root拥有真正的root权限。否则,container内的root只是外部的一个普通用户权限。

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) into 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 &
    

insert image description here

3. nginx mirror

(1) create

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

vim Dockerfile

#基于基础镜像
FROM centos:7
#用户信息
MAINTAINER this is nginx image <exo>
#添加环境包
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
#上传nginx软件压缩包,并解压
ADD nginx-1.12.0.tar.gz /opt/
#指定工作目录
WORKDIR /opt/nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
#指定http和https端口
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf			#关闭 nginx 在后台运行
#添加宿主机中run.sh到容器中
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
  • Access http://192.168.174.15:32772 with a browser

insert image description here

4. tomcat image

(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

vim Dockerfile

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

insert image description here

5.mysql mirror

(1) create

mkdir /opt/mysqld
cd /opt/mysqld

vim Dockerfile

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 && make && 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

Guess you like

Origin blog.csdn.net/Riky12/article/details/132402779