docker基本操作

docker pull registry   //下載registry 鏡像

docker run -d -p 5000:5000 registy   //以registry鏡像啓動容器,監聽5000端口

curl 127.0.0.1:5000  //可以訪問它

docker tag aming_test  172.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 start  registry_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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章