Docker安裝和基礎用法實例分析

蝸牛 互聯網技術資訊 2022-05-26 77 0

本文小編為大家詳細介紹“Docker安裝和基礎用法實例分析”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Docker安裝和基礎用法實例分析”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

1. 安裝

1.1 在 ubuntu 14.04 上安裝 docker

前提要求:

內核版本必須是3.10或者以上

依次執行下面的步驟:

sudo?apt-get?update
sudo?apt-get?install?apt-transport-https?ca-certificates
sudo?apt-key?adv?--keyserver?hkp://p80.pool.sks-keyservers.net:80?--recv-keys?58118e89f3a912897c070adbf76221572c52609d
編輯?/etc/apt/sources.list.d/docker.list?文件,添加?deb?https://apt.dockerproject.org/repo?ubuntu-trusty?main
sudo?apt-get?update
sudo?apt-get?purge?lxc-docker
apt-cache?policy?docker-engine
apt-get?upgrade
sudo?apt-get?install?linux-image-extra-$(uname?-r)?linux-image-extra-virtual
sudo?apt-get?install?docker-engine

至此,安裝過程完成。

運行 sudo service docker start 啟動 docker 守護進程。

運行 docker version 查看 docker 版本

root@devstack:/home/sammy#?docker?--version
docker?version?1.12.1,?build?23cf638

啟動第一個容器:

啟動第一個docker 容器 docker run hello-world

root@devstack:/home/sammy#?docker?run?hello-world

hello?from?docker!
this?message?shows?that?your?installation?appears?to?be?working?correctly.

它的運行成功也表明前面的安裝步驟都運行正確了。

以上內容參考自 docker 官網:

1.2 docker 到目前(2016/09/16)為止的版本歷史
Docker安裝和基礎用法實例分析  docker 第1張

2. docker 的基本操作

2.1 docker 容器的狀態機Docker安裝和基礎用法實例分析  docker 第2張

一個容器在某個時刻可能處于以下幾種狀態之一:

  • created:已經被創建 (使用 docker ps -a 命令可以列出)但是還沒有被啟動 (使用 docker ps 命令還無法列出)

  • running:運行中

  • paused:容器的進程被暫停了

  • restarting:容器的進程正在重啟過程中

  • exited:上圖中的 stopped 狀態,表示容器之前運行過但是現在處于停止狀態(要區別于 created 狀態,它是指一個新創出的尚未運行過的容器)??梢酝ㄟ^ start 命令使其重新進入 running 狀態

  • destroyed:容器被刪除了,再也不存在了

你可以在 docker inspect 命令的輸出中查看其詳細狀態:

"state":?{
???"status":?"running",
???"running":?true,
???"paused":?false,
???"restarting":?false,
???"oomkilled":?false,
???"dead":?false,
???"pid":?4597,
???"exitcode":?0,
???"error":?"",
???"startedat":?"2016-09-16t08:09:34.53403504z",
???"finishedat":?"2016-09-16t08:06:44.365106765z"
??}

2.2 docker 命令概述

我們可以把docker 的命令大概地分類如下:

鏡像操作:
?build??build?an?image?from?a?dockerfile
?commit?create?a?new?image?from?a?container's?changes
?images?list?images
?load??load?an?image?from?a?tar?archive?or?stdin
?pull??pull?an?image?or?a?repository?from?a?registry
?push??push?an?image?or?a?repository?to?a?registry
?rmi??remove?one?or?more?images
?search?search?the?docker?hub?for?images
?tag??tag?an?image?into?a?repository
?save??save?one?or?more?images?to?a?tar?archive?(streamed?to?stdout?by?default)
?history?顯示某鏡像的歷史
?inspect?獲取鏡像的詳細信息

?容器及其中應用的生命周期操作:
?create?create?a?new?container?(創建一個容器)??
?kill??kill?one?or?more?running?containers
?inspect?return?low-level?information?on?a?container,?image?or?task
?pause??pause?all?processes?within?one?or?more?containers
?ps??list?containers
?rm??remove?one?or?more?containers?(刪除一個或者多個容器)
?rename?rename?a?container
?restart?restart?a?container
?run??run?a?command?in?a?new?container?(創建并啟動一個容器)
?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?(停止一個處于運行狀態的容器)
?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
?wait??block?until?a?container?stops,?then?print?its?exit?code
?attach?attach?to?a?running?container
?exec??run?a?command?in?a?running?container
?port??list?port?mappings?or?a?specific?mapping?for?the?container
?logs??獲取容器的日志?
?
?容器文件系統操作:
?cp??copy?files/folders?between?a?container?and?the?local?filesystem
?diff??inspect?changes?on?a?container's?filesystem
?export?export?a?container's?filesystem?as?a?tar?archive
?import?import?the?contents?from?a?tarball?to?create?a?filesystem?image
?
?docker?registry?操作:
?login??log?in?to?a?docker?registry.
?logout?log?out?from?a?docker?registry.
?
?volume?操作
?volume?manage?docker?volumes
?
?網絡操作
?network?manage?docker?networks
?
?swarm?相關操作
?swarm??manage?docker?swarm
?service?manage?docker?services
?node??manage?docker?swarm?nodes??
?
?系統操作:?
?version?show?the?docker?version?information
?events?get?real?time?events?from?the?server?(持續返回docker?事件)
?info??display?system-wide?information?(顯示docker?主機系統范圍內的信息)

