Docker入門——image制作


制作初始模板

模板的制作在之前的一篇已經簡單介紹過了,下邊是這兩天遇到的比較蛋疼的問題和收獲的經驗,分享給大家避免走彎路。

所謂的模板就是一個通用的底層,其他應用都可以以最小修改的方式完成應用的部署,當然模板也需要輕便和精簡。

mount /dev/sr0 /mnt
febootstrap -i iputils -i vim-common -i openssh-server -i yum -i passwd -i wget -i git -i telnet rhel redhat file:///mnt/   #這個軟件包需要epel的,需要fakeroot支持,fedora的貌似不是一樣的工具

我選了幾個比較重要的包,在使用中經常需要安裝

打包上傳到docker上,基本的任務就算完成了。

tar -c . | docker import - rhel65

查看一下上傳好的images

[root@craft redhat]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
rhel65              latest              9feee56f6c68        48 seconds ago      385.4 MB

其實網上有許多已經成型各種應用的模板可以下載,通過

[root@craft redhat]# docker search nginx
NAME                                     DESCRIPTION                                     STARS     OFFICIAL   TRUSTED
walm/nginx                               Nginx 1.4.3                                     0                    
dockerfile/nginx                         Trusted Nginx (http://nginx.org/) Build         3                    [OK]
bconklin/nginx                           Test nginx module                               0                    
stapelberg/nginx                         nginx 1.4.4-1 from Debian                       0                    
yoshiso/nginx                            centos6.5 with nginx works on port 80           0                    
mguymon/nginx                            Basic nginx container for Ubuntu 13.04          1                    [OK]
michaloo/nginx                           This is nginx compiled on ubuntu base image.    0                    
mmckeen/nginx                            Nginx application image based on an openSU...   0                    [OK]
.....................

但是因為docker的服務器在牆外。。。可以通過http代理完成下載,不過我還是認為很多東西必須自己做 才有意義,才放心使用。

上傳之后發現一個比較蛋疼的問題,我之前做基礎包的時候都是講yum源配好的,現在已經上傳完了,但yum源還沒有配置,是不是要重新打包上傳呢?

答案是no,no,no!

我們先啟動一個容器(還是叫VM比較習慣,下邊統稱為VM)

docker run -t -i -v /mnt/:/mnt/ rhel65 bash   #-t 偽終端   -i標准輸出   -v 目錄重定向   rhel65 是images的名字   bash是啟動VM執行的命令

啟動一個無名氏虛擬機(因為沒有指定--name參數)(-i參數是shell終端直接進入到vm的終端,所以需要重啟起一個終端才能查看)

[root@craft ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
2e3537df5275        rhel65:latest       bash                37 seconds ago      Up 36 seconds                           sharp_archimede 

VM此時的狀態

bash-4.1#

因為我們在定制虛擬機模板的時候已經安裝好了ssh服務,所以可以直接啟動

bash-4.1# /etc/init.d/sshd start
Generating SSH1 RSA host key:                              [  OK  ]
Generating SSH2 RSA host key:                              [  OK  ]
Generating SSH2 DSA host key:                              [  OK  ]
Starting sshd:                                             [  OK  ]
bash-4.1# ifconfig    #通過ifconfig 我們可以看到虛擬機的ip地址為172.17.0.2 通過宿主用ssh可以直接連接。
eth0      Link encap:Ethernet  HWaddr F2:38:51:9B:15:F4  
          inet addr:172.17.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::f038:51ff:fe9b:15f4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:368 (368.0 b)  TX bytes:368 (368.0 b)

使用ssh連接到VM(記得要先修改虛擬機的密碼)

[root@craft ~]# ssh 172.17.0.2
The authenticity of host '172.17.0.2 (172.17.0.2)' can't be established.
RSA key fingerprint is f2:8b:83:3d:8f:34:8a:28:7c:88:4e:12:35:ab:03:24.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.2' (RSA) to the list of known hosts.
root@172.17.0.2's password: 
Connection to 172.17.0.2 closed.

連接錯誤了,此時需要修改ssh的配置文件,將PAM禁用

bash-4.1# sed -i 's/UsePAM yes/UsePAM no/' /etc/ssh/sshd_config   #修改sshd配置文件
bash-4.1# /etc/init.d/sshd restart                    #重啟sshd服務
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]

再嘗試連接

[root@craft ~]# ssh 172.17.0.2
root@172.17.0.2's password: 
Last login: Thu Jan 16 01:38:27 2014 from 172.17.42.1
-bash-4.1# 

發現終端很丑有木有,因為沒有執行/etc/bashrc 生成環境變量

我們將宿主的bash腳本拷貝到VM中

[root@craft ~]# scp .bash* 172.17.0.2:/root/
root@172.17.0.2's password: 
.bash_history                                                                                                                                                                                100%   16KB  15.9KB/s   00:00    
.bash_logout                                                                                                                                                                                   100%   18     0.0KB/s   00:00    
.bash_profile                                                                                                                                                                                  100%  176     0.2KB/s   00:00    
.bashrc                                                                                                                                                                                        100%  176     0.2KB/s   00:00    

重新連接ssh即可。

下邊配置下yum源

bash-4.1# cat > /etc/yum.repos.d/base.repo        
[local]
name=local
baseurl=file:///mnt/
enabled=1
gpgcheck=0

基本的配置已經做好了,我們將配置信息推送回模板,這樣等下次需要一個VM時就不需要以上麻煩的初始化了。

[root@craft ~]# docker ps      #查看一下VM的信息,Container id
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
2e3537df5275        rhel65:latest       bash                15 minutes ago      Up 15 minutes                           sharp_archimede     
[root@craft ~]# docker commit 2e rhel65-new  #執行合並命令,2e是VM container id的簡寫,可以直接識別到虛擬機 rhel65-new是合並新模板的名字,也可以覆蓋原有模板
78efa95a25888cac0b7f98e325c465bcf14f7008e62b09f80287091c882fdd93
[root@craft ~]# docker images    #查看所有的模板,現在有兩個模板
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
rhel65-new          latest              78efa95a2588        13 seconds ago      385.4 MB
rhel65              latest              9feee56f6c68        23 minutes ago      385.4 MB

通過新模板啟動一個VM

[root@craft ~]# docker run -i -t -p 22 rhel65-new bash  # -p是將VM的22端口指向到宿主機,如果沒有指定,則會隨機分配一個。
bash-4.1# ls -a    #查看下剛才修改過的地方,發現已經生效了
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc

使用ssh連接到VM,首先需要在VM啟動sshd服務

[root@craft ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMES
cf34cd94a055        rhel65-new:latest   bash                5 minutes ago       Up 5 minutes        0.0.0.0:49153->22/tcp   kickass_turing      
2e3537df5275        rhel65:latest       bash                24 minutes ago      Up 24 minutes                               sharp_archimede
Xshell:\> ssh 192.168.91.142 49153  #是宿主機的ip 和端口

如果不希望啟動一個VM后直接進入到VM的終端,而是希望后台運行,通過ssh服務連接的話可以嘗試下

[root@craft ~]# docker run -t -d  rhel65-new /usr/sbin/sshd -D
9710b69c0fafdea8b1790b2406fe664e85a45038675c3feeb31880fd20d6c602
[root@craft ~]# docker inspect 97 | grep IPAddress
        "IPAddress": "172.17.0.5",
[root@craft ~]# ssh 172.17.0.5
The authenticity of host '172.17.0.5 (172.17.0.5)' can't be established.
RSA key fingerprint is f2:8b:83:3d:8f:34:8a:28:7c:88:4e:12:35:ab:03:24.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.5' (RSA) to the list of known hosts.
root@172.17.0.5's password: 
Last login: Thu Jan 16 01:41:08 2014 from 172.17.42.1
[root@9710b69c0faf ~]# 

或者直接通過端口轉發

[root@craft ~]# docker run -t -d  -p 10010:22 rhel65-new /usr/sbin/sshd -D
e0568b009603143c7838cf2afe1771de77a570ccacfcd27de3a75d2f47b360f6
[root@craft ~]# ssh 192.168.91.142 -p 10010
The authenticity of host '[192.168.91.142]:10010 ([192.168.91.142]:10010)' can't be established.
RSA key fingerprint is f2:8b:83:3d:8f:34:8a:28:7c:88:4e:12:35:ab:03:24.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.91.142]:10010' (RSA) to the list of known hosts.
root@192.168.91.142's password: 
Last login: Thu Jan 16 01:41:08 2014 from 172.17.42.1
[root@e0568b009603 ~]# 

 

 

最后值得提醒的是 記得宿主上要關閉selinux。

 

下一次會分享,通過Dockerfile 創建VM

歡迎加入 Docker部落:345819364

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM