Docker入門——image製做

製做初始模板linux

模板的製做在以前的一篇已經簡單介紹過了,下邊是這兩天遇到的比較蛋疼的問題和收穫的經驗,分享給你們避免走彎路。nginx

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

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

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

tar -c . | docker import - rhel65

查看一下上傳好的imagesbootstrap

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

其實網上有許多已經成型各類應用的模板能夠下載,經過ubuntu

[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代理完成下載,不過我仍是認爲不少東西必須本身作 纔有意義,才放心使用。vim

上傳以後發現一個比較蛋疼的問題,我以前作基礎包的時候都是講yum源配好的,如今已經上傳完了,但yum源尚未配置,是否是要從新打包上傳呢?centos

答案是no,no,no!bash

咱們先啓動一個容器(仍是叫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

相關文章
相關標籤/搜索