比較有意思的幾個命令:

(1)容器從生到死整個生命周期

root@devstack:/home/sammy#?docker?create?--name?web31?training/webapp?python?app.py?#創建名字為?web31?的容器
7465f4cb7c49555af32929bd1bc4213f5e72643c0116450e495b71c7ec128502
root@devstack:/home/sammy#?docker?inspect?--format='{{.state.status}}'?web31?#其狀態為?created
created
root@devstack:/home/sammy#?docker?start?web31?#啟動容器
web31
root@devstack:/home/sammy#?docker?inspect?--format='{{.state.status}}'?web31?#其狀態為?running
running
root@devstack:/home/sammy#?docker?pause?web31?#暫停容器
web31
root@devstack:/home/sammy#?docker?inspect?--format='{{.state.status}}'?web31
paused
root@devstack:/home/sammy#?docker?unpause?web31?#繼續容器
web31
root@devstack:/home/sammy#?docker?inspect?--format='{{.state.status}}'?web31
running
root@devstack:/home/sammy#?docker?rename?web31?newweb31?#重命名
root@devstack:/home/sammy#?docker?inspect?--format='{{.state.status}}'?newweb31
running
root@devstack:/home/sammy#?docker?top?newweb31?#在容器中運行?top?命令
uid?????pid?????ppid????c?????stime????tty?????time????cmd
root????5009????4979????0?????16:28??????????00:00:00???python?app.py
root@devstack:/home/sammy#?docker?logs?newweb31?#獲取容器的日志
?*?running?on?http://0.0.0.0:5000/?(press?ctrl+c?to?quit)
root@devstack:/home/sammy#?docker?stop?newweb31?#停止容器
newweb31
root@devstack:/home/sammy#?docker?inspect?--format='{{.state.status}}'?newweb31
exited
root@devstack:/home/sammy#?docker?rm?newweb31?#刪除容器
newweb31
root@devstack:/home/sammy#?docker?inspect?--format='{{.state.status}}'?newweb31
error:?no?such?image,?container?or?task:?newweb31

(2) docker stop 和 docker kill

在docker stop 命令執行的時候,會先向容器中pid為1的進程發送系統信號 sigterm,然后等待容器中的應用程序終止執行,如果等待時間達到設定的超時時間(默認為 10秒,用戶可以指定特定超時時長),會繼續發送sigkill的系統信號強行kill掉進程。在容器中的應用程序,可以選擇忽略和不處理sigterm信號,不過一旦達到超時時間,程序就會被系統強行kill掉,因為sigkill信號是直接發往系統內核的,應用程序沒有機會去處理它。

比如運行 docker stop web5 -t 20 命令后:

2016-09-16t16:01:18.206540853+08:00 container kill b3256ef1400a7f6a6f242e377a77af5e25d3b12237c4ee7c2e9b31a5f6437868 (image=training/webapp, name=web5, signal=15)
2016-09-16t16:01:38.212352224+08:00 container kill b3256ef1400a7f6a6f242e377a77af5e25d3b12237c4ee7c2e9b31a5f6437868 (image=training/webapp, name=web5, signal=9)
2016-09-16t16:01:38.235021315+08:00 container die b3256ef1400a7f6a6f242e377a77af5e25d3b12237c4ee7c2e9b31a5f6437868 (exitcode=137, image=training/webapp, name=web5)

能看到:

  1. 首先 docker 向容器發出 sigterm 信號(signal=15)

  2. 等待20秒 (01:18 到 01:38)

  3. 再發送 sigkill 系統信號 (signal = 9)

  4. 然后容器被殺掉了 (die)

而 docker kill 命令會直接發出sigkill的系統信號,以強行終止容器中程序的運行。運行 docker kill web5 命令后:

