Docker 的安裝
Docker 是一個開源的商業產品,有兩個版本:社區版(Community Edition,縮寫爲 CE)和企業版(Enterprise Edition,縮寫爲 EE)。企業版包含了一些收費服務,個人開發者一般用不到。下面的介紹都針對社區版。
Docker CE 的安裝請參考官方文檔。
安裝完成後,運行下面的命令,驗證是否安裝成功。
$ docker version
# 或者
$ docker info
Docker 需要用戶具有 sudo 權限,爲了避免每次命令都輸入 sudo,可以把用戶加入 Docker 用戶組(官方文檔)。
$ sudo usermod -aG docker $USER
Docker 是服務器--客戶端
架構。命令行運行 docker 命令的時候,需要本機有 Docker 服務。如果這項服務沒有啓動,可以用下面的命令啓動(官方文檔)。
# service 命令的用法
$ sudo service docker start
# systemctl 命令的用法
$ sudo systemctl start docker
查看 docker 版本
$ docker version
Client:
Version: 1.12.1
API version: 1.24
Go version: go1.7.1
Git commit: 6f9534c
Built: Thu Sep 8 10:31:18 2016
OS/Arch: darwin/amd64
Server:
Version: 1.12.1
API version: 1.24
Go version: go1.6.3
Git commit: 23cf638
Built: Thu Aug 18 17:52:38 2016
OS/Arch: linux/amd64
搜索可用 docker 鏡像
$ docker search <image_name>
# 例如
$ docker search tutorial
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
learn/tutorial 40
georgeyord/reactjs-tutorial This is the backend of the React comment b... 5 [OK]
microsoft/aci-tutorial-app 2
fiware/tutorials.tourguide-app FIWARE Tour Guide App sample application 1 [OK]
mhausenblas/kairosdb-tutorial GitHub fetcher for KairosDB tutorial 1 [OK]
microsoft/aci-tutorial-sidecar 1
muli/gluon-tutorials-zh https://github.com/mli/gluon-tutorials-zh 1 [OK]
chris24walsh/flask-aws-tutorial Runs a simple flask webapp demo, with the ... 1 [OK]
colemurray/medium-facenet-tutorial Container for medium-facenet-tutorial. Con... 1
fgraichen/concourse-tutorial-hello-world 0
camphor/python-tutorial camphor-/python-tutorial 0 [OK]
activeeon/par-connector-tutorial Do the par-connector tutorial with R. The ... 0 [OK]
splicemachine/tutorial-spark-kafka-consumer Spark Streaming Tutorial 0
krishnatest/docker-nodejs-tutorialkk docker-nodejs-tutorialkk 0
rade95/concourse-tutorial-hello-world 0
helinwang/paddle-k8s-tutorial 0
sahotay/concourse-tutorial-hello-world 0
kidikarus/concourse-tutorial-47-tasks 0
lukasheinrich/quickana-tutorial Image for the analysis code built from htt... 0
starkandwayne/concourse-tutorial-ci 0
bluerdocker/concourse-tutorial-hello-world 0
dlws/tutorial-tensorflow-cpu 0
joshuacox/rabbitmq-tutorials Rabbit MQ Tutorial Demos 0 [OK]
cloudboost/tutorial 0
starkandwayne/concourse-tutorial 0
問題一:默認情況下 docker 是到哪裏拉取鏡像的?如何自定義配置?
回答:默認拉取地址:https://hub.docker.com
centos 下修改爲國內源:
# 在 /etc/sysconfig/docker 文件中末尾增加一行:
DOCKER_OPTS="--registry-mirror=http://hub-mirror.c.163.com"
# 然後重新運行 docker 服務:
service docker restart
問題二:拉取下來的鏡像又是放在哪裏?如何自定義配置?
回答:通過下面命令查看
docker info | grep "Docker Root Dir"
通過下面方法自定義路徑:
## 如果是 CentOS 則添加下面這行:
OPTIONS="--graph=/root/data/docker" --selinux-enabled -H fd://
## 如果是 Ubuntu 則添加下面這行(因爲 Ubuntu 默認沒開啓 selinux):
OPTIONS="--graph=/root/data/docker" -H fd://
## 或者添加配置信息
DOCKER_OPTS="-g /root/data/docker"
在容器中運行命令
docker 容器可以理解爲在沙盒中運行的進程。這個沙盒包含了該進程運行所必須的資源,包括文件系統、系統類庫、shell 環境等等。但這個沙盒默認是不會運行任何程序的。你需要在沙盒中運行一個進程來啓動某一個容器。這個進程是該容器的唯一進程,所以當該進程結束的時候,容器也會完全的停止。
docker run
命令有兩個參數,一個是鏡像名,一個是要在鏡像中運行的命令。
$ docker run learn/tutorial echo "hello word"
hello world
$ docker run learn/tutorial cat /etc/issue
Ubuntu 12.04 LTS \n \l
在容器中安裝新的程序
下一步我們要做的事情是在容器裏面安裝一個簡單的程序(ping)。我們之前下載的 tutorial 鏡像是基於 ubuntu 的,所以你可以使用 ubuntu 的 apt-get 命令來安裝 ping 程序:apt-get install -y ping
。
備註:apt-get 命令執行完畢之後,容器就會停止,但對容器的改動不會丟失。
注意:在執行 apt-get 命令的時候,要帶上 -y 參數。如果不指定 -y 參數的話,apt-get 命令會進入交互模式,需要用戶輸入命令來進行確認,但在 docker 環境中是無法響應這種交互的。
$ docker run learn/tutorial apt-get install -y ping
Reading package lists...
Building dependency tree...
The following NEW packages will be installed:
iputils-ping
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 56.1 kB of archives.
After this operation, 143 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ precise/main iputils-ping amd64 3:20101006-1ubuntu1 [56.1 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 56.1 kB in 1s (28.8 kB/s)
Selecting previously unselected package iputils-ping.
(Reading database ... 7545 files and directories currently installed.)
Unpacking iputils-ping (from .../iputils-ping_3%3a20101006-1ubuntu1_amd64.deb) ...
Setting up iputils-ping (3:20101006-1ubuntu1) ...
保存對容器的修改
當你對某一個容器做了修改之後(通過在容器中運行某一個命令),可以把對容器的修改保存下來,這樣下次可以從保存後的最新狀態運行該容器。docker 中保存狀態的過程稱之爲 committing
,它保存的新舊狀態之間的區別,從而產生一個新的版本。
使用 docker ps -l
命令獲得安裝完 ping 命令之後容器的 id。然後把這個鏡像保存爲 learn/ping
。
使用 docker commit 來保存對容器的修改:
- 運行 docker commit,可以查看該命令的參數列表。
- 你需要指定要提交保存容器的 ID。(譯者按:通過docker ps -l 命令獲得)
$ docker commit fa56b78b5a50 learn/ping
sha256:c7e772ec6d5112b84831a7130c74e5a0d0cd1804ad9426b004230c7a0af7ba94
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
learn/ping latest c7e772ec6d51 28 seconds ago 139.5 MB
learn/tutorial latest a7876479f1aa 5 years ago 128 MB
但是實際環境中並不推薦如上所示的 commit 用法,具體可以查看《Docker 從入門到實踐》中的描述,總結下來爲三點:
- 由於
commit
命令的執行,有很多文件被改動或添加了。這還僅僅是最簡單的操作,如果是安裝軟件包、編譯構建,那會有大量的無關內容被添加進來,如果不小心清理,將會導致鏡像極爲臃腫。 - 使用
commit
生成的鏡像也被稱爲黑箱鏡像,換句話說,就是除了製作鏡像的人知道執行過什麼命令、怎麼生成的鏡像,別人根本無從得知。 - 不符合分層存儲的概念,即除當前層外,之前的每一層都是不會發生改變的。
使用運行新鏡像
在新的鏡像中運行 ping www.163.com
命令。一定要使用新的鏡像名 learn/ping
來運行 ping 命令。(因爲最開始下載的 learn/tutorial
鏡像中是沒有 ping 命令的)
$ docker run learn/ping ping www.163.com
PING www.163.com.lxdns.com (115.223.24.39) 56(84) bytes of data.
64 bytes from 115.223.24.39: icmp_req=1 ttl=37 time=0.264 ms
64 bytes from 115.223.24.39: icmp_req=2 ttl=37 time=0.546 ms
查看容器中的日誌
## 實時查看 docker 容器日誌
$ docker logs -f -t --tail 行數 容器名
參考:
1、http://www.ruanyifeng.com/blog/2018/02/docker-tutorial.html
2、http://www.ruanyifeng.com/blog/2018/02/docker-wordpress-tutorial.html