RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Docker的数据管理方式

一.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

欢迎学习Docker!

#在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

欢迎学习Docker!

 #在宿主机上创建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

欢迎学习Docker!

#验证绑定

[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  

Sovle Docker Unknown flag --mount

#在宿舍机上创建一个文件,会同步到容器指定目录下

[root@docker wwwroot]# vim  test.html

Sovle Docker Unknown flag --mount

 #重新创建一个容器,并指一个端口 

[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

#测试成功

Docker的数据管理方式

Docker的数据管理方式



本文标题:Docker的数据管理方式
本文URL:http://lswzjz.com/article/jhjhij.html