在我的小 rmbp 256G的硬盤里,實(shí)在是裝不下100多個(gè)G的虛擬機(jī)了,所以想把一些東西遷移到這兩年很火的Docker下,Docker以前也有過一兩次,只是按著別人給的用法用的,具體的一些細(xì)節(jié)并沒有深入,和git一樣,這么牛掰的東西怎么能不好好學(xué)一些呢?
Docker是一種容器,虛擬機(jī)是一種管理程序虛擬機(jī)化(hypervisor virtualization,HV)。管理程序虛擬化通過中間層將一臺(tái)或者多臺(tái)獨(dú)立的機(jī)器虛擬運(yùn)行在物理硬件之上,而容器(比如Docker)則是直接運(yùn)行在操作系統(tǒng)內(nèi)核之上的用戶空間。由于容器是運(yùn)行在操作系統(tǒng)上的,所以只能運(yùn)行底層和宿主機(jī)相同或者類似的操作系統(tǒng),比如說在Ubuntu下可以在容器里運(yùn)行Centos,卻不能運(yùn)行Windows。
目前Windows上的Docker可以跑linux的Docker容器,是因?yàn)榈紫屡芰薒inux的VM,但是馬上就可以支持Windows Server 2016了,如鏈接[Introducing the Technical PReview of Docker Engine for Windows Server 2016](http://Introducing the Technical Preview of Docker Engine for Windows Server 2016)。
容器的優(yōu)點(diǎn):
一次save,到處運(yùn)行。啟動(dòng)速度快,消耗資源少。Docker與虛擬機(jī)性能比較容器缺點(diǎn):
資源隔離方面不如虛擬機(jī)。安全性問題,“權(quán)限隔離”做的不夠好,只要有Docker的命令權(quán)限,就可以操作所有的Docker實(shí)例。Docker客戶端和服務(wù)器
Docker是一個(gè)C/S架構(gòu)的程序,Docker客戶端需要向Docker服務(wù)器發(fā)出請(qǐng)求,服務(wù)器完成請(qǐng)求后返回信息。一個(gè)本地Docker客戶端可以連接遠(yuǎn)端的Docker服務(wù)器進(jìn)行操作,如下圖。
Docker鏡像
鏡像是構(gòu)建Docker世界的基石。用戶基于鏡像來維護(hù)自己的容器。Docker鏡像是Docker容器運(yùn)行時(shí)的只讀模板,每一個(gè)鏡像由一系列的層 (layers) 組成。Docker使用 UnionFS來將這些層聯(lián)合到單獨(dú)的鏡像中。UnionFS允許獨(dú)立文件系統(tǒng)中的文件和文件夾(稱之為分支)被透明覆蓋,形成一個(gè)單獨(dú)連貫的文件系統(tǒng)。正因?yàn)橛辛诉@些層的存在,Docker是如此的輕量。當(dāng)你改變了一個(gè)Docker鏡像,比如升級(jí)到某個(gè)程序到新的版本,一個(gè)新的層會(huì)被創(chuàng)建。因此,不用替換整個(gè)原先的鏡像或者重新建立(在使用虛擬機(jī)的時(shí)候你可能會(huì)這么做),只是一個(gè)新的層被添加或升級(jí)了。現(xiàn)在你不用重新發(fā)布整個(gè)鏡像,只需要升級(jí),層使得分發(fā)Docker鏡像變得簡(jiǎn)單和快速。
Docker倉庫(Registry)
Docker使用Registry來保存用戶構(gòu)建的鏡像,就像蘋果的apple store。Registry分為私有和公有兩種,Docker公司自己運(yùn)營(yíng)的Registry叫做Docker Hub。
Docker容器
Docker可以幫你構(gòu)建和部署容器,用戶只需要把自己的應(yīng)用程序或服務(wù)打包放進(jìn)容器即可。每一個(gè)Docker容器都是從Docker鏡像創(chuàng)建的。Docker容器可以運(yùn)行、開始、停止、移動(dòng)和刪除。每一個(gè)Docker容器都是獨(dú)立和安全的應(yīng)用平臺(tái),Docker容器是Docker的運(yùn)行部分。
Docker的技術(shù)組件
Docker可以被安裝在x64架構(gòu),內(nèi)核3.10以上的linux系主機(jī)、win10以上windows和OS X 10.10.3且2010年以后的Mac上。在2013年Docker剛發(fā)布的時(shí)候,它是一款基于LXC的開源容器管理引擎。把LXC復(fù)雜的容器創(chuàng)建與使用方式簡(jiǎn)化為Docker自己的一套命令體系。 隨著Docker的不斷發(fā)展,它開始有了更為遠(yuǎn)大的目標(biāo),那就是反向定義容器的實(shí)現(xiàn)標(biāo)準(zhǔn),將底層實(shí)現(xiàn)都抽象化到Libcontainer的接口。這就意味 著,底層容器的實(shí)現(xiàn)方式變成了一種可變的方案,無論是使用namespace、cgroups技術(shù)抑或是使用systemd等其他方案,只要實(shí)現(xiàn)了 Libcontainer定義的一組接口,Docker都可以運(yùn)行。
安裝方法都很簡(jiǎn)單,值得注意的是當(dāng)前Docker版本的安裝需求,比如現(xiàn)在Linux下安裝的需求就上x64架構(gòu),內(nèi)核3.10以上。
Mac下安裝方法,直接在官網(wǎng)上下載docker app,安裝即可。 Linux下安裝方法,Linux下最簡(jiǎn)單的安裝方法就是apt和yum包管理工具進(jìn)行安裝了。 Windows下安裝方法
還有一個(gè)比較好用的安裝腳本,這個(gè)腳本只支持在lsb、debian、fedora、Oracle、centos、redhat、os這幾個(gè)發(fā)行版中使用。
在安裝結(jié)束后,可以使用docker info
命令來查看Docker是否裝好了。Mac下的docker info結(jié)果:
Mac裝好后如下圖的樣子,基本功能都已經(jīng)在菜單上了。
同時(shí)Mac還有一個(gè)GUI界面Kitmatic,目前還是beta版,但是用起來還是很不錯(cuò)的。
各種各樣的image看起來很好看。
先把Docker的命令行打印出來。
~ docker --helpUsage: docker [OPTIONS] COMMAND [arg...] docker [ --help | -v | --version ]A self-sufficient runtime for containers.Options: --config=~/.docker Location of client config files -D, --debug Enable debug mode -H, --host=[] Daemon socket(s) to connect to -h, --help Print usage -l, --log-level=info Set the logging level --tls Use TLS; implied by --tlsverify --tlscacert=~/.docker/ca.pem Trust certs signed only by this CA --tlscert=~/.docker/cert.pem Path to TLS certificate file --tlskey=~/.docker/key.pem Path to TLS key file --tlsverify Use TLS and verify the remote -v, --version Print version information and quitCommands: attach Attach to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on a container, image or task kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry. logout Log out from a Docker registry. logs Fetch the logs of a container network Manage Docker networks node Manage Docker Swarm nodes pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart a container rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images service Manage Docker services start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers swarm Manage Docker Swarm tag Tag an image into a repository top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information volume Manage Docker volumes wait Block until a container stops, then print its exit codeRun 'docker COMMAND --help' for more information on a command.'我相信能用Docker都是的大神,就不翻譯了。
在安裝好以后來運(yùn)行一個(gè)最簡(jiǎn)單的hello world吧。
docker run hello-world所見即所得,如圖。
在運(yùn)行docker run
的時(shí)候,可以看到打印出了Hello from Docker!
,首先docker在本地去檢查了是否有一個(gè)叫做hello-world
的鏡像,在這里,我們剛裝好的docker里必然是沒有的,所以docker就去Docker Hub上找這個(gè)鏡像,找到以后下載下來,run。讀一下這個(gè)helloworld的輸出,可以docker run -it ubuntu bash
來運(yùn)行一個(gè)ubuntu。來試一試
運(yùn)行以后,在docker hub里下載好ubuntu鏡像后,docker構(gòu)造好容器啟動(dòng),就可以和正常的shell一樣的進(jìn)行操作了。
更多內(nèi)容盡在docker-learn1。
原文鏈接:http://dengnanyi.com/2016/09/28/docker-learn-0/
新聞熱點(diǎn)
疑難解答
圖片精選