Docker入門

目錄

一砚亭、Docker介紹
二宠蚂、安裝Docker
三、鏡像管理
四势木、通過容器創(chuàng)建鏡像
五戳表、通過模板創(chuàng)建鏡像
六桶至、容器管理
七、倉庫管理

一匾旭、Docke介紹

  • Docke簡介

官網(wǎng) www.docker.com
github https://github.com/docker/docker.github.io
開源的容器引擎镣屹,可以讓開發(fā)者打包應(yīng)用以及依賴的庫,然后發(fā)布到任何流行的linux發(fā)行版上价涝,移植很方便
由go語言編寫女蜈,基于apache2.0協(xié)議發(fā)布
基于linux kernel,要想在win下運(yùn)行需要借助一個(gè)vm(虛擬機(jī))來實(shí)現(xiàn)
自2013年開始色瘩,近些年發(fā)展迅猛
docker從1.13x開始伪窖,版本分為社區(qū)版ce和企業(yè)版ee,并且基于年月的時(shí)間線形式居兆,當(dāng)前最新穩(wěn)定版為17.09 參考http://blog.csdn.net/chenhaifeng2016/article/details/68062414

  • Docker和傳統(tǒng)的虛擬化比較
image.png
image.png
  • Docker的優(yōu)勢(shì)

啟動(dòng)非掣采剑快,秒級(jí)實(shí)現(xiàn)
資源利用率高泥栖,一臺(tái)高配置服務(wù)器可以跑上千個(gè)docker容器
更快的交付和部署簇宽,一次創(chuàng)建和配置后,可以在任意地方運(yùn)行
內(nèi)核級(jí)別的虛擬化吧享,不需要額外的hypevisor支持魏割,會(huì)有更高的性能和效率
易遷移,平臺(tái)依賴性不強(qiáng)


image.png
  • Docker核心概念

鏡像钢颂,是一個(gè)只讀的模板钞它,類似于安裝系統(tǒng)用到的那個(gè)iso文件,我們通過鏡像來完成各種應(yīng)用的部署。
容器须揣,鏡像類似于操作系統(tǒng)盐股,而容器類似于虛擬機(jī)本身。它可以被啟動(dòng)耻卡、開始疯汁、停止、刪除等操作卵酪,每個(gè)容器都是相互隔離的幌蚊。
倉庫,存放鏡像的一個(gè)場(chǎng)所溃卡,倉庫分為公開倉庫和私有倉庫溢豆。 最大的公開倉庫是Docker hub(hub.docker.com),國內(nèi)公開倉庫(dockerpool.com)

二瘸羡、安裝Docker

curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo
yum install -y docker-ce
速度比較慢漩仙,可以直接下載rpm包
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/
下載完,上傳到linux下
也需要用yum安裝犹赖,可以自動(dòng)解決依賴關(guān)系
yum install -y docker-ce-xxxx.rpm
systemctl start docker 啟動(dòng)docker

#下載官方y(tǒng)um源
[root@minglinux-01 ~] curl https://download.docker.com/linux/centos/docker-ce.repo -o  /etc/yum.repos.d/docker.repo
[root@minglinux-01 ~] yum list |grep docker
cockpit-docker.x86_64                   176-4.el7.centos               extras   
containerd.io.x86_64                    1.2.4-3.1.el7                  docker-ce-stable
docker.x86_64                           2:1.13.1-91.git07f3374.el7.centos
docker-ce.x86_64                        3:18.09.3-3.el7                docker-ce-stable
docker-ce-cli.x86_64                    1:18.09.3-3.el7                docker-ce-stable
docker-ce-selinux.noarch                17.03.3.ce-1.el7               docker-ce-stable
docker-client.x86_64                    2:1.13.1-91.git07f3374.el7.centos
docker-client-latest.x86_64             1.13.1-58.git87f2fab.el7.centos
docker-common.x86_64                    2:1.13.1-91.git07f3374.el7.centos
docker-compose.noarch                   1.18.0-2.el7                   epel     
docker-distribution.x86_64              2.6.2-2.git48294d9.el7         extras   
docker-latest.x86_64                    1.13.1-58.git87f2fab.el7.centos
docker-latest-logrotate.x86_64          1.13.1-58.git87f2fab.el7.centos
docker-latest-v1.10-migrator.x86_64     1.13.1-58.git87f2fab.el7.centos
docker-logrotate.x86_64                 2:1.13.1-91.git07f3374.el7.centos
docker-lvm-plugin.x86_64                2:1.13.1-91.git07f3374.el7.centos
docker-novolume-plugin.x86_64           2:1.13.1-91.git07f3374.el7.centos
docker-registry.x86_64                  0.9.1-7.el7                    extras   
docker-v1.10-migrator.x86_64            2:1.13.1-91.git07f3374.el7.centos
golang-github-fsouza-go-dockerclient-devel.x86_64
kdocker.x86_64                          4.9-1.el7                      epel     
pcp-pmda-docker.x86_64                  4.1.0-5.el7_6                  updates  
podman-docker.noarch                    0.12.1.2-2.git9551f6b.el7.centos
python-docker-py.noarch                 1:1.10.6-8.el7_6               extras   
python-docker-pycreds.noarch            1:0.3.0-8.el7_6                extras   
python-docker-scripts.noarch            0.4.4-1.el7                    epel     
python-dockerfile-parse.noarch          0.0.5-1.el7                    epel     
python2-avocado-plugins-runner-docker.noarch
python2-dockerpty.noarch                0.4.1-9.el7                    epel     
python34-docker.noarch                  2.6.1-1.el7                    epel     
python34-docker-pycreds.noarch          0.2.1-1.el7                    epel     
python34-dockerpty.noarch               0.4.1-9.el7                    epel     

