数据卷:
实现宿主机和容器进行数据共享平台
nginx-
将宿主机的/web/挂载到容器中的/usr/share/nginx/html
宿主机变容器变
[root@mytest web]# docker run -d -p 80:80 --name docker_nginx -v /web:/usr/share/ng inx/html nginx
60e4dd60173dd136612337c6de02fb2c0ff2cff202d770774777fd7dfe676e45
修改宿主机的index.html:
修改后:
mysql-
1、先打一台容器,让宿主机的/mysql挂载进去获得文件数据:
[root@mytest mysql]# docker run -d -p 3307:3306 -v /mysql:/etc/mysql -v /mysql_data :/var/lib/mysql --name my_mysql2 -e MYSQL_ROOT_PASSWORD='Tzkwan1314=' mysql:5.7
6d703a281972f65cef676b2ad8a1067b31a23ac16f0bba0841c82aa93a9f8833
root@ec3f26f40a26:/mnt# cp /etc/mysql . -r
root@ec3f26f40a26:/mnt# ls
mysql
root@ec3f26f40a26:/mnt# cp /var/lib/mysql mysql_data -r
root@ec3f26f40a26:/mnt# ls
mysql mysql_data
2、查看宿主机上是否有数据过来:
[root@mytest mysql]# ls
mysql mysql_data
3、再起一个新的容器,把这两个文件夹分别挂载到容器里的/etc/mysql /var/lib/mysql
[root@mytest mysql]# docker run -d -p 3308:3306 -v /mysql/mysql:/etc/mysql -v /mysq l/mysql_data:/var/lib/mysql --name my_mysql3 -e MYSQL_ROOT_PASSWORD='Tzkwan1314=' m ysql:5.7
5d20a6b39a7f8d7d5076ebf7c8924e7782b176576edbd83d67dc2e6a5f74a314
[root@mytest mysql]# docker exec -it my_mysql3 bash
root@5d20a6b39a7f:/# mysql -uroot -pTzkwan1314=
mysql: [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 2
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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.
mysql>
4、修改宿主机上的数据,查看容器重新连接mysql后的变化:
[root@mytest mysql]# ls
mysql mysql_data
[root@mytest mysql]# cd mysql
[root@mytest mysql]# ls
conf.d my.cnf my.cnf.fallback mysql.cnf mysql.conf.d
[root@mytest mysql]# cd conf.d/
[root@mytest conf.d]# ls
docker.cnf mysql.cnf mysqldump.cnf
[root@mytest conf.d]# vim mysql.cnf
[mysql]
auto-rehash
prompt=\\u@\\d \\R:\\m mysql>
mysql> exit
Bye
root@5d20a6b39a7f:/# mysql -uroot -pTzkwan1314=
mysql: [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 3
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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.
root@(none) 08:05 mysql>