Docker——网络和存储(数据卷)

iptables -t nat -vnL |grep docker

查看docker桥接网卡:brctl show

本地端口随机映射到docker容器的80端口上: docker run -d -P nginx

查看日志:docker logs 容器_id

nginx 必须放在前台运行

本地端口指定映射到docker容器的80端口上:#81是宿主机端口,80是dokcer容器里面的端口

docker run -d -p 81:80 nginx

 

存储:

 

数据卷:所有的docker都能连接到上面

挂载一个 /data0到centos(dokcer容器)上: docker run -it --name volume-test1 -v /data0 centos

相当于nfs挂载一样

查看挂载信息:docker  inspect 容器_id

 

指定目录挂载:docker run -it  -v /opt:/opt centos

这是docker容器的/opt目录下内容

这是物理挤/opt下目录内容

 

挂载到指定目录并且指定权限:docker run -it  -v /data0:/data0:rw centos

 

但是 docker容器那面还是没权限这是因为centos的安全模块seliunx限制

 1.在运行容器的时候,给容器加特权,及加上 --privileged=true 参数:
   docker run -i -t -v /soft:/soft --privileged=true 686672a1d0cc /bin/bash
   2.临时关闭selinux:
   setenforce 0
   3.添加selinux规则,改变要挂载的目录的安全性文本
# 更改安全性文本的格式如下
chcon [-R] [-t type] [-u user] [-r role] 文件或者目录

 chcon -Rt svirt_sandbox_file_t /data0/

选顷不参数: 
-R  :连同该目录下癿次目录也同时修改; 
-t  :后面接安全性本文的类型字段!例如 httpd_sys_content_t ; 
-u  :后面接身份识别,例如 system_u; 
-r  :后面街觇色,例如 system_r

[root@localhost Desktop]# chcon --help
Usage: chcon [OPTION]... CONTEXT FILE...
  or:  chcon [OPTION]... [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE...
  or:  chcon [OPTION]... --reference=RFILE FILE...
Change the SELinux security context of each FILE to CONTEXT.
With --reference, change the security context of each FILE to that of RFILE.

Mandatory arguments to long options are mandatory for short options too.
      --dereference      affect the referent of each symbolic link (this is
                         the default), rather than the symbolic link itself
  -h, --no-dereference   affect symbolic links instead of any referenced file
  -u, --user=USER        set user USER in the target security context
  -r, --role=ROLE        set role ROLE in the target security context
  -t, --type=TYPE        set type TYPE in the target security context
  -l, --range=RANGE      set range RANGE in the target security context
      --no-preserve-root  do not treat '/' specially (the default)
      --preserve-root    fail to operate recursively on '/'
      --reference=RFILE  use RFILE's security context rather than specifying
                         a CONTEXT value
  -R, --recursive        operate on files and directories recursively
  -v, --verbose          output a diagnostic for every file processed

The following options modify how a hierarchy is traversed when the -R
option is also specified.  If more than one is specified, only the final
one takes effect.

  -H                     if a command line argument is a symbolic link
                         to a directory, traverse it
  -L                     traverse every symbolic link to a directory
                         encountered
  -P                     do not traverse any symbolic links (default)

      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'chcon invocation'

 

在主机中修改/soft目录的安全性文档

[root@localhost Desktop]# chcon -Rt svirt_sandbox_file_t /soft

[root@ba471da26d07 soft]# ll
total 384264
-rw-r--r--. 1 root root 212046774 Aug  8 10:01 hadoop-2.7.2.tar.gz
-rw-r--r--. 1 root root 181435897 Aug  8 09:23 jdk-8u102-linux-x64.tar.gz

 

在docker中就可以正常访问该目录下的相关资源了。

 

 

        数据卷容器:

 

 

创建一个名字叫nfs的容器:docker run -d --name nfs -v /data centos

 

启动一个test1的容器并进入,使用名字叫nfs的卷:docker run -it --name test1 --volumes-from nfs centos

可以做成集群共享资源

 

 

docker提供了一种机制,可以让一个容器和另一个容器很方便的连接起来。举例:

 

docker run --name test1 --link myweb:web -it ubuntu /bin/bash

 

上面命令创建了一个新的容器test1。 这里引入了一个新的标记 --link,其参数部分的myweb表示要连接的容器,web是要连接的容器的别名。

 

posted @ 2018-04-16 11:19  努力乄小白  阅读(387)  评论(0编辑  收藏  举报