S-Docker_02_基本概念_01_鏡像

1.鏡像:images  search pull run -t -i  commit  build  import  save  rmi load

1.1 列出鏡像

docker images  (列出本地已有鏡像列表)
REPOSITORY(所在倉庫)   TAG(標記)   IMAGE ID(唯一ID號)  CREATE(創建時間) VIRTUAL SIZE(鏡像大小)

1.2 搜索鏡像

docker search 鏡像名

1.3 下載鏡像 

docker pull 鏡像名   (默認在docker hub中下載) 
     docker pull IP/域名:5000/鏡像名
 dl.dockerpool.com:5000/ubuntu:12.04
 具體請參考http://dockerpool.com/

1.4 修改鏡像_01(docker commit)

1.4.1

鏡像默認使用的國外的的源,修改成國內的源,如morris.163.com
root@vmc01:~# docker  run -t -i ubuntu /bin/bash
root@f63998a68f59:/# apt-get update  (國外的可能比較慢)
    root@f63998a68f59:/# apt-get install vim
root@f63998a68f59:/# vim /etc/apt/sources.list
....加入源
root@f63998a68f59:/#apt-get update (改完之後就很快)
root@f63998a68f59:/#exit
root@vmc01:~# docker  commit -m "added 163 mirrors" -a "docker new" f63998a68f59 ubuntu:v2_163
root@vmc01:~# docker  images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              v2_163              8c525f0bec06        8 seconds ago       263 MB
debian              latest              d4b2ba78e3b4        3 weeks ago         125.1 MB
ubuntu              latest              af88597ec24b        3 weeks ago         187.9 MB


1.5 修改鏡像_02(docker build) 

1.5.1

docker commit 擴展鏡像適合個人使用,相比較,使用dockfile 創建鏡像更方便,易於共享;

1.5.2 

比如我通過Dockerfile 來創建一個可以提供http服務的容器;

root@vmc01:~/ljp/docker# mkdir -p ubuntu_lijp
root@vmc01:~/ljp/docker# cd ubuntu_lijp/
root@vmc01:~/ljp/docker/ubuntu_lijp# vim  Dockerfile
#this is a docker
FROM  ubuntu:v2_163
MAINTAINER  <[email protected]>
RUN apt-get update
RUN apt-get install -qqy apache2
root@vmc01:~/ljp/docker/ubuntu_lijp# docker build -t="ubuntu:v3_apache2" /root/ljp/docker/ubuntu_lijp/
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon 
Step 0 : FROM ubuntu:v2_163
---> 8c525f0bec06
Step 1 : MAINTAINER <[email protected]>
---> Running in f7d56b23b140
---> 2e796c9cd775
Removing intermediate container f7d56b23b140
Step 2 : RUN apt-get update
---> Running in 8fae114e67be
....
root@vmc01:~/ljp/docker/ubuntu_lijp# docker  images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              v3_apache2          71f2b7fb0272        50 seconds ago      300.1 MB
ubuntu              v2_163              8c525f0bec06        44 minutes ago      263 MB
debian              latest              d4b2ba78e3b4        3 weeks ago         125.1 MB
ubuntu              latest              af88597ec24b        3 weeks ago         187.9 MB
root@vmc01:~/ljp/docker/ubuntu_lijp# docker run -t -i ubuntu:v3_apache2  /bin/bash
root@6094bbf8d1d7:/# /etc/init.d/apache2  start
...
root@6094bbf8d1d7:/# netstat -antp 
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp6       0      0 :::80                   :::*                    LISTEN      54/apache2      
root@6094bbf8d1d7:/# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 02:42:ac:11:00:0d  
  inet addr:172.17.0.13  Bcast:0.0.0.0  Mask:255.255.0.0
root@vmc01:~# curl  172.17.0.13
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
...
上面步驟完整將使用dockerfile創建images的步驟演示出來了,有很多需要優化的地方,將在學習dockfile時集中學習;

1.6 鏡像導入,導出

1.6.1

從本地鏡像文件導入本地鏡像庫(https://openvz.org/Download/template/precreated)

root@vmc01:~/ljp/docker# ls -lh
total 219M
-rw-r--r-- 1 root root 219M Jan 29 12:12 centos-6-x86_64.tar.gz
drwxr-xr-x 2 root root 4.0K Jan 29 11:44 ubuntu_lijp
root@vmc01:~/ljp/docker# cat  centos-6-x86_64.tar.gz | docker  import  - centos:6_v1
17d0130ca4cc7e644eb20e5052956973332bdd0c9ee23e79872d1859e2101f8d
root@vmc01:~/ljp/docker# docker  images 
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              6_v1                17d0130ca4cc        32 seconds ago      612 MB
ubuntu              v3_apache2          71f2b7fb0272        41 minutes ago      300.1 MB
ubuntu              v2_163              8c525f0bec06        About an hour ago   263 MB
debian              latest              d4b2ba78e3b4        3 weeks ago         125.1 MB
ubuntu              latest              af88597ec24b        3 weeks ago         187.9 MB

1.6.2

從鏡像導出到模板文件

root@vmc01:~/ljp/docker# docker  save -o ubuntu_14.04_apache2.tar ubuntu:v3_apache2
root@vmc01:~/ljp/docker# ls
centos-6-x86_64.tar.gzubuntu_14.04_apache2.tar  ubuntu_lijp
root@vmc01:~/ljp/docker# ls -lh
total 517M
-rw-r--r-- 1 root root 219M Jan 29 12:12 centos-6-x86_64.tar.gz
-rw-r--r-- 1 root root 298M Jan 29 12:23 ubuntu_14.04_apache2.tar
drwxr-xr-x 2 root root 4.0K Jan 29 11:44 ubuntu_lijp
root@vmc01:~/ljp/docker# 
1.6.3

導入的其他方式

docker load --input centos-6-x86_64.tar.gz
docker load < centos-6-x86_64.tar.gz #將導入鏡像以及其相關的元數據信息(包括標籤等)

1.6.4

移除本地鏡像

root@vmc01:~/ljp/docker# docker  rmi centos:6_v1 
Untagged: centos:6_v1
Deleted: 17d0130ca4cc7e644eb20e5052956973332bdd0c9ee23e79872d1859e2101f8d
root@vmc01:~/ljp/docker# docker  images 
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              v3_apache2          71f2b7fb0272        59 minutes ago      300.1 MB
ubuntu              v2_163              8c525f0bec06        About an hour ago   263 MB
debian              latest              d4b2ba78e3b4        3 weeks ago         125.1 MB
ubuntu              latest              af88597ec24b        3 weeks ago         187.9 MB
#移除鏡像需要預先刪除依賴於此鏡像的容器docker rm

###鏡像導入docker import  與 docker load 的區別:

1.load 是將整個鏡像存儲文件導入本地鏡像庫,記錄完整,體積大

2.import 是見容器快照文件導入本地鏡像庫,快照文件沒有歷史記錄和元數據信息,體積小,可以重新指定標籤和元數據信息


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