运行一个container的本身就是开启一个具有独立namespace的进程
进程有自己的网络,文件系统等.
docker通过run命令来启动一个container
运行一个container必须要指定一个image作为初始化的文件系统
对于不存在的image,docker会自动去registry里面下载对应的image,然后再运行container
一.网络

docker使用bridge桥接的方式来实现container之间以及和外部的通信
查看host主机的网络信息

两张veth网卡对应两个container容器(正在运行的)
veth网络都桥接在docker0上(被分配)

可以用docker inspect d9f82680332d 查看容器网络信息

network架构:
在host主机上的一个veth{id}的虚拟网卡和一个container里面的eth0网卡一一映射
host上的bridge负责把数据流在不同的veth间转发,实现网络的IO
bridge(docker0)使用RFC1918私有网络,给每一个container分配ip
网络设置:
通过--net参数来修改container的网络设置,默认是bridge的方式
none表示关闭container的网路连接
host表示使用主机的网络栈,这个时候host主机不会创建veth虚拟网卡映射到container
container的网络和主机host的网络在同一网段
[root@VM_82_228_centos ~]# docker run -d --net host docker.io/centos python -m SimpleHTTPServer
3e69b247f2da554ddaa19424cece0654f54e4a832a187723a2a18d027dec3d77
-d:创建在后台运行
--net host:创建在本机host网络上
python -m SimpleHTTPServer:在容器内执行的命令(命令进程不终止,容器状态也不会停止)
host主机查看进程



如果不将容器的网络挂在本机host上,那么如何进行访问容器的服务呢?
将本机host的端口同容器内的端口进行映射,详情看下面端口映射
DNS:
默认使用host的dns设置
可以通过--dns的参数来指定container自己的dns配置
端口映射:
docker通过采用端口映射的方式,允许把内部container的服务端口暴露到外部
使用-p参数可以指定需要暴露的container的内部端口,在不指定特定的host的对应端口的情况下,docker会自动分配(49000-49900)在一个host上的端口与其映射
使用-P参数,表示暴露所有在image中通过EXPOSE指定的端口
root@docker ~]# docker run -dt -p 8000 saltstack/ubuntu-14.04 python -m SimpleHTTPServer
f7d04a31f5a2d6220a70c8dd7d78a5819bd722b3baab9e743c6c098533bd96ec
p 8000:表示将容器内的8000端口暴露了出来,由于这里没有明确指定用本机host的哪个端口与之映射,所以这里采用了随机端口
-p 8000:80 -p 443:443:映射容器中的多个端口(前面本机host端口,后面container端口)



[root@docker ~]# docker run -dt -p 80:8000 saltstack/ubuntu-14.04 python -m SimpleHTTPServer
a9c3d1c7b6f4319ee34fe726eb5989b7f20bdf4e75e26ef4d5e734665c7efa75

如果想映射多个端口时:
-p 8000:80 -p 443:443:映射容器中的多个端口,只需要在后面连接映射本机host端口对应container端口就行
或者直接-P暴露掉container中的所有端口对应host本机的随机端口
二.数据卷
volume绑定:
通过-v参数,可以把host上的一个目录绑定到container中,允许container对其进行读写
-v /zam/web:/web 将本机host下面的test目录映射到container中下面的web目录
docker run -t -v /zam/web/:/web docker.io/centos sh -c "ls -al web "

docker 数据卷
-v, --volume list Bind mount a volume, 为container绑定一个数据卷
docker run -it -v /zam/web/ -h shuju docker.io/centos

-i:交互模式,直接进入到container容器中
-t:tty,伪终端
-h wadeson:指定为container设置hostname
-v /data:container内的/data为挂载点
使用 docker inspect 82202acb4588 查看挂载信息

在host主机中创建文件


将本机host的/root目录挂载到容器中的/root目录
[root@VM_82_228_centos ~]# docker run -it -v /root/:/root -h shuju2 docker.io/centos
[root@shuju2 /]# ls /root/
centos7.tar
如果需要将目录挂载在container中只读:
docker run -it -v /root:/root:ro -h shuju3 docker.io/centos
从一个指定的容器选择挂载点
相当于两个container共同使用本机host的挂载点

[root@docker ~]# docker run -it --volumes-from b8c05a805280 -h wadeson saltstack/ubuntu-14.04
root@wadeson:/# ls /data/
root@wadeson:/#
参考:http://www.cnblogs.com/jsonhc/p/7760144.html
网友评论