宿主机的操作:
先删除原有的容器
[iyunv@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29e9a41e563d centos "/bin/bash" 11 minutes ago Up 11 minutes volume-test2
8884fc392971 nginx "nginx -g 'daemon of 13 hours ago Up 51 minutes 80/tcp, 443/tcp nginxdocker
14caa9ab03a9 dockerui/dockerui "/dockerui" 46 hours ago Up 51 minutes 0.0.0.0:9000->9000/tcp dockerui_qinwen
e278cabec91e centos "/bin/bash" 47 hours ago Up 51 minutes centos_aways
删除正在允许的容器时,会有报错信息:
[iyunv@docker ~]# docker rm 29e9a41e563d -f
Error response from daemon: Cannot destroy container 29e9a41e563d: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f
Error response from daemon: no such id: -f
Error: failed to remove containers: [29e9a41e563d -f]
[iyunv@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29e9a41e563d centos "/bin/bash" 11 minutes ago Up 11 minutes volume-test2
8884fc392971 nginx "nginx -g 'daemon of 13 hours ago Up 52 minutes 80/tcp, 443/tcp nginxdocker
14caa9ab03a9 dockerui/dockerui "/dockerui" 46 hours ago Up 52 minutes 0.0.0.0:9000->9000/tcp dockerui_qinwen
e278cabec91e centos "/bin/bash" 47 hours ago Up 52 minutes centos_aways
先停止容器,然后再进行删除:
[iyunv@docker ~]# docker stop 29e9a41e563d
29e9a41e563d
[iyunv@docker ~]# docker rm 29e9a41e563d
29e9a41e563d
[iyunv@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8884fc392971 nginx "nginx -g 'daemon of 13 hours ago Up 52 minutes 80/tcp, 443/tcp nginxdocker
14caa9ab03a9 dockerui/dockerui "/dockerui" 46 hours ago Up 52 minutes 0.0.0.0:9000->9000/tcp dockerui_qinwen
e278cabec91e centos "/bin/bash" 47 hours ago Up 52 minutes centos_aways
运行一个基于centos镜像的容器,容器名称为volume-test2,并设置容器中的主机名为centos7(仅对容器有效),挂载本地的/volume-test2-data以及/opt目录,并且指定/opt目录对容器是只读的
[iyunv@docker ~]# docker run -it -d --name volume-test2 -h centos7 --restart=always -v /volume-test2-data:/centos-data -v /opt:/opt:ro centos
37652d4601948fe8421c0371278e9bee8c5cbb35f16c7bf755764d56ddafa304
[iyunv@docker ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
37652d460194 centos "/bin/bash" 16 seconds ago Up 15 seconds volume-test2
[iyunv@docker ~]# ./in.sh 37652d460194
容器内的操作:
[iyunv@centos7 /]# cd /opt/
[iyunv@centos7 opt]# ls
rh
[iyunv@centos7 opt]# ls
opt.iso rh
[iyunv@centos7 opt]# touch 123.txt
touch: cannot touch ‘123.txt’: Read-only file system
--volume-from:
继承容器数据卷的操作:
[iyunv@docker ~]# docker run -it -d --restart=always --name volume-test04 --volumes-from volume-test2 centos
94ea720a2c418286d26068d126df35523742e2f910685863f876c3ba9c09638c
[iyunv@docker ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94ea720a2c41 centos "/bin/bash" 6 seconds ago Up 4 seconds volume-test04
[iyunv@docker ~]# ./in.sh 94ea720a2c41
[iyunv@94ea720a2c41 /]# ls /
anaconda-post.log centos-data etc lib lost+found mnt proc run srv tmp var
bin dev home lib64 media opt root sbin sys usr
[iyunv@94ea720a2c41 /]# cd /opt/
[iyunv@94ea720a2c41 opt]# ls
opt.iso rh
[iyunv@94ea720a2c41 opt]# cd /centos-data/
[iyunv@94ea720a2c41 centos-data]# ls
abrt-2.0.8 passwd
[iyunv@94ea720a2c41 centos-data]# touch 12345.txt
[iyunv@94ea720a2c41 centos-data]# cd /opt
[iyunv@94ea720a2c41 opt]# ls
opt.iso rh
[iyunv@94ea720a2c41 opt]# touch 111
touch: cannot touch ‘111’: Read-only file system
拥有volume-test2的容器的操作:
[iyunv@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94ea720a2c41 centos "/bin/bash" 45 seconds ago Up 43 seconds volume-test04
37652d460194 centos "/bin/bash" About an hour ago Up About an hour volume-test2
8884fc392971 nginx "nginx -g 'daemon of 14 hours ago Up About an hour 80/tcp, 443/tcp nginxdocker
14caa9ab03a9 dockerui/dockerui "/dockerui" 47 hours ago Up About an hour 0.0.0.0:9000->9000/tcp dockerui_qinwen
e278cabec91e centos "/bin/bash" 2 days ago Up About an hour centos_aways
[iyunv@docker ~]# ./in.sh 37652d460194
[iyunv@centos7 /]# ls
anaconda-post.log centos-data etc lib lost+found mnt proc run srv tmp var
bin dev home lib64 media opt root sbin sys usr
[iyunv@centos7 /]# cd /centos-data/
[iyunv@centos7 centos-data]# ls
abrt-2.0.8 passwd
[iyunv@centos7 centos-data]# ls
12345.txt abrt-2.0.8 passwd
[iyunv@docker ~]# docker run -it -d --restart=always --name volume-test04 --volumes-from volume-test2 centos
94ea720a2c418286d26068d126df35523742e2f910685863f876c3ba9c09638c
宿主机上的操作:
[iyunv@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94ea720a2c41 centos "/bin/bash" 14 minutes ago Up 14 minutes volume-test04
37652d460194 centos "/bin/bash" About an hour ago Up About an hour volume-test2
8884fc392971 nginx "nginx -g 'daemon of 14 hours ago Up 2 hours 80/tcp, 443/tcp nginxdocker
14caa9ab03a9 dockerui/dockerui "/dockerui" 47 hours ago Up 2 hours 0.0.0.0:9000->9000/tcp dockerui_qinwen
e278cabec91e centos "/bin/bash" 2 days ago Up 2 hours
[iyunv@docker ~]# docker stop 37652d460194
37652d460194
[iyunv@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94ea720a2c41 centos "/bin/bash" 14 minutes ago Up 14 minutes volume-test04
8884fc392971 nginx "nginx -g 'daemon of 14 hours ago Up 2 hours 80/tcp, 443/tcp nginxdocker
14caa9ab03a9 dockerui/dockerui "/dockerui" 47 hours ago Up 2 hours 0.0.0.0:9000->9000/tcp dockerui_qinwen
e278cabec91e centos "/bin/bash" 2 days ago Up 2 hours centos_aways
[iyunv@docker _data]# cd /volume-test2-data
[iyunv@docker volume-test2-data]# ls
12345.txt abrt-2.0.8 passwd
[iyunv@docker volume-test2-data]# ls -lrt
total 8
drwxr-xr-x 2 root root 4096 Feb 24 09:58 abrt-2.0.8
-rw-r--r-- 1 root root 692 Feb 24 10:01 passwd
-rw-r--r-- 1 root root 0 Feb 24 11:16 12345.txt
容器内的操作:
[iyunv@94ea720a2c41 opt]# cd /centos-data/
[iyunv@94ea720a2c41 centos-data]# ls -lrt
total 8
drwxr-xr-x 2 root root 4096 Feb 24 01:58 abrt-2.0.8
-rw-r--r-- 1 root root 692 Feb 24 02:01 passwd
-rw-r--r-- 1 root root 0 Feb 24 03:16 12345.txt
[iyunv@94ea720a2c41 centos-data]#