2016-09-16t16:06:44.351086471+08:00 container kill b3256ef1400a7f6a6f242e377a77af5e25d3b12237c4ee7c2e9b31a5f6437868 (image=training/webapp, name=web5, signal=9)
2016-09-16t16:06:44.365116100+08:00 container die b3256ef1400a7f6a6f242e377a77af5e25d3b12237c4ee7c2e9b31a5f6437868 (exitcode=137, image=training/webapp, name=web5)
可見直接發出的是 sigkill 信號,容器立馬就被殺掉了。

(3)使用 docker cp 在 host 和 container 之間拷貝文件或者目錄

root@devstack:/home/sammy#?docker?cp?/home/sammy/mydockerbuild/dockerfile?web5:/webapp?#從?host?拷貝文件到?container?里面
root@devstack:/home/sammy#
root@devstack:/home/sammy#?docker?cp?web5:/webapp/dockerfile?/home/sammy/dockerfile?#從?container?里面拷貝文件到?host?上
root@devstack:/home/sammy#?ls?/home/sammy
chroot?devstack?dockerfile?mongodbdocker?mydockerbuild?webapp

(4)docker export 和 import

docker export:將一個容器的文件系統打包為一個壓縮文件

root@devstack:/home/sammy#?docker?export?web5?-o?./web5
root@devstack:/home/sammy#?ls
chroot?devstack?dockerfile?mongodbdocker?mydockerbuild?web5?webapp

docker import:從一個壓縮文件創建一個鏡像

root@devstack:/home/sammy#?docker?import?web5?web5img?-m?"imported?on?0916"
sha256:745bb258be0a69a517367667646148bb2f662565bb3d222b50c0c22e5274a926
root@devstack:/home/sammy#?docker?history?web5img
image????created????created?by???size????comment
745bb258be0a??6?seconds?ago???????324?mb????imported?on?0916

?2.3 docker run 命令

docker run 命令會創建一個容器并啟動它,它也是包含很多的參數,按照用途將它們分類如下:

