在使用 Docker 之前我們通常會使用 Vagrant 來 build & share
開發(fā)環(huán)境。每個 Vagrant package
都是一個完整的虛擬主機(jī)诀艰,一個 package
通常有幾個 G雳锋。共享和更新幾個 G 的 package 是很一件昂貴的事情黄绩。
在 Docker 世界中將 Vagrant
中的 package
拆成 containers
& images
,這兩個概念支撐這整個 Docker 世界玷过。
- container:一個剝掉殼的最小化 Linux爽丹。
- image:一個 software,它將會被加載到
container
中辛蚊。
當(dāng)運行 docker run helloworld
時粤蝎,docker
會做如下三件事情:
- 在本地查找
hello-world
software image - 如果找不到,從 Docker Hub 上下載 image
- 將
image
加載到container
中并run
通常我們使用 docker-machine
管理 container
袋马,使用 docker
來運行image
初澎。images
通過 Docker Hub 來共享。
(如果你是 Github 的使用者虑凛,你將會很輕松的上手 Docker Hub
碑宴。關(guān)于 Docker Hub
本文暫不做介紹)
Docker 中的 container 在哪?
container
是一個剝了殼的桑谍,最基礎(chǔ)的 Linux延柠。在 Mac OS 中我們使用 docker-machine
來管理 container
。
創(chuàng)建 container
創(chuàng)建一個名為 dev
的 container
: docker-machine create --driver virtualbox dev
查看本地的 containers
? mydockerbuild docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM
default virtualbox Running tcp://192.168.99.101:2376
dev virtualbox Running tcp://192.168.99.100:2376
查看 container 狀態(tài)
? mydockerbuild docker-machine active
dev
? mydockerbuild docker-machine status dev
Running
連接到 container
? mydockerbuild docker-machine ssh dev
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.8.2, build master : aba6192 - Thu Sep 10 20:58:17 UTC 2015
Docker version 1.8.2, build 0a8c2e3
er@dev:~$ uname -a
Linux dev 4.0.9-boot2docker #1 SMP Thu Sep 10 20:39:20 UTC 2015 x86_64 GNU/Linux
Docker 中的 image 是什么?
docker run helloworld 運行一個image
此時 docker 正在 run
一個名為 hello-world
的應(yīng)用程序锣披,它將會打印如下結(jié)果:
? sts docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
535020c3e8ad: Pull complete
af340544ed62: Pull complete
Digest: sha256:a68868bfe696c00866942e8f5ca39e3e31b79c1e50feaee4ce5e28df2f051d5c
Status: Downloaded newer image for hello-world:latest
Hello from Docker.
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
此時的 hello-world
程序在 docker 中成為一個 image
贞间,它被 docker 放在 container
上面運行贿条。
docker images 查看本地的 images
? mydockerbuild docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker-whale latest 1935900e3705 5 hours ago 274 MB
ubuntu latest 91e54dfb1179 6 weeks ago 188.4 MB
hello-world latest af340544ed62 8 weeks ago 960 B
docker/whalesay latest fb434121fc77 4 months ago 247 MB
總結(jié)
images
& containers
是 Docker 世界中最基本的概念。image
即 software增热,container
即運行 software 用的操作系統(tǒng)整以。
在 Docker 世界中,我們大部分時間都在和 Docker 中的 images
打交道钓葫。docker
采用類似 git
的機(jī)制來管理和分享 images
悄蕾,當(dāng)我們使用 docker
創(chuàng)建分享 images
時票顾,如同使用 git
一樣础浮。
Docker Hub
的存在,如同 Github
對 git
的存在一樣奠骄,讓 docker images
分享變得非常容易豆同。