转载:https://www.bilibili.com/video/BV1og4y1q7M4?p=24&spm_id_from=pageDriver
注:狂神说,永远的神
docker的理念回顾
将应用和环境打包成一个镜像!
数据?如果把数据都在容器中,那么我们容器删除,数据就会丢失!需求:数据可以持久化
MySQL,容器删了,删库跑路!需求:MySQL数据可以存储在本地!
容器之间可以有一个数据共享的技术!Docker容器中产生的数据,同步到本地!
这就是卷技术!目录的挂载,将我们的容器内的目录,挂载到Linux上面!
说白了,就是数据同步机制
总结:容器的持久化和同步操作,荣期间也可以数据共享
方式一:直接使用命令来挂载 -v
docker run -it -v 主机目录:容器内目录 #测试 #宿主机 zzw@zzw-virtual-machine:$ sudo docker run -it -v ~/Desktop/ceshi:/home centos /bin/bash zzw@zzw-virtual-machine:$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b4875ed327d4 centos "/bin/bash" 27 seconds ago Up 27 seconds ecstatic_galois b178949f719a tomcat "/bin/bash" 54 minutes ago Up 54 minutes 0.0.0.0:3345->8080/tcp, :::3345->8080/tcp serene_keller 41a75e42ac6a nginx "/docker-entrypoint.…" 56 minutes ago Up 56 minutes 0.0.0.0:3344->80/tcp, :::3344->80/tcp amazing_galileo a88ab898993a portainer/portainer "/portainer" 18 hours ago Up 3 hours 0.0.0.0:8088->9000/tcp, :::8088->9000/tcp sleepy_cori zzw@zzw-virtual-machine:~/Desktop/ceshi$ ls test1.java test2.java #容器内 zzw@zzw-virtual-machine:~$ sudo docker attach b4875ed327d4 [root@b4875ed327d4 /]# cd /home [root@b4875ed327d4 home]# ls test1.java test2.java
思考:MySQL的数据持久化的问题
#1、启动mysql #2、运行容器,需要进行数据挂载,同步配置文件、数据、配置密码 #解释 -d #后台运行 -p #端口映射 -v #数据卷挂载 -e #环境配置 --name #修改容器名字 zzw@zzw-virtual-machine:~$ sudo docker run -d -p 3310:3306 -v ~/Desktop/mysql/conf:/etc/mysql/conf.d -v ~/Desktop/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 --privileged=true mysql:8.0
#匿名挂载 -v 容器内路径! docker run -d -P --name nginx01 -v /etc/nginx nginx #查看所有卷(volume)的情况 docker volume ls #这里发现,这种就是匿名挂载,我们在-v 只写了容器内的路径,没有写宿主机的路径
#具名挂载 docker run -d -P --name nginx01 -v juming-nginx:/etc/nginx nginx #查看具名挂载路径 docker volume inspect juming-nginx
参考:链接
解决问题:
zzw@zzw-virtual-machine:~$ sudo docker restart mysql01 mysql01 zzw@zzw-virtual-machine:~$ sudo docker exec -it 6205b543f256 /bin/bash root@6205b543f256:/# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 15 Server version: 8.0.24 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. #ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456' #注意:虚拟机时用下面这句 mysql> ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY '123456'; Query OK, 0 rows affected (0.00 sec) # 刷新权限 mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)