cgroups?和?namespace?相關:
??--blkio-weight?value???block?io?(relative?weight),?between?10?and?1000
??--blkio-weight-device?value?block?io?weight?(relative?device?weight)?(default?[])
??--cgroup-parent?string??optional?parent?cgroup?for?the?container
??--cpu-percent?int????cpu?percent?(windows?only)
??--cpu-period?int????limit?cpu?cfs?(completely?fair?scheduler)?period
??--cpu-quota?int????limit?cpu?cfs?(completely?fair?scheduler)?quota
?-c,?--cpu-shares?int????cpu?shares?(relative?weight)
??--cpuset-cpus?string???cpus?in?which?to?allow?execution?(0-3,?0,1)
??--cpuset-mems?string???mems?in?which?to?allow?execution?(0-3,?0,1)
??--device-read-bps?value??limit?read?rate?(bytes?per?second)?from?a?device?(default?[])
??--device-read-iops?value??limit?read?rate?(io?per?second)?from?a?device?(default?[])
??--device-write-bps?value??limit?write?rate?(bytes?per?second)?to?a?device?(default?[])
??--device-write-iops?value??limit?write?rate?(io?per?second)?to?a?device?(default?[])
??--ipc?string?????ipc?namespace?to?use
?-m,?--memory?string????memory?limit
??--memory-reservation?string?memory?soft?limit
??--memory-swap?string???swap?limit?equal?to?memory?plus?swap:?'-1'?to?enable?unlimited?swap
??--memory-swappiness?int??tune?container?memory?swappiness?(0?to?100)?(default?-1)
??--kernel-memory?string??kernel?memory?limit
?-u,?--user?string?????username?or?uid?(format:?<name|uid>[:<group|gid>])
??--userns?string????user?namespace?to?use
??--uts?string?????uts?namespace?to?use
?-h,?--hostname?string????container?host?name
??--pid?string?????pid?namespace?to?use
??--pids-limit?int????tune?container?pids?limit?(set?-1?for?unlimited)
??--isolation?string???container?isolation?technology
??--io-maxbandwidth?string??maximum?io?bandwidth?limit?for?the?system?drive?(windows?only)
??--io-maxiops?uint????maximum?iops?limit?for?the?system?drive?(windows?only)
?
?linux?process?capabilities?相關參數:
??--cap-add?value????add?linux?capabilities?(default?[])
??--cap-drop?value????drop?linux?capabilities?(default?[])
?
?容器運行模式和環境相關:
?-d,?--detach??????run?container?in?background?and?print?container?id
?-e,?--env?value?????set?environment?variables?(default?[])
??--env-file?value????read?in?a?file?of?environment?variables?(default?[])
?
?dns?相關:
??--dns?value?????set?custom?dns?servers?(default?[])
??--dns-opt?value????set?dns?options?(default?[])
??--dns-search?value???set?custom?dns?search?domains?(default?[])
?
?健康檢查相關:
??--health-cmd?string???command?to?run?to?check?health
??--health-interval?duration?time?between?running?the?check
??--health-retries?int???consecutive?failures?needed?to?report?unhealthy
??--health-timeout?duration??maximum?time?to?allow?one?check?to?run
??--no-healthcheck????disable?any?container-specified?healthcheck
??
?ip?和端口:
??--ip?string?????container?ipv4?address?(e.g.?172.30.100.104)
??--ip6?string?????container?ipv6?address?(e.g.?2001:db8::33)
?-p,?--publish?value????publish?a?container's?port(s)?to?the?host?(default?[])
?-p,?--publish-all?????publish?all?exposed?ports?to?random?ports
??--expose?value????expose?a?port?or?a?range?of?ports?(default?[])
??--mac-address?string???container?mac?address?(e.g.?92:d0:c6:0a:29:33)
??--add-host?value????add?a?custom?host-to-ip?mapping?(host:ip)?(default?[])
??
?volume?相關:
?-v,?--volume?value????bind?mount?a?volume?(default?[])
??--volume-driver?string??optional?volume?driver?for?the?container
??--volumes-from?value???mount?volumes?from?the?specified?container(s)?(default?[])
??--storage-opt?value???storage?driver?options?for?the?container?(default?[])
?
?network?有關:
??--network?string????connect?a?container?to?a?network?(default?"default")
??--network-alias?value???add?network-scoped?alias?for?the?container?(default?[])
??--link?value?????add?link?to?another?container?(default?[])
??--link-local-ip?value???container?ipv4/ipv6?link-local?addresses?(default?[])
?
?日志有關:
??--log-driver?string???logging?driver?for?the?container
??--log-opt?value????log?driver?options?(default?[])
?
?交互性有關:
?-a,?--attach?value????attach?to?stdin,?stdout?or?stderr?(default?[])
?-i,?--interactive?????keep?stdin?open?even?if?not?attached
?
?oom?有關:?
??--oom-kill-disable???disable?oom?killer
??--oom-score-adj?int???tune?host's?oom?preferences?(-1000?to?1000)
?
?其它(待更進一步分類):
??--cidfile?string????write?the?container?id?to?the?file
??--detach-keys?string???override?the?key?sequence?for?detaching?a?container
??--device?value????add?a?host?device?to?the?container?(default?[])
??--disable-content-trust??skip?image?verification?(default?true)
??--entrypoint?string???overwrite?the?default?entrypoint?of?the?image
??--group-add?value????add?additional?groups?to?join?(default?[])
??--help??????print?usage
?-l,?--label?value?????set?meta?data?on?a?container?(default?[])
??--label-file?value???read?in?a?line?delimited?file?of?labels?(default?[])
??--name?string?????assign?a?name?to?the?container
??--privileged?????give?extended?privileges?to?this?container
??--read-only?????mount?the?container's?root?filesystem?as?read?only
??--restart?string????restart?policy?to?apply?when?a?container?exits?(default?"no")
??--rm???????automatically?remove?the?container?when?it?exits
??--runtime?string????runtime?to?use?for?this?container
??--security-opt?value???security?options?(default?[])
??--shm-size?string????size?of?/dev/shm,?default?value?is?64mb
??--sig-proxy?????proxy?received?signals?to?the?process?(default?true)
??--stop-signal?string???signal?to?stop?a?container,?sigterm?by?default?(default?"sigterm")
??--sysctl?value????sysctl?options?(default?map[])
??--tmpfs?value?????mount?a?tmpfs?directory?(default?[])
?-t,?--tty???????allocate?a?pseudo-tty
??--ulimit?value????ulimit?options?(default?[])
?-w,?--workdir?string????working?directory?inside?the?container

具體的內容以后會有專門文件分析。

3. doker 平臺的基本構成Docker安裝和基礎用法實例分析  docker 第3張
docker 平臺基本上由三部分組成:

  • 客戶端:用戶使用 docker 提供的工具(cli 以及 api 等)來構建,上傳鏡像并發布命令來創建和啟動容器

  • docker 主機:從 docker registry 上下載鏡像并啟動容器

  • docker registry:docker 鏡像倉庫,用于保存鏡像,并提供鏡像上傳和下載

讀到這里,這篇“Docker安裝和基礎用法實例分析”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注蝸牛博客行業資訊頻道。

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:niceseo99@gmail.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

評論

日本韩欧美一级A片在线观看