最近聽說docker很火,這兩天學習了一下。本人實用的是mac os操作系統。
有關什么是docker、docker的好處以及安裝,請參考:http://tech.uc.cn/?p=2726
一、基本命令
1、安裝ubuntu虛擬機
docker pull ubuntu
2、查看鏡像
@~ $ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest 9bd07e480c5b 9 days ago 192.7 MB
3、登錄ubuntu
@~ $ docker run -a stdin -a stdout -a stderr -i -t ubuntu /bin/bash root@ccaffb725895:/# uname -a Linux ccaffb725895 3.16.4-tinycore64 #1 SMP Thu Oct 23 16:14:24 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux root@ccaffb725895:/#
下面解釋一下上面的命令:docker run -i -t <image> /bin/bash 使用image創建container並進入交互模式, login shell是/bin/bash
4、查看container。退出ubuntu之后,查看
root@ccaffb725895:/# exit exit @~ $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ccaffb725895 ubuntu:latest "/bin/bash" 3 minutes ago Exited (0) 15 seconds ago trusting_bohr
二、保存container
如果我以后登錄ubuntu都輸入上面這些命令太繁瑣了,需要鎖定container
1、啟動container
@~ $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ccaffb725895 ubuntu:latest "/bin/bash" 3 minutes ago Exited (0) 15 seconds ago trusting_bohr @~ $ docker start ccaffb725895 ccaffb725895
2、attach
@~ $ docker start ccaffb725895 ccaffb725895 @~ $ docker attach ccaffb725895 root@ccaffb725895:/# root@ccaffb725895:/# uname -a Linux ccaffb725895 3.16.4-tinycore64 #1 SMP Thu Oct 23 16:14:24 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux root@ccaffb725895:/#
3、保存鏡像,commit命令
@~ $ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest 9bd07e480c5b 9 days ago 192.7 MB @~ $ docker commit -a "liqiu <qiu.li@qunar.com>" ccaffb725895 ubuntu:ssh-ubuntu 5d2941e8c0bf7fe988604f11d043a03331ec83536ce09f841e760d51156b6865 @~ $ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu ssh-ubuntu 5d2941e8c0bf About a minute ago 192.7 MB ubuntu latest 9bd07e480c5b 9 days ago 192.7 MB
三、刪除container
@~ $ docker stop ccaffb725895 ccaffb725895 @~ $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ccaffb725895 ubuntu:latest "/bin/bash" 16 minutes ago Exited (127) 5 minutes ago trusting_bohr @~ $ docker rm ccaffb725895 ccaffb725895 @~ $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES @
四、恢復
一切恢復平靜之后,思考一個問題,我在虛擬機安裝了一個curl,過幾天我再啟動它curl還存在嗎?
1、進入ubuntu,安裝curl
@~ $ docker run -a stdin -a stdout -a stderr -i -t ubuntu /bin/bash
root@0efb23b52565:/# sudo apt-install curl
2、退出之后,保存鏡像
@~ $ docker commit -a "liqiu <qiu.li@qunar.com>" 0efb23b52565 ubuntu:curl-ubuntu aac468ce18c883bc71e849028a1718e57d80d3c6231840e6b1fdb4d66354bfe6 @~ $ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu curl-ubuntu aac468ce18c8 About a minute ago 204.1 MB ubuntu latest 9bd07e480c5b 9 days ago 192.7 MB
3、重新進入鏡像
@~ $ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu curl-ubuntu aac468ce18c8 About a minute ago 204.1 MB ubuntu latest 9bd07e480c5b 9 days ago 192.7 MB @~ $ docker run -a stdin -a stdout -a stderr -i -t aac468ce18c8 /bin/bash root@00ddbbab1ccf:/# curl www.baidu.com <!DOCTYPE html> ...... ...... ......
五、其他命令介紹:
ctrl-p ctrl-q 退出虛擬機,如果重新進入,可以使用docker attach ***
root@2c1282c522bf:/# (此時ctrl+p 然后 ctrl+q)
@~/git/github/docker-hive-on-tez-master $ docker attach 2c1282c522bf root@2c1282c522bf:/# (重新進入了)