您好,登錄后才能下訂單哦!
一.Docker數據管理方式簡介
Docker提供三種不同的方式將數據從宿主機掛載到容器中:volumes,bind mounts和tmpfs。
1.Volumes:Docker管理宿主機文件系統的一部分(/var/lib/docker/volumes)。
2.bind mounts:可以掛載在宿主機系統的任意位置。
3.tmpfs: 掛載存儲在宿主機系統的內存中,而不會寫入宿主機的文系統。
二管理volume卷
1.管理volume卷
#顯示卷
[root@docker sky9890]# docker volume ls
DRIVER VOLUME NAME
local 0b37cdeff2004e6f6293ba0175df953e93a0244c945dd030a27c9c2d70015473
……
#創建卷
[root@docker sky9890]# docker volume create nginx-vol
nginx-vol
[root@docker sky9890]# docker volume ls
local nginx-vol
[root@docker sky9890]# docker volume inspect nginx-vol
[
{
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/nginx-vol/_data",
"Name": "nginx-vol",
"Options": {},
"Scope": "local"
}
]
2.用卷創建一個容器:
[root@docker sky9890]#
docker run -d --name=nginx-test --mount src=nginx-vol,dst=/usr/share/nginx/html nginx
unknown flag: --mount
[root@docker sky9890]# docker run -d -it --name=nginx-001 -v nginx-vol:/usr/share/nginx/html nginx
2c3e14431a622a4c9af303596853cc5b59696c42c4da5e264dfc190e7f5f4c71
#執行nginx-001容器
[root@docker sky9890]# docker exec -it nginx-001 bash
root@2c3e14431a62:/#
root@2c3e14431a62:/# cd /usr/share/nginx/html/
root@2c3e14431a62:/usr/share/nginx/html# ls
50x.html index.html
#在容器中創建一個test.html文件
root@2c3e14431a62:/usr/share/nginx/html# touch test.html
root@2c3e14431a62:/usr/share/nginx/html# ls
50x.html index.html test.html
root@2c3e14431a62:/usr/share/nginx/html# vim test.html
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<h2>歡迎學習Docker!</h2>
</html>
#在volumes數據卷中同步了容器中創建的文件
[root@docker sky9890]# cd /var/lib/docker/volumes/nginx-vol/_data/
[root@docker _data]# ls
50x.html index.html
[root@docker _data]# ls
50x.html index.html test.html
#刪除所有容器
#Docker rm –f $(docker ps –q –a)
#Docker ps -a
#創建nginx-003容器
[root@docker _data]# docker run -d -it --name=nginx-003 -p 8081:80 -v nginx-vol:/usr/share/nginx/html nginx
7aacbe13f3817e54965e0c7d48969d465665a28e4413b418066385805a08b60f
#停止nginx-test容器
[root@docker _data]# docker container stop nginx-test
nginx-test
#刪除nginx-test容器
[root@docker _data]# docker container rm nginx-test
nginx-test
#無法刪除nginx-vol卷,原因是還在使用
[root@docker _data]# docker volume rm nginx-vol
Error response from daemon: Unable to remove volume, volume still in use: remove nginx-vol: volume is in use - [2c3e14431a622a4c9af303596853cc5b59696c42c4da5e264dfc190e7f5f4c71, e64dab498554ee101b0157cbbf4fbe5d5295ec2519327c6afbe042e60222d618, 7aacbe13f3817e54965e0c7d48969d465665a28e4413b418066385805a08b60f]
docker stop nginx-vol
docker volume rm nginx-vol
docker stop nginx-001
docker stop nginx-002
docker stop nginx-003
[root@docker _data]# docker ps
#將所有容器停止了也法刪除nginx-vol卷
小結:
1.如果沒有指定卷,則會自動創建。
2.教練建議使用—mount,更通用。但是使用mount就報錯。
三.Bind Mounts
#先刪除所有容器
[root@docker sky9890]# docker rm -f $(docker ps -q -a)
7aacbe13f381
e64dab498554
2c3e14431a62
01791c36ed28
......
fe6672a1ba22
#創建卷
#方式一:必須先創建原有目錄,否則報錯
[root@docker sky9890]# mkdir -p /app/wwwrott
[root@docker sky9890]#
docker run -d -it --name=nginx-test
--mount type=bind,src=/app/wwwroot,dst=/usr/share/nginx/html nginx
unknown flag: --mount
#mount報錯
方式二:
[root@docker sky9890]# docker run -d -it --name=nginx-test
-v /app/wwwroot:/usr/share/nginx/html nginx
ee97dec7d52b310eb3d93f42e05f272e3f97e23b5c49625966310d86b834ab0d
#執行容器
[root@docker sky9890]# docker exec -it nginx-test bash
root@ee97dec7d52b:/#
root@ee97dec7d52b:/usr/share/nginx/html# ls
root@ee97dec7d52b:/usr/share/nginx/html# ls
index.html
root@ee97dec7d52b:/usr/share/nginx/html# cat index.html
<html>
<meta http-equive="Cent-Type" content="text/html; charset=utf-8" />
<h2>歡迎學習Docker!</h2>
</html>
#在宿主機上創建index.html,會同步到容器指定目錄中
[root@docker wwwroot]# pwd
/app/wwwroot
[root@docker wwwroot]# ls
[root@docker wwwroot]# touch index.html
[root@docker wwwroot]# ls
index.html
[root@docker wwwroot]# vim index.html
<html>
<meta http-equive="Cent-Type" content="text/html; charset=utf-8" />
<h2>歡迎學習Docker!</h2>
</html>
#驗證綁定
[root@docker wwwroot]# docker inspect nginx-test
#docker container stop nginx-test
#docker container rm nginx-test
小結:
1.如果源文件/目錄沒有存在,不會自動創建,會拋出一個錯誤 。
2.如果掛載目錄在容器中非空目錄,則該目錄現有內容將被隱藏。
四.創建數據卷容器過程中的問題
在使用volume與Bind Mounts創建容器中使用的mount參數會報如下錯誤:
Docker Unknown flag --mount
原因如下:
docker run support for the --mount option was only introduced in Docker 17.06. You are using Docker 1.13.1. You have two choices:
1. Update to Docker 17.06 or later if you can;
2. Use the -v approach to bind mount the volume you require e.g. docker run -v $(pwd):/home
解決步驟:升級docker版本
1.查找主機上相關軟件包
[root@docker /]# rpm -qa|grep docker
docker-1.13.1-108.git4ef4b30.el7.centos.x86_64
docker-client-1.13.1-108.git4ef4b30.el7.centos.x86_64
docker-common-1.13.1-108.git4ef4b30.el7.centos.x86_64
2.卸載軟件相關包
[root@docker /]# yum remove docker-1.13.1-108.git4ef4b30.el7.centos.x86_64
警告:/etc/sysconfig/docker-storage 已另存為 /etc/sysconfig/docker-storage.rpmsave
警告:/etc/docker/daemon.json 已另存為 /etc/docker/daemon.json.rpmsave
[root@docker /]#yum remove docker-client-1.13.1-108.git4ef4b30.el7.centos.x86_64
[root@docker /]# ym remove docker-common-1.13.1-108.git4ef4b30.el7.centos.x86_64
[root@docker sky9890]# docker
bash: docker: 未找到命令
3.升級到最新版本
[root@docker sky9890]# curl -fssl https://get.docker.com/ |sh
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:
sudo usermod -aG docker your-user
Remember that you will have to log out and back in for this to take effect!
WARNING: Adding a user to the "docker" group will grant the ability to run
containers which can be used to obtain root privileges on the
docker host.
Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
for more information.
4.重啟Docker
#systemctl rstart docker
5.設置開機啟動
#systemctl enable docker
6.查看docker版本信息
[root@docker sky9890]# docker version
Client: Docker Engine - Community
Version: 19.03.7
API version: 1.40
Go version: go1.12.17
Git commit: 7141c199a2
Built: Wed Mar 4 01:24:10 2020
OS/Arch: linux/amd64
Experimental: false
......
7.查看容器信息
[root@docker sky9890]# docker info
Client:
......
Kernel Version: 3.10.0-862.11.6.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 983.3MiB
Name: docker
ID: E33X:T43J:BY3N:CFKG:6L7Q:XSGB:JZPA:GFNI:QHT5:LSAU:YDZW:BPAI
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
WARNING: IPv4 forwarding is disabled
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
8.查看鏡像
[root@docker sky9890]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sky9899 self 1302c27299d6 20 hours ago 1.22MB
busybox v2 6e9545b1e2a2 20 hours ago 1.22MB
tomcat latest 4e7840b49fad 6 days ago 529MB
nginx latest a1523e859360 7 days ago 127MB
python 3.5 0320ef7199ca 7 days ago 909MB
mysql latest c8ad2be69a22 7 days ago 465MB
php latest e66ae809d99a 7 days ago 405MB
httpd latest c5a012f9cf45 7 days ago 165MB
mongo latest bcef5fd2979d 12 days ago 386MB
ubuntu latest 72300a873c2c 12 days ago 64.2MB
centos latest 470671670cac 6 weeks ago 237MB
busybox latest 6d5fcfe5ff17 2 months ago 1.22MB
nginx 1.11 5766334bdaa0 2 years ago 183MB
nginx 1.11 5766334bdaa0 2 years ago 183MB
nginx v1 5766334bdaa0 2 years ago 183MB
WARNING: IPv4 forwarding is disabled
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
9.解決上述三年報警問題
[root@docker sky9890]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
[root@docker sky9890]# sysctl -p
10.測試mount
[root@docker sky9890]# docker run -d -it --name=nginx-test --mount type=bind,src=/app/wwwroot,dst=/usr/share/nginx/html nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
68ced04f60ab: Already exists
28252775b295: Pull complete
a616aa3b0bf2: Pull complete
Digest: sha256:2539d4344dd18e1df02be842ffc435f8e1f699cfc55516e2cf2cb16b7a9aea0b
Status: Downloaded newer image for nginx:latest
docker: Error response from daemon: Conflict. The container name "/nginx-test" is already in use by container "ee97dec7d52b310eb3d93f42e05f272e3f97e23b5c49625966310d86b834ab0d". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
[root@docker sky9890]# docker run -d -it --name=nginx --mount type=bind,src=/app/wwwroot,dst=/usr/share/nginx/html nginx
15ef64d51e113d571b0cb6fb89d3b891bcba826f4cc2ae2e3ad584294274aa9d
[root@docker sky9890]# docker start nginx
nginx
[root@docker sky9890]# docker exec -it nginx bash
root@15ef64d51e11:/#
root@15ef64d51e11:/# ls /usr/share/nginx/html/test.html
/usr/share/nginx/html/test.html
root@15ef64d51e11:/# cat /usr/share/nginx/html/test.html
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<h2>
Sovle Docker Unknown flag --mount
</h2>
</html>
#在宿舍機上創建一個文件,會同步到容器指定目錄下
[root@docker wwwroot]# vim test.html
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<h2>
Sovle Docker Unknown flag --mount
</h2>
</html>
#重新創建一個容器,并指一個端口
[root@docker sky9890]# docker run -d -it --name=nginx01 -p 8080:80 --mount type=bind,src=/app/wwwroot,dst=/usr/share/nginx/html nginx
6cc7f85a4cd1cc0fb23f2a60a1ae74bec2812d7a607ce7800f9f2db97ba8a052
#測試成功
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。