docker run -d --name web01 -p 82:80 -v /data/test/htdocs/:/usr/local/apache2/htdocs/ httpd
3.查看容器状态
代码语言:shell复制
[root@docker test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05d35ba971ad httpd "httpd-foreground" About a minute ago Up About a minute 0.0.0.0:82->80/tcp, :::82->80/tcp web01
[root@docker test]# pwd
/data/test
[root@docker test]# cd htdocs/
[root@docker htdocs]# ls
[root@docker htdocs]#
2.新建index.html文件
代码语言:shell复制
echo "Docker technology plays an important role in today's society." > index.html
3.查看容器内index.html
代码语言:shell复制
[root@docker htdocs]# docker exec -it web01 /bin/bash
root@05d35ba971ad:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
root@05d35ba971ad:/usr/local/apache2# cd htdocs/
root@05d35ba971ad:/usr/local/apache2/htdocs# ls
index.html
root@05d35ba971ad:/usr/local/apache2/htdocs# cat index.html
Docker technology plays an important role in today's society.
四、删除测试容器,查看宿主机index.heml
代码语言:shell复制
[root@docker htdocs]# docker stop web01
web01
[root@docker htdocs]# docker rm web01
web01
[root@docker htdocs]# ls
index.html
[root@docker htdocs]# cat index.html
Docker technology plays an important role in today's society.
[root@docker htdocs]# docker exec -it web02 /bin/bash
root@f028c9fbcddb:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
root@f028c9fbcddb:/usr/local/apache2# cd htdocs/
root@f028c9fbcddb:/usr/local/apache2/htdocs# echo "Docker technology plays an important role in today's society." > index.html
root@f028c9fbcddb:/usr/local/apache2/htdocs#
4.宿主机访问内容
代码语言:shell复制
[root@docker htdocs]# curl 127.0.0.1:85
Docker technology plays an important role in today's society.
5.在宿主机默认挂载卷检查index.html
代码语言:shell复制
[root@docker htdocs]# docker inspect web02 |grep Mounts -A5
"Mounts": [
{
"Type": "volume",
"Name": "3b1eab4eb072a43375068c8ff534028de2a7f6dd6aeceb60c0d1584c4bc046a6",
"Source": "/var/lib/docker/volumes/3b1eab4eb072a43375068c8ff534028de2a7f6dd6aeceb60c0d1584c4bc046a6/_data",
"Destination": "/usr/local/apache2/htdocs",
[root@docker htdocs]# cd /var/lib/docker/volumes/3b1eab4eb072a43375068c8ff534028de2a7f6dd6aeceb60c0d1584c4bc046a6/_data
[root@docker _data]# ls
index.html
[root@docker _data]# cat index.html
Docker technology plays an important role in today's society.
6.删除容器,验证数据持久化
代码语言:shell复制
[root@docker _data]# docker stop web02
web02
[root@docker _data]# docker rm web02
web02
[root@docker _data]# ls
index.html
[root@docker _data]# pwd
/var/lib/docker/volumes/3b1eab4eb072a43375068c8ff534028de2a7f6dd6aeceb60c0d1584c4bc046a6/_data
[root@docker _data]# cat index.html
Docker technology plays an important role in today's society.
[root@docker htdocs]# docker exec -it test04 /bin/bash
OCI runtime exec failed: exec failed: unable to start container process: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown
[root@docker htdocs]# docker exec -it test04 /bin/sh
/ # cat /usr/local/apache2/htdocs/index.html
Docker technology plays an important role in today's society.
/ #
4.在test04容器挂载路径创建文件,验证宿主机路径信息
代码语言:shell复制
/ # cd /other/tools/
/other/tools # ls
/other/tools # echo "aa-test-out " > test.out
/other/tools #