#yum安裝
[root@minglinux-01 ~] yum install -y docker-ce

#啟動(dòng)
[root@minglinux-01 ~] ps aux |grep docker
root       5212  3.3  2.8 354916 52888 ?        Ssl  22:10   0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root       5357  0.0  0.0 112720   984 pts/0    S+   22:10   0:00 grep --color=auto docker

#看一下docker幫我們生成的iptables規(guī)則队他,每次啟動(dòng)都會(huì)自動(dòng)生成,但不自動(dòng)保存
[root@minglinux-01 ~] iptables -nvL
Chain INPUT (policy ACCEPT 148 packets, 11168 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DOCKER-USER  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 DOCKER-ISOLATION-STAGE-1  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  *      docker0  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    0     0 DOCKER     all  --  *      docker0  0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  docker0 !docker0  0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  docker0 docker0  0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy ACCEPT 179 packets, 14932 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain DOCKER (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DOCKER-ISOLATION-STAGE-2  all  --  docker0 !docker0  0.0.0.0/0            0.0.0.0/0           
    0     0 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain DOCKER-ISOLATION-STAGE-2 (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      docker0  0.0.0.0/0            0.0.0.0/0           
    0     0 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain DOCKER-USER (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0           

[root@minglinux-01 ~] cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.21 on Fri Dec 21 17:02:16 2018
*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [1:172]
:POSTROUTING ACCEPT [1:172]
-A PREROUTING -d 192.168.162.130/32 -p tcp -m tcp --dport 1122 -j DNAT --to-destination 192.168.100.100:22
-A POSTROUTING -s 192.168.100.100/32 -j SNAT --to-source 192.168.162.130
COMMIT
# Completed on Fri Dec 21 17:02:16 2018
# Generated by iptables-save v1.4.21 on Fri Dec 21 17:02:16 2018
*filter
:INPUT ACCEPT [56:3696]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [31:2992]
COMMIT
# Completed on Fri Dec 21 17:02:16 2018

#可以保存規(guī)則
[root@minglinux-01 ~] service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  確定  ]

#規(guī)則清空后再重啟docker就會(huì)恢復(fù)
[root@minglinux-01 ~] iptables -t nat -F
[root@minglinux-01 ~] iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 7 packets, 420 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 7 packets, 420 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain DOCKER (0 references)
 pkts bytes target     prot opt in     out     source               destination         
[root@minglinux-01 ~] systemctl restart docker
[root@minglinux-01 ~] iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DOCKER     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 162 packets, 9720 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DOCKER     all  --  *      *       0.0.0.0/0           !127.0.0.0/8          ADDRTYPE match dst-type LOCAL

Chain POSTROUTING (policy ACCEPT 162 packets, 9720 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 MASQUERADE  all  --  *      !docker0  172.17.0.0/16        0.0.0.0/0           

Chain DOCKER (2 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 RETURN     all  --  docker0 *       0.0.0.0/0            0.0.0.0/0           
#一般docker服務(wù)器的iptables規(guī)則不要去動(dòng)

三峻村、鏡像管理

docker pull centos//可以下載centos鏡像麸折,速度很慢
配置docker加速器(參考 http://blog.csdn.net/xlemonok/article/details/71403534
vi /etc/docker/daemon.json//加入如下內(nèi)容
{
"registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"]
}
說明:這個(gè)url為加速器地址,需要同學(xué)們自行到阿里云申請(qǐng)
配置完加速器粘昨,重啟docker服務(wù)垢啼,再次docker pull centos會(huì)快很多
docker images 查看本地的鏡像
docker search xxx //搜索鏡像,其中xxx是關(guān)鍵詞
docker tag centos aming123 //給鏡像打標(biāo)簽
docker run -itd centos //把鏡像啟動(dòng)為容器张肾,-i表示讓容器的標(biāo)準(zhǔn)輸入打開芭析,-t表示分配一個(gè)偽終端,-d表示后臺(tái)啟動(dòng)捌浩,要把-i -t -d 放到鏡像名字前面
docker ps //查看運(yùn)行的容器放刨,加上-a選項(xiàng)后可以查看所有容器,包括未運(yùn)行的
docker rmi centos //用來刪除指定鏡像尸饺, 其中后面的參數(shù)可以是tag,如果是tag時(shí)助币,實(shí)際上是刪除該tag浪听。當(dāng)后面的參數(shù)為鏡像ID時(shí),則會(huì)徹底刪除整個(gè)鏡像眉菱,所有標(biāo)簽也會(huì)一同刪除

#下載centos鏡像迹栓,下載慢的話可以配置加速器,重啟docker服務(wù)再次pull
[root@minglinux-01 ~] docker pull centos
Using default tag: latest
latest: Pulling from library/centos
8ba884070f61: Pull complete 
Digest: sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861
Status: Downloaded newer image for centos:latest

#查看本地鏡像
[root@minglinux-01 ~] docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              9f38484d220f        18 hours ago        202MB

#搜索鏡像
[root@minglinux-01 ~] docker search nginx
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                                                  Official build of Nginx.                        11064               [OK]                
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker con…   1558                                    [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable of…   689                                     [OK]
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as p…   492                                     [OK]
webdevops/php-nginx                                    Nginx with PHP-FPM                              123                                     [OK]
kitematic/hello-world-nginx                            A light-weight nginx container that demonstr…   123                                     
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   91                                      [OK]
bitnami/nginx                                          Bitnami nginx Docker Image                      64                                      [OK]
linuxserver/nginx                                      An Nginx container, brought to you by LinuxS…   56                                      
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          49                                      [OK]
tobi312/rpi-nginx                                      NGINX on Raspberry Pi / armhf                   24                                      [OK]
nginx/nginx-ingress                                    NGINX Ingress Controller for Kubernetes         17                                      
nginxdemos/hello                                       NGINX webserver that serves a simple page co…   12                                      [OK]
wodby/drupal-nginx                                     Nginx for Drupal container image                12                                      [OK]
blacklabelops/nginx                                    Dockerized Nginx Reverse Proxy Server.          12                                      [OK]
schmunk42/nginx-redirect                               A very simple container to redirect HTTP tra…   11                                      [OK]
centos/nginx-18-centos7                                Platform for running nginx 1.8 or building n…   10                                      
centos/nginx-112-centos7                               Platform for running nginx 1.12 or building …   7                                       
nginxinc/nginx-unprivileged                            Unprivileged NGINX Dockerfiles                  4                                       
1science/nginx                                         Nginx Docker images that include Consul Temp…   4                                       [OK]
mailu/nginx                                            Mailu nginx frontend                            3                                       [OK]
travix/nginx                                           NGinx reverse proxy                             2                                       [OK]
toccoag/openshift-nginx                                Nginx reverse proxy for Nice running on same…   1                                       [OK]
wodby/nginx                                            Generic nginx                                   0                                       [OK]
ansibleplaybookbundle/nginx-apb                        An APB to deploy NGINX                          0                                       [OK]

[root@minglinux-01 ~] docker tag centos lucci_linux  #給鏡像打標(biāo)簽
[root@minglinux-01 ~] docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              9f38484d220f        18 hours ago        202MB
lucci_linux         latest              9f38484d220f        18 hours ago        202MB
ubuntu              latest              94e814e2efa8        3 days ago          88.9MB
[root@minglinux-01 ~] docker tag centos test1:123
[root@minglinux-01 ~] docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              9f38484d220f        18 hours ago        202MB
lucci_linux         latest              9f38484d220f        18 hours ago        202MB
test1               123                 9f38484d220f        18 hours ago        202MB
ubuntu              latest              94e814e2efa8        3 days ago          88.9MB

#把鏡像啟動(dòng)為容器
[root@minglinux-01 ~] docker run -itd centos 
b5a7977de9e319b65e605c53e1e9ecee26d0d2871e4e7c2b863c4c173874501c
[root@minglinux-01 ~] docker ps  #查看運(yùn)行的容器俭缓,加-a查看全部包括啟動(dòng)的和停止的
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
b5a7977de9e3        centos              "/bin/bash"         11 seconds ago      Up 10 seconds                           naughty_mclean

#刪除指定鏡像
[root@minglinux-01 ~] docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              9f38484d220f        18 hours ago        202MB
lucci_linux         latest              9f38484d220f        18 hours ago        202MB
test1               123                 9f38484d220f        18 hours ago        202MB
ubuntu              latest              94e814e2efa8        3 days ago          88.9MB
[root@minglinux-01 ~] docker rmi test1:123  #相當(dāng)于刪除了一個(gè)tag 克伊,真鏡像是centos
Untagged: test1:123
[root@minglinux-01 ~] docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              9f38484d220f        18 hours ago        202MB
lucci_linux         latest              9f38484d220f        18 hours ago        202MB
ubuntu              latest              94e814e2efa8        3 days ago          88.9MB

四酥郭、通過容器創(chuàng)建鏡像

docker run啟動(dòng)容器后,可以通過下面命令進(jìn)入容器
docker exec -it xxxxx bash//其中xxxxx為容器id愿吹,這個(gè)id可以用docker ps查看不从,最后面的bash為進(jìn)入容器后我們要執(zhí)行的命令,這樣就可以打開一個(gè)終端
進(jìn)入到該容器中犁跪,我們做一些變更椿息,比如安裝一些東西,然后針對(duì)這個(gè)容器進(jìn)行創(chuàng)建新的鏡像
在容器中執(zhí)行 yum install -y net-tools坷衍,然后ctrl d退出容器
docker commit -m "change somth" -a "somebody info" container_id new_image_name //container_id通過docker ps -a獲取寝优,后面的new_image_name為新鏡像名字
例如: docker commit -m "install net-tools" -a "ming" 2c74d574293f centos_with_nettool 這個(gè)命令有點(diǎn)像svn的提交,-m 加一些改動(dòng)信息枫耳,-a 指定作者相關(guān)信息 2c74d這一串為容器id乏矾,再后面為新鏡像的名字

#根據(jù)個(gè)人需求創(chuàng)建自己的鏡像
#進(jìn)入容器并執(zhí)行bash開啟一個(gè)新終端
[root@minglinux-01 ~] docker exec -it b5a797 bash
[root@b5a7977de9e3 /]# 
[root@b5a7977de9e3 /]# ls
anaconda-post.log  dev  home  lib64  mnt  proc  run   srv  tmp  var
bin                etc  lib   media  opt  root  sbin  sys  usr
[root@b5a7977de9e3 /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
overlay          28G  9.6G   19G  35% /
tmpfs            64M     0   64M   0% /dev
tmpfs           911M     0  911M   0% /sys/fs/cgroup
/dev/sda3        28G  9.6G   19G  35% /etc/hosts
shm              64M     0   64M   0% /dev/shm
tmpfs           911M     0  911M   0% /proc/asound
tmpfs           911M     0  911M   0% /proc/acpi
tmpfs           911M     0  911M   0% /proc/scsi
tmpfs           911M     0  911M   0% /sys/firmware
[root@b5a7977de9e3 /]# free
              total        used        free      shared  buff/cache   available
Mem:        1865276      751940      161204        9904      952132      855116
Swap:       2097148           0     2097148


#容器就像是被閹割過的系統(tǒng),有很多東西是沒有的
[root@b5a7977de9e3 /]# ifconfig
bash: ifconfig: command not found
[root@b5a7977de9e3 /]# ip addr
bash: ip: command not found
[root@b5a7977de9e3 /]# yum install -y net-tools  #安裝net-tools
[root@b5a7977de9e3 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 2135  bytes 9667371 (9.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2138  bytes 119365 (116.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
# 將容器做成鏡像
[root@minglinux-01 ~] docker commit -m "install net-tools" -a "lucci" b5a7977de9e3 centos_with_net
sha256:9655a540a99e8a775cf5e8654e622373d9f3543138706db42dcfad8e350c5a08
[root@minglinux-01 ~] docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos_with_net     latest              9655a540a99e        9 seconds ago       280MB
centos              latest              9f38484d220f        19 hours ago        202MB
lucci_linux         latest              9f38484d220f        19 hours ago        202MB
ubuntu              latest              94e814e2efa8        3 days ago          88.9MB

#將新建鏡像啟動(dòng)為容器并進(jìn)入
[root@minglinux-01 ~] docker run -itd centos_with_net
3da5abccfd8d85b9ca96ac0829f10dff3c5b16a40802455a326bdbd3bdc52788
[root@minglinux-01 ~] docker exec -it 3da5a bash  #也可以使用docker ps中的NAMES來啟動(dòng)
[root@3da5abccfd8d /]# 
[root@3da5abccfd8d /]# ifconfig  #ip變0.3了
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 15  bytes 1242 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7  bytes 530 (530.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

#然后推到宿主機(jī)查看網(wǎng)卡信息發(fā)現(xiàn)有多了一個(gè)網(wǎng)卡
[root@3da5abccfd8d /]# exit
[root@minglinux-01 ~] ifconfig 
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:38ff:fe57:43ae  prefixlen 64  scopeid 0x20<link>
        ether 02:42:38:57:43:ae  txqueuelen 0  (Ethernet)
        RX packets 2147  bytes 89933 (87.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2135  bytes 9667359 (9.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.162.130  netmask 255.255.255.0  broadcast 192.168.162.255
        inet6 fe80::20c:29ff:fede:c112  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:de:c1:12  txqueuelen 1000  (Ethernet)
        RX packets 158154  bytes 196994901 (187.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 54587  bytes 3737463 (3.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 6790  bytes 597408 (583.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6790  bytes 597408 (583.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth4a0a7c8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::3cd5:94ff:fe00:4cf6  prefixlen 64  scopeid 0x20<link>
        ether 3e:d5:94:00:4c:f6  txqueuelen 0  (Ethernet)
        RX packets 2140  bytes 119461 (116.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2137  bytes 9667455 (9.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth55a4193: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::c008:cfff:fea0:53b8  prefixlen 64  scopeid 0x20<link>
        ether c2:08:cf:a0:53:b8  txqueuelen 0  (Ethernet)
        RX packets 7  bytes 530 (530.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 15  bytes 1242 (1.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

五迁杨、通過模板創(chuàng)建鏡像

首先去下載一個(gè)模板
http://openvz.org/Download/templates/precreated //下載一個(gè)centos6的模板centos-6-x86-minimal.tar.gz
導(dǎo)入該鏡像的命令為:
cat centos-6-x86-minimal.tar.gz|docker import - centos6
docker images查看導(dǎo)入的鏡像
把現(xiàn)有鏡像妻熊,導(dǎo)出為一個(gè)文件:
docker save -o ming-centos.tar ming
我們還可以用該文件恢復(fù)本地鏡像:
docker load --input ming-centos.tar 或者
docker load < ming-centos.tar
docker push image_name //可以把自己的鏡像傳到dockerhub官方網(wǎng)站上去,但前提是需要先注冊(cè)一個(gè)用戶仑最,后續(xù)如果有需求再研究吧

#使用rz命令將下載好的模板上傳到虛擬機(jī)
[root@minglinux-01 ~] rz
[root@minglinux-01 ~] ls centos-6-x86-minimal.tar.gz 
centos-6-x86-minimal.tar.gz
[root@minglinux-01 ~] du -h centos-6-x86-minimal.tar.gz 
201M    centos-6-x86-minimal.tar.gz

#導(dǎo)入該鏡像到docker
[root@minglinux-01 ~] cat centos-6-x86-minimal.tar.gz|docker import - centos6
sha256:28927c522726ae66450cc0de22216e8dc8aa78ace2eaa15533ed283c481784fe
[root@minglinux-01 ~] docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              28927c522726        37 seconds ago      512MB
centos_with_net     latest              9655a540a99e        About an hour ago   280MB
centos              latest              9f38484d220f        20 hours ago        202MB
lucci_linux         latest              9f38484d220f        20 hours ago        202MB
ubuntu              latest              94e814e2efa8        3 days ago          88.9MB

#啟動(dòng)鏡像扔役,進(jìn)入容器
[root@minglinux-01 ~] docker run -itd centos6 bash
13109a927747652594e794eeec79c1a97154519f468e4c12772920ac4797529c
[root@minglinux-01 ~] docker exec -it 13109a bash
[root@13109a927747 /]# 
[root@13109a927747 /]# cat /etc/issue
CentOS release 6.8 (Final)
Kernel \r on an \m

[root@13109a927747 /]# uname -a
Linux 13109a927747 3.10.0-862.11.6.el7.x86_64 #1 SMP Tue Aug 14 21:49:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@13109a927747 /]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:04  
          inet addr:172.17.0.4  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:648 (648.0 b)  TX bytes:0 (0.0 b)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

#把現(xiàn)有鏡像,導(dǎo)出為一個(gè)文件
[root@13109a927747 /]# exit 
[root@minglinux-01 ~] docker save -o centos7_withnettool.tar centos_with_net
[root@minglinux-01 ~] du -sh centos7_withnettool.tar 
275M    centos7_withnettool.tar

#用該文件恢復(fù)為本地鏡像警医,先刪除亿胸,后導(dǎo)入
[root@minglinux-01 ~] docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              28927c522726        11 minutes ago      512MB
centos_with_net     latest              9655a540a99e        2 hours ago         280MB
centos              latest              9f38484d220f        20 hours ago        202MB
lucci_linux         latest              9f38484d220f        20 hours ago        202MB
ubuntu              latest              94e814e2efa8        3 days ago          88.9MB
[root@minglinux-01 ~] docker rmi 9655a540a99e
Error response from daemon: conflict: unable to delete 9655a540a99e (cannot be forced) - image is being used by running container 3da5abccfd8d
[root@minglinux-01 ~] docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
13109a927747        centos6             "bash"              10 minutes ago      Up 10 minutes                           elated_kowalevski
3da5abccfd8d        centos_with_net     "/bin/bash"         2 hours ago         Up 2 hours                              infallible_yalow
b5a7977de9e3        centos              "/bin/bash"         2 hours ago         Up 2 hours                              naughty_mclean
[root@minglinux-01 ~] docker rm -f 3da5abccfd8d
3da5abccfd8d
[root@minglinux-01 ~] docker rmi 9655a540a99e
Untagged: centos_with_net:latest
Deleted: sha256:9655a540a99e8a775cf5e8654e622373d9f3543138706db42dcfad8e350c5a08
Deleted: sha256:74b73c8cc0bca12cac68112602778d255d2e34f4d879a54a69abe7bec658bfa8
[root@minglinux-01 ~] docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              28927c522726        15 minutes ago      512MB
centos              latest              9f38484d220f        20 hours ago        202MB
lucci_linux         latest              9f38484d220f        20 hours ago        202MB
ubuntu              latest              94e814e2efa8        3 days ago          88.9MB

#從文件導(dǎo)入恢復(fù)鏡像
[root@minglinux-01 ~] docker load < centos7_withnettool.tar 
66c1740fd4c5: Loading layer  78.01MB/78.01MB
Loaded image: centos_with_net:latest
[root@minglinux-01 ~] docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              28927c522726        16 minutes ago      512MB
centos_with_net     latest              9655a540a99e        2 hours ago         280MB
centos              latest              9f38484d220f        20 hours ago        202MB
lucci_linux         latest              9f38484d220f        20 hours ago        202MB
ubuntu              latest              94e814e2efa8        3 days ago          88.9MB

六、容器管理

docker create -it centos6 bash //這樣可以創(chuàng)建一個(gè)容器预皇,但該容器并沒有啟動(dòng)
docker start container_id //啟動(dòng)容器后侈玄,可以使用 docker ps 查看到,有start 就有stop吟温,和restart
之前我們使用的docker run 相當(dāng)于先create再start
docker run -it centos bash
這樣進(jìn)入了一個(gè)虛擬終端里面序仙,我們可以運(yùn)行一些命令,使用命令exit或者ctrl d 退出該bash鲁豪,當(dāng)退出后這個(gè)容器也會(huì)停止潘悼。
docker run -d 可以讓容器在后臺(tái)運(yùn)行
比如:docker run -d centos bash -c "while :; do echo "123"; sleep 2; done"
docker run -itd --name centos6_1 centos6 bash // --name 給容器自定義名字
docker run --rm -it centos bash -c "sleep 30" //--rm 可以讓容器退出后
直接刪除,在這里命令執(zhí)行完容器就會(huì)退出

[root@minglinux-01 ~] docker create -it centos6 bash  ##創(chuàng)建容器爬橡,未啟動(dòng)
9d87507b2fcd81fe46a97d4ef85b5322e00ba21c4a9e95b25fdfec946153953f
[root@minglinux-01 ~] docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@minglinux-01 ~] docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
9d87507b2fcd        centos6             "bash"              14 seconds ago      Created                                         adoring_perlman
13109a927747        centos6             "bash"              15 hours ago        Exited (137) 15 hours ago                       elated_kowalevski
b5a7977de9e3        centos              "/bin/bash"         17 hours ago        Exited (137) 15 hours ago                       naughty_mclean
[root@minglinux-01 ~] docker start 9d87507b2fcd  #啟動(dòng)鏡像
9d87507b2fcd
[root@minglinux-01 ~] docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9d87507b2fcd        centos6             "bash"              2 minutes ago       Up 3 seconds                            adoring_perlman

#不加-d啟動(dòng)centos鏡像
[root@minglinux-01 ~] docker run -it centos bash 
[root@a74baaff6908 /]# ifconfig
bash: ifconfig: command not found
[root@a74baaff6908 /]# exit
exit
[root@minglinux-01 ~] docker ps  #僅有上一個(gè)9d875容器在運(yùn)行
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9d87507b2fcd        centos6             "bash"              7 minutes ago       Up 4 minutes                            adoring_perlman
[root@minglinux-01 ~] docker ps -a  #退出該bash后這個(gè)容器就會(huì)停止治唤,啟動(dòng)時(shí)加-d則容器會(huì)繼續(xù)在后臺(tái)運(yùn)行
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                        PORTS               NAMES
a74baaff6908        centos              "bash"              About a minute ago   Exited (127) 30 seconds ago                       pensive_mcclintock
9d87507b2fcd        centos6             "bash"              8 minutes ago        Up 5 minutes                                      adoring_perlman
13109a927747        centos6             "bash"              15 hours ago         Exited (137) 15 hours ago                         elated_kowalevski
b5a7977de9e3        centos              "/bin/bash"         17 hours ago         Exited (137) 15 hours ago                         naughty_mclean

#啟動(dòng)時(shí)給容器及自定義名字
[root@minglinux-01 ~] docker run -itd --name centos6_1 centos6 bash
9ac1becfb0018f06f41e79f0303a6f349d3c1055e33edb69b73badcda3359169
[root@minglinux-01 ~] docker exec -it centos6_1 bash #使用自定義名字進(jìn)入容器開啟終端,比使用容器id更舒服
[root@9ac1becfb001 /]# 
[root@9ac1becfb001 /]# exit


#運(yùn)行容器然后讓容器退出后直接刪除糙申,好像沒什么用
[root@minglinux-01 ~] docker run --rm -it centos bash -c "sleep 30"

[root@minglinux-01 ~] docker ps  #命令執(zhí)行過程中centos鏡像啟動(dòng)
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
598e42c7d359        centos              "bash -c 'sleep 30'"   3 seconds ago       Up 3 seconds                            loving_ramanujan
9ac1becfb001        centos6             "bash"                 23 minutes ago      Up 23 minutes                           centos6_1
9d87507b2fcd        centos6             "bash"                 About an hour ago   Up About an hour                        adoring_perlman

[root@minglinux-01 ~] docker ps  -a#30s過后命令執(zhí)行完容器退出并刪除宾添,所以這里沒有記錄
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES
9ac1becfb001        centos6             "bash"                   27 minutes ago      Up 27 minutes                                        centos6_1
848c8a6e82f7        centos              "bash"                   32 minutes ago      Exited (0) 32 minutes ago                            jolly_kepler
712acdc9dea5        centos              "bash"                   33 minutes ago      Exited (0) 32 minutes ago                            silly_spence
84c9aa678fbb        centos              "bash -c 'while :; d…"   About an hour ago   Exited (137) 44 minutes ago                          recursing_allen
a74baaff6908        centos              "bash"                   About an hour ago   Exited (127) About an hour ago                       pensive_mcclintock
9d87507b2fcd        centos6             "bash"                   About an hour ago   Up About an hour                                     adoring_perlman
13109a927747        centos6             "bash"                   16 hours ago        Exited (137) 16 hours ago                            elated_kowalevski
b5a7977de9e3        centos              "/bin/bash"              18 hours ago        Exited (137) 16 hours ago                            naughty_mclean

docker logs 可以獲取到容器的運(yùn)行歷史信息,用法如下
docker logs container_id
docker attach 可以進(jìn)入一個(gè)后臺(tái)運(yùn)行的容器,比如
docker attach container_id //但是attach命令不算好用缕陕,比如我們想要退出終端粱锐,就得exit了,這樣容器也就退出了扛邑,還有一種方法
docker exec -it container_id bash //可以臨時(shí)打開一個(gè)虛擬終端怜浅,并且exit后,容器依然運(yùn)行著
docker rm container_id //container_id是ps的時(shí)候查看到的鹿榜,這樣就可以把container刪除海雪,如果是運(yùn)行的容器,可以加-f
docker export container_id > file.tar // 導(dǎo)出容器舱殿,可以遷移到其他機(jī)器上奥裸,需要導(dǎo)入
cat file.tar |docker import - test //這樣會(huì)生成test的鏡像

#查看容器的運(yùn)行歷史信息
[root@minglinux-01 ~] docker run -itd  centos bash -c "echo hello world"
815faf4812be06de33c838696dab641cac552f1237e2fddb8e0ae3125bae20ef
[root@minglinux-01 ~] docker logs 815faf4
hello world

#進(jìn)入一個(gè)后臺(tái)運(yùn)行的容器方法1(exit后容器也一起退出)
[root@minglinux-01 ~] docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9ac1becfb001        centos6             "bash"              34 minutes ago      Up 34 minutes                           centos6_1
9d87507b2fcd        centos6             "bash"              About an hour ago   Up About an hour                        adoring_perlman
[root@minglinux-01 ~] docker attach 9ac1becfb001
[root@9ac1becfb001 /]# exit
exit
[root@minglinux-01 ~] docker ps   #這個(gè)方法exit后容器也一起退出了
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9d87507b2fcd        centos6             "bash"              About an hour ago   Up About an hour                        adoring_perlman

#進(jìn)入一個(gè)后臺(tái)運(yùn)行的容器方法1(exit后容器不退出)
[root@minglinux-01 ~] docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9d87507b2fcd        centos6             "bash"              About an hour ago   Up About an hour                        adoring_perlman
[root@minglinux-01 ~] docker exec -it 9d87507b2fcd bash
[root@9d87507b2fcd /]# exit
exit
[root@minglinux-01 ~] docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9d87507b2fcd        centos6             "bash"              About an hour ago   Up About an hour                        adoring_perlman

#刪除所有未運(yùn)行容器
[root@minglinux-01 ~] docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
815faf4812be        centos              "bash -c 'echo hello…"   4 hours ago         Exited (0) 4 hours ago                          stupefied_hypatia
9ac1becfb001        centos6             "bash"                   4 hours ago         Exited (0) 4 hours ago                          centos6_1
848c8a6e82f7        centos              "bash"                   4 hours ago         Exited (0) 4 hours ago                          jolly_kepler
712acdc9dea5        centos              "bash"                   4 hours ago         Exited (0) 4 hours ago                          silly_spence
84c9aa678fbb        centos              "bash -c 'while :; d…"   5 hours ago         Exited (137) 5 hours ago                        recursing_allen
a74baaff6908        centos              "bash"                   5 hours ago         Exited (127) 5 hours ago                        pensive_mcclintock
9d87507b2fcd        centos6             "bash"                   5 hours ago         Up 5 hours                                      adoring_perlman
13109a927747        centos6             "bash"                   20 hours ago        Exited (137) 20 hours ago                       elated_kowalevski
b5a7977de9e3        centos              "/bin/bash"              22 hours ago        Exited (137) 20 hours ago                       naughty_mclean
[root@minglinux-01 ~] docker rm 815faf4812be
815faf4812be
[root@minglinux-01 ~] docker rm 9ac1becfb001
9ac1becfb001
···
···
[root@minglinux-01 ~] docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9d87507b2fcd        centos6             "bash"              5 hours ago         Up 5 hours                              adoring_perlman

#刪除正在運(yùn)行的容器需要加-f
[root@minglinux-01 ~] docker rm 9d87507b2fcd
Error response from daemon: You cannot remove a running container 9d87507b2fcd81fe46a97d4ef85b5322e00ba21c4a9e95b25fdfec946153953f. Stop the container before attempting removal or force remove
[root@minglinux-01 ~] docker rm -f 9d87507b2fcd
9d87507b2fcd
[root@minglinux-01 ~] docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

#導(dǎo)出容器,可以遷移到其他機(jī)器上再導(dǎo)入
#使用export和import

七沪袭、倉庫管理

docker pull registry //下載registry 鏡像湾宙,registy為docker官方提供的一個(gè)鏡像,我們可以用它來創(chuàng)建本地的docker私有倉庫冈绊。
docker run -d -p 5000:5000 registry //以registry鏡像啟動(dòng)容器侠鳄,-p會(huì)把容器的端口映射到宿主機(jī)上,:左邊為宿主機(jī)監(jiān)聽端口死宣,:右邊為容器監(jiān)聽端口
curl 127.0.0.1:5000/v2/_catalog //可以訪問它
下面我們來把其中一個(gè)鏡像上傳到私有倉庫
docker tag aming_test 192.168.162.130:5000/centos //標(biāo)記一下tag伟恶,必須要帶有私有倉庫的ip:port
docker push 192.168.162.130:5000/centos //把標(biāo)記的鏡像給推送到私有倉庫
此時(shí)并不會(huì)成功,Get https://192.168.162.130:5000/v2/: http: server gave HTTP response to HTTPS client
更改配置文件毅该,vi /etc/docker/daemon.json//更改為
{ "insecure-registries":["192.168.162.130:5000"] }
systemctl restart docker
docker ps -a //查看容器已經(jīng)關(guān)閉博秫,還需要啟動(dòng)
docker start id //這里的id為registry容器id
再次push
docker push 192.168.162.130:5000/centos
curl 127.0.0.1:5001/v2/_catalog //可以查看到推送上來的鏡像

#下載registry 鏡像
[root@minglinux-01 ~] docker pull registry 
[root@minglinux-01 ~] docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              28927c522726        20 hours ago        512MB
centos_with_net     latest              9655a540a99e        22 hours ago        280MB
centos              latest              9f38484d220f        41 hours ago        202MB
lucci_linux         latest              9f38484d220f        41 hours ago        202MB
ubuntu              latest              94e814e2efa8        4 days ago          88.9MB
registry            latest              f32a97de94e1        8 days ago          25.8MB

#以registry鏡像啟動(dòng)容器 
[root@minglinux-01 ~] docker run -d -p 5000:5000 registry  #-p選項(xiàng)會(huì)把容器的端口映射到宿主機(jī)上,:左邊為宿主機(jī)監(jiān)聽端口眶掌,:右邊為容器監(jiān)聽端口
ef8c00af1fd7c29770631105745cb14e90ba9482c507fcf96e35bb3cf3f246d2
[root@minglinux-01 ~] docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
ef8c00af1fd7        registry            "/entrypoint.sh /etc…"   9 seconds ago       Up 8 seconds        0.0.0.0:5000->5000/tcp   awesome_heyrovsky

#測(cè)試5000端口聯(lián)通性
[root@minglinux-01 ~] netstat -lntp | grep 5000
tcp6       0      0 :::5000                 :::*                    LISTEN      36966/docker-proxy  
[root@minglinux-01 ~] telnet 127.0.0.1 5000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
^]
telnet> quit
Connection closed.

#訪問倉庫
[root@minglinux-01 ~] curl 127.0.0.1:5000/v2/_catalog
{"repositories":[]}  #現(xiàn)在是空的

#上傳鏡像到私有倉庫
[root@minglinux-01 ~] docker tag centos6 192.168.162.130:5000/centos6  #標(biāo)記一下tag挡育,必須要帶有私有倉庫的ip:port
[root@minglinux-01 ~] docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
centos6                        latest              28927c522726        21 hours ago        512MB
192.168.162.130:5000/centos6   latest              28927c522726        21 hours ago        512MB
centos_with_net                latest              9655a540a99e        22 hours ago        280MB
centos                         latest              9f38484d220f        41 hours ago        202MB
lucci_linux                    latest              9f38484d220f        41 hours ago        202MB
ubuntu                         latest              94e814e2efa8        4 days ago          88.9MB
registry                       latest              f32a97de94e1        8 days ago          25.8MB
[root@minglinux-01 ~] docker push 192.168.162.130:5000/centos6
The push refers to repository [192.168.162.130:5000/centos6]
Get https://192.168.162.130:5000/v2/: http: server gave HTTP response to HTTPS client

#修改daemon.json文件指定私有倉庫的地址
[root@minglinux-01 ~] vim /etc/docker/daemon.json
  1 { "insecure-registries":["192.168.162.130:5000"] }

#重啟操作
[root@minglinux-01 ~] systemctl restart docker 
[root@minglinux-01 ~] docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
da9add7e5695        registry            "/entrypoint.sh /etc…"   19 minutes ago      Exited (2) 33 seconds ago                       jovial_sammet
ef8c00af1fd7        registry            "/entrypoint.sh /etc…"   29 minutes ago      Exited (2) 20 minutes ago                       awesome_heyrovsky
[root@minglinux-01 ~] docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@minglinux-01 ~] docker start da9add7e5695 
da9add7e5695

#再次push
[root@minglinux-01 ~] docker push 192.168.162.130:5000/centos6
The push refers to repository [192.168.162.130:5000/centos6]
0a2f11f7b1ef: Pushed 
latest: digest: sha256:3620c309f7a611a0f3a28af82ed0a7069e0307de6bee01defc93f483c48e93d4 size: 529
[root@minglinux-01 ~] curl 127.0.0.1:5000/v2/_catalog  #訪問私有倉庫
{"repositories":["centos6"]}  

#使用其他機(jī)器pull
[root@minglinux-02 ~]  curl https://download.docker.com/linux/centos/docker-ce.repo -o  /etc/yum.repos.d/docker.repo
[root@minglinux-02 ~] yum install -y docker-ce
[root@minglinux-02 ~] vim /etc/docker/daemon.json  #指定私有倉庫的地址
{ "insecure-registries":["192.168.162.130:5000"] }
[root@minglinux-02 ~] systemctl restart docker  #要重啟
[root@minglinux-02 ~] curl 192.168.162.130:5000/v2/_catalog
{"repositories":["centos6"]}
[root@minglinux-02 ~] docker pull 192.168.162.130:5000/centos6
Using default tag: latest
latest: Pulling from centos6
58cc42c750fa: Pull complete 
Digest: sha256:3620c309f7a611a0f3a28af82ed0a7069e0307de6bee01defc93f483c48e93d4
Status: Downloaded newer image for 192.168.162.130:5000/centos6:latest
[root@minglinux-02 ~] docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
192.168.162.130:5000/centos6   latest              28927c522726        30 hours ago        512MB
#pull后面直接加鏡像名仍是從官方倉庫下載,daemon.json改動(dòng)無影響
[root@minglinux-02 ~] docker pull docker

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末朴爬,一起剝皮案震驚了整個(gè)濱河市即寒,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌召噩,老刑警劉巖母赵,帶你破解...
    沈念sama閱讀 218,607評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異蚣常,居然都是意外死亡市咽,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門抵蚊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事贞绳」茸恚” “怎么了?”我有些...
    開封第一講書人閱讀 164,960評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵冈闭,是天一觀的道長俱尼。 經(jīng)常有香客問我,道長萎攒,這世上最難降的妖魔是什么遇八? 我笑而不...
    開封第一講書人閱讀 58,750評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮耍休,結(jié)果婚禮上刃永,老公的妹妹穿的比我還像新娘。我一直安慰自己羊精,他們只是感情好斯够,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著喧锦,像睡著了一般读规。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上燃少,一...
    開封第一講書人閱讀 51,604評(píng)論 1 305
  • 那天束亏,我揣著相機(jī)與錄音,去河邊找鬼阵具。 笑死碍遍,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的怔昨。 我是一名探鬼主播雀久,決...
    沈念sama閱讀 40,347評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼趁舀!你這毒婦竟也來了赖捌?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,253評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤矮烹,失蹤者是張志新(化名)和其女友劉穎越庇,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體奉狈,經(jīng)...
    沈念sama閱讀 45,702評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡卤唉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了仁期。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片桑驱。...
    茶點(diǎn)故事閱讀 40,015評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡竭恬,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出熬的,到底是詐尸還是另有隱情痊硕,我是刑警寧澤,帶...
    沈念sama閱讀 35,734評(píng)論 5 346
  • 正文 年R本政府宣布押框,位于F島的核電站岔绸,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏橡伞。R本人自食惡果不足惜盒揉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望兑徘。 院中可真熱鬧刚盈,春花似錦、人聲如沸道媚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽最域。三九已至谴分,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間镀脂,已是汗流浹背牺蹄。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評(píng)論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留薄翅,地道東北人沙兰。 一個(gè)月前我還...
    沈念sama閱讀 48,216評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像翘魄,于是被迫代替她去往敵國和親鼎天。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容