Docker网络和数据卷

docker 网络映射
1,随机映射,随机映射端口不会有冲突 -P

[root@zabbix-3 ~]# docker run -d -P --name mynginx1 nginx
ff5442b84da4b346e5470e3fc74c401ae14f7c3b3b72ad2d85ce04195f26976f
[root@zabbix-3 ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                           NAMES
ff5442b84da4        nginx               "nginx -g 'daemon off"   7 seconds ago       Up 5 seconds        0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp   mynginx1

[root@zabbix-3 ~]#
2.-p指定端口映射

[root@zabbix-3 ~]# docker run -d -p 91:80 --name mynginx2 nginx
219c145ad549d11c50d611838063fa387ae174b436f8cc6d1edae997baa7a571
[root@zabbix-3 ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
219c145ad549        nginx               "nginx -g 'daemon off"   10 seconds ago      Up 9 seconds        443/tcp, 0.0.0.0:91->80/tcp   mynginx2
[root@zabbix-3 ~]# 

3.docker数据存储,可参考:http://dockone.io/article/128
数据卷,可绕过ufs将数据写在物理主机上

[root@zabbix-3 ~]# docker run -it --name volume-test1 -h centos -v /data centos
[root@centos /]# ls /data/
[root@centos /]# ls /
anaconda-post.log  data  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
bin                dev   home  lib64  media       opt  root  sbin  sys  usr
[root@centos /]# 

[root@zabbix-3 ~]# docker run --name mark1 -v /data -ti centos /bin/bash
[root@fca3f6850e6a /]# ls -ld /data/
drwxr-xr-x. 2 root root 6 Jun 28 09:45 /data/
[root@fca3f6850e6a /]# df -Th
Filesystem                                                                                         Type   Size  Used Avail Use% Mounted on
/dev/mapper/docker-253:0-68314296-da4f78f8af9a7c3e89ac958fcb9801e6ccc778fd3eda6ed1da4cb9503938a03d xfs     10G  241M  9.8G   3% /
tmpfs                                                                                              tmpfs  1.9G     0  1.9G   0% /dev
tmpfs                                                                                              tmpfs  1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root                                                                            xfs     18G  3.0G   15G  17% /data
shm                                                                                                tmpfs   64M     0   64M   0% /dev/shm
[root@fca3f6850e6a /]# 

3.1查看卷位置

[root@zabbix-3 fce6e616518fb37c581a1a5b93247757ffae2dec861fc95bc3b3aae2ca53f523]# docker inspect mark1|awk '/"Source"/{print $2}'
"/var/lib/docker/volumes/fce6e616518fb37c581a1a5b93247757ffae2dec861fc95bc3b3aae2ca53f523/_data",
[root@zabbix-3 fce6e616518fb37c581a1a5b93247757ffae2dec861fc95bc3b3aae2ca53f523]# 

如下:

"Mounts": [
    {
        "Name": "fce6e616518fb37c581a1a5b93247757ffae2dec861fc95bc3b3aae2ca53f523",
        "Source": "/var/lib/docker/volumes/fce6e616518fb37c581a1a5b93247757ffae2dec861fc95bc3b3aae2ca53f523/_data",
        "Destination": "/data",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
],

/var/lib/docker/volumes/fce6e616518fb37c581a1a5b93247757ffae2dec861fc95bc3b3aae2ca53f523/_data文件其实上映射在容器内的data文件
在os上创建文件,在容器中则可以发现,他们几乎是相同的

[root@zabbix-3 _data]# cat /var/lib/docker/volumes/fce6e616518fb37c581a1a5b93247757ffae2dec861fc95bc3b3aae2ca53f523/_data/mark 
helo word!
[root@zabbix-3 _data]# 

查看

[root@zabbix-3 ~]# ./in.sh mark1
[root@fca3f6850e6a /]# ls /data/
mark
[root@fca3f6850e6a /]# cat /data/mark 
helo word!
[root@fca3f6850e6a /]# 

3.2指定目录挂载到容器中:
在opt中touch一个文件,而后挂载到容器中叫做ott

[root@zabbix-3 ~]# touch /opt/`date +%F-%T`.txt
[root@zabbix-3 ~]#

挂载到容器中,/opt和/ott之间用冒号间隔

[root@zabbix-3 _data]# docker run -it --name mark2 -h nginx -v /opt:/ott centos
[root@nginx /]# ll /ott/
total 0
-rw-r--r--. 1 root root 0 Jun 28 13:36 2016-06-28-09:36:45.txt
[root@nginx /]#

当我们容器的日志也可以这样存放到本机上,而这种映射在docke file上使用不了
如果我们挂载只读则可以添加ro

docker run -it --name mark2 -h nginx -v /opt:/ott:ro centos

4.挂载文件

[root@zabbix-3 ~]# docker run -it --name mark3 -h nginx -v /root/in.sh:/etc/in.sh centos
[root@nginx /]# ll /etc/in.sh 
-rwxr-xr-x. 1 root root 134 Jun 27 17:59 /etc/in.sh
[root@nginx /]# 

5.docker 数据卷容器
创建一个新的容器,新容器来自于mark1容器,容器共享

[root@zabbix-3 ~]# docker run -it --name mark4 --volumes-from mark1 centos
[root@6cf741b2ef70 /]# ls /data/
mark
[root@6cf741b2ef70 /]# 

0 分享

您可以选择一种方式赞助本站

支付宝扫码赞助

支付宝扫码赞助

日期: 2016-07-01分类: Docker

标签: docker

发表评论