32121fff 发表于 2016-4-12 09:28:53

docker基本操作

docker pull registry   //下载registry 镜像
docker run -d -p 5000:5000 registy   //以registry镜像启动容器,监听5000端口
curl 127.0.0.1:5000//可以访问它
docker tag aming_test172.7.15.106:5000/centos //标记一下tag,必须要带有私有仓库的ip:port
docker push 172.7.15.106:5000/centos   //此时报错了类似如下
Error response from daemon: invalid registry endpoint https://172.7.15.106:5000/v0/: unable to ping registry endpoint https://172.7.15.106:5000/v0/
v2 ping attempt failed with error: Get https://172.7.15.106:5000/v2/: EOF
v1 ping attempt failed with error: Get https://172.7.15.106:5000/v1/_ping: EOF. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry 172.7.15.106:5000` to the daemon's arguments. In the case of HTTPS, if you have access to the registry's CA certificate, no need for the flag; simply place the CA certificate at /etc/docker/certs.d/172.7.15.106:5000/ca.crt


这是因为Docker从1.3.X之后,与docker registry交互默认使用的是https,然而此处搭建的私有仓库只提供http服务,所以当与私有仓库交互时就会报上面的错误。为了解决这个问题需要在启动docker server时增加启动参数为默认使用http访问。解决该问题的方法为:

vi /etc/init.d/docker

把 $exec -d $other_args 改为
$exec -d --insecure-registry 172.7.15.106:5000 $other_args


然后重启docker
service docker restart
再启动registry容器
docker startregistry_container_id

curl http://172.7.15.106:5000/v1/search   //可以查看私有仓库里面的所有镜像

挂载本地的目录到容器里
docker run -tid -v /data/:/data aming bash   ##-v 用来指定挂载目录,:前面的/data/为本地目录,:后面的/data/为容器里的目录
挂载数据卷
docker run -itd -v /data/--name centos_test centos bash
docker run -itd --volumes-from centos_test centos bash

数据卷的备份与恢复
备份
mkdir /vol_data_backup
docker run -itd --volumes-from centos_test -v /vol_data_backup/:/backup centos tar cvf /backup/data.tar /data/##把/data/目录下面的文件打包到成data.tar文件放到/backup目录下面
恢复
新建数据卷容器:
docker run -itd -v /data/--name centos_test centos bash
挂载数据卷新建容器:
docker run --vilunes-from centos_test -v /vol_data_backup/:/backup/ centos tar xvf /backup/data.tar

docker网络管理
host模式,使用docker run时使用--net=host指定docker使用的网络实际上和宿主机一样,在容器内看到的网卡ip是宿主机上的ip
container模式,使用--net=container:container_id/container_name多个容器使用共同的网络,看到的ip是一样的
none模式,使用--net=none指定这种模式下,不会配置任何网络
bridge模式,使用--net=bridge指定默认模式,不用指定默认就是这种网络模式。这种模式会为每个容器分配一个独立的Network Namespace。类似于vmware的nat网络模式。同一个宿主机上的所有容器会在同一个网段下,相互之间是可以通信的。


让外部网络访问容器资源
docker run -it centos_with_net:hc bash
进入容器后
yum install -y httpd
/usr/sbin/httpd
exit
docker commit -m "centos_with_httpd" -a "hc" 32ea4 centos_with_httpd:hc##将现有容器保存为镜像
docker run -itd -p 5123:80 centos_with_httpd:hc bash##-p 5123:80 宿主机的5123端映射到容器的80端
?vi /var/www/html/1.html
hclinux
curl localhost/1.html
exit
curl 192.168.220.144/1.html


页: [1]
查看完整版本: docker基本操作