最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

理解Docker(1):Docker安裝和基礎(chǔ)用法詳細(xì)介紹

 更新時(shí)間:2016年11月11日 09:17:07   作者:SammyLiu  
本篇文章主要是介紹了Docker 安裝和基礎(chǔ)用法,對(duì)于想要需要的Docker的同學(xué)可以了解一下。

Docker是一個(gè)用了一種新穎方式實(shí)現(xiàn)的超輕量虛擬機(jī),在實(shí)現(xiàn)的原理和應(yīng)用上還是和VM有巨大差別,專(zhuān)業(yè)的叫法是應(yīng)用容器(Application Container)。(我個(gè)人還是喜歡稱(chēng)虛擬機(jī))

1. 安裝

1.1 在 Ubuntu 14.04 上安裝 Docker

前提要求:

內(nèi)核版本必須是3.10或者以上

依次執(zhí)行下面的步驟:

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

至此,安裝過(guò)程完成。

運(yùn)行 sudo service docker start 啟動(dòng) Docker 守護(hù)進(jìn)程。

運(yùn)行 docker version 查看 Docker 版本

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

啟動(dòng)第一個(gè)容器:

啟動(dòng)第一個(gè)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.

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

以上內(nèi)容參考自 Docker 官網(wǎng):https://docs.docker.com/engine/installation/linux/ubuntulinux/

1.2 Docker 到目前(2016/09/16)為止的版本歷史

2. Docker 的基本操作

2.1 Docker 容器的狀態(tài)機(jī)

一個(gè)容器在某個(gè)時(shí)刻可能處于以下幾種狀態(tài)之一:

  • created:已經(jīng)被創(chuàng)建 (使用 docker ps -a 命令可以列出)但是還沒(méi)有被啟動(dòng) (使用 docker ps 命令還無(wú)法列出)
  • running:運(yùn)行中
  • paused:容器的進(jìn)程被暫停了
  • restarting:容器的進(jìn)程正在重啟過(guò)程中
  • exited:上圖中的 stopped 狀態(tài),表示容器之前運(yùn)行過(guò)但是現(xiàn)在處于停止?fàn)顟B(tài)(要區(qū)別于 created 狀態(tài),它是指一個(gè)新創(chuàng)出的尚未運(yùn)行過(guò)的容器)??梢酝ㄟ^(guò) start 命令使其重新進(jìn)入 running 狀態(tài)
  • destroyed:容器被刪除了,再也不存在了

你可以在 docker inspect 命令的輸出中查看其詳細(xì)狀態(tài):

"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 的命令大概地分類(lèi)如下:

鏡像操作:
 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 獲取鏡像的詳細(xì)信息

 容器及其中應(yīng)用的生命周期操作:
 create Create a new container (創(chuàng)建一個(gè)容器)  
 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 (刪除一個(gè)或者多個(gè)容器)
 rename Rename a container
 restart Restart a container
 run  Run a command in a new container (創(chuàng)建并啟動(dòng)一個(gè)容器)
 start  Start one or more stopped containers (啟動(dòng)一個(gè)處于停止?fàn)顟B(tài)的容器)
 stats  Display a live stream of container(s) resource usage statistics (顯示容器實(shí)時(shí)的資源消耗信息)
 stop  Stop one or more running containers (停止一個(gè)處于運(yùn)行狀態(tài)的容器)
 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  獲取容器的日志 
 
 容器文件系統(tǒng)操作:
 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
 
 網(wǎng)絡(luò)操作
 network Manage Docker networks
 
 Swarm 相關(guān)操作
 swarm  Manage Docker Swarm
 service Manage Docker services
 node  Manage Docker Swarm nodes  
 
 系統(tǒng)操作: 
 version Show the Docker version information
 events Get real time events from the server (持續(xù)返回docker 事件)
 info  Display system-wide information (顯示Docker 主機(jī)系統(tǒng)范圍內(nèi)的信息)

比較有意思的幾個(gè)命令:

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

root@devstack:/home/sammy# docker create --name web31 training/webapp python app.py #創(chuàng)建名字為 web31 的容器
7465f4cb7c49555af32929bd1bc4213f5e72643c0116450e495b71c7ec128502
root@devstack:/home/sammy# docker inspect --format='{{.State.Status}}' web31 #其狀態(tài)為 created
created
root@devstack:/home/sammy# docker start web31 #啟動(dòng)容器
web31
root@devstack:/home/sammy# docker inspect --format='{{.State.Status}}' web31 #其狀態(tài)為 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 #繼續(xù)容器
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 #在容器中運(yùn)行 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 命令執(zhí)行的時(shí)候,會(huì)先向容器中PID為1的進(jìn)程發(fā)送系統(tǒng)信號(hào) SIGTERM,然后等待容器中的應(yīng)用程序終止執(zhí)行,如果等待時(shí)間達(dá)到設(shè)定的超時(shí)時(shí)間(默認(rèn)為 10秒,用戶可以指定特定超時(shí)時(shí)長(zhǎng)),會(huì)繼續(xù)發(fā)送SIGKILL的系統(tǒng)信號(hào)強(qiáng)行kill掉進(jìn)程。在容器中的應(yīng)用程序,可以選擇忽略和不處理SIGTERM信號(hào),不過(guò)一旦達(dá)到超時(shí)時(shí)間,程序就會(huì)被系統(tǒng)強(qiáng)行kill掉,因?yàn)镾IGKILL信號(hào)是直接發(fā)往系統(tǒng)內(nèi)核的,應(yīng)用程序沒(méi)有機(jī)會(huì)去處理它。

比如運(yùn)行 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 向容器發(fā)出 SIGTERM 信號(hào)(signal=15)
  2. 等待20秒 (01:18 到 01:38)
  3. 再發(fā)送 SIGKILL 系統(tǒng)信號(hào) (signal = 9)
  4. 然后容器被殺掉了 (die)

而 docker kill 命令會(huì)直接發(fā)出SIGKILL的系統(tǒng)信號(hào),以強(qiáng)行終止容器中程序的運(yùn)行。運(yùn)行 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)
可見(jiàn)直接發(fā)出的是 SIGKILL 信號(hào),容器立馬就被殺掉了。

(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:將一個(gè)容器的文件系統(tǒng)打包為一個(gè)壓縮文件

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

docker import:從一個(gè)壓縮文件創(chuàng)建一個(gè)鏡像

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 命令會(huì)創(chuàng)建一個(gè)容器并啟動(dòng)它,它也是包含很多的參數(shù),按照用途將它們分類(lèi)如下:

cgroups 和 namespace 相關(guān):
  --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 相關(guān)參數(shù):
  --cap-add value    Add Linux capabilities (default [])
  --cap-drop value    Drop Linux capabilities (default [])
 
 容器運(yùn)行模式和環(huán)境相關(guān):
 -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 相關(guān):
  --dns value     Set custom DNS servers (default [])
  --dns-opt value    Set DNS options (default [])
  --dns-search value   Set custom DNS search domains (default [])
 
 健康檢查相關(guān):
  --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 相關(guān):
 -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 有關(guān):
  --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 [])
 
 日志有關(guān):
  --log-driver string   Logging driver for the container
  --log-opt value    Log driver options (default [])
 
 交互性有關(guān):
 -a, --attach value    Attach to STDIN, STDOUT or STDERR (default [])
 -i, --interactive     Keep STDIN open even if not attached
 
 OOM 有關(guān): 
  --oom-kill-disable   Disable OOM Killer
  --oom-score-adj int   Tune host's OOM preferences (-1000 to 1000)
 
 其它(待更進(jìn)一步分類(lèi)):
  --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

具體的內(nèi)容以后會(huì)有專(zhuān)門(mén)文件分析。

3. Doker 平臺(tái)的基本構(gòu)成
Docker 平臺(tái)基本上由三部分組成:

  • 客戶端:用戶使用 Docker 提供的工具(CLI 以及 API 等)來(lái)構(gòu)建,上傳鏡像并發(fā)布命令來(lái)創(chuàng)建和啟動(dòng)容器
  • Docker 主機(jī):從 Docker registry 上下載鏡像并啟動(dòng)容器
  • Docker registry:Docker 鏡像倉(cāng)庫(kù),用于保存鏡像,并提供鏡像上傳和下載

后面的文章會(huì)具體分析。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • docker之安裝nginx的教程

    docker之安裝nginx的教程

    這篇文章主要介紹了docker之安裝nginx的教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Docker部署SpringBoot項(xiàng)目的實(shí)現(xiàn)步驟

    Docker部署SpringBoot項(xiàng)目的實(shí)現(xiàn)步驟

    本文主要介紹了Docker部署SpringBoot項(xiàng)目的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2007-02-02
  • Docker容器互相連接三種實(shí)現(xiàn)方法詳解

    Docker容器互相連接三種實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Docker容器互連三種實(shí)現(xiàn)方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Docker 架構(gòu)簡(jiǎn)介

    Docker 架構(gòu)簡(jiǎn)介

    這篇文章主要介紹了Docker 架構(gòu)的相關(guān)資料,文中講解非常細(xì)致,幫助大家開(kāi)始學(xué)習(xí)docker,感興趣的朋友可以了解下
    2020-07-07
  • SpringBoot 整合 Docker的詳細(xì)過(guò)程

    SpringBoot 整合 Docker的詳細(xì)過(guò)程

    這篇文章主要介紹了SpringBoot 整合 Docker的詳細(xì)過(guò)程,本文通過(guò)一個(gè)簡(jiǎn)單的項(xiàng)目來(lái)給大家介紹整合的詳細(xì)過(guò)程,需要的朋友可以參考下
    2021-11-11
  • docker安裝并持久化postgresql數(shù)據(jù)庫(kù)的操作步驟

    docker安裝并持久化postgresql數(shù)據(jù)庫(kù)的操作步驟

    這篇文章主要介紹了docker安裝并持久化postgresql數(shù)據(jù)庫(kù)的操作步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • docker部署OceanBase-ce cluster方式

    docker部署OceanBase-ce cluster方式

    這篇文章主要介紹了docker部署OceanBase-ce cluster方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Docker容器服務(wù)編排利器詳解

    Docker容器服務(wù)編排利器詳解

    這篇文章主要介紹了Docker容器服務(wù)編排利器,主要包括使用使用Docker?Compose必要性及定義及一些Docker?Compose應(yīng)用參考資料,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • docker容器commit打包越來(lái)越大的原因分析及解決

    docker容器commit打包越來(lái)越大的原因分析及解決

    文章介紹了Docker容器打包變大的原因,并提供了解決方法,具體步驟包括使用export命令導(dǎo)出容器,然后使用import命令導(dǎo)入鏡像,最后將鏡像展開(kāi)成容器,這種方法可以有效減少鏡像文件的大小,避免因Docker鏡像層的概念導(dǎo)致的文件膨脹問(wèn)題
    2025-03-03
  • Docker鏡像發(fā)布到Docker?Hub的實(shí)現(xiàn)方法

    Docker鏡像發(fā)布到Docker?Hub的實(shí)現(xiàn)方法

    本文主要介紹了Docker鏡像發(fā)布到Docker?Hub的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01

最新評(píng)論

汝州市| 鄂州市| 鹤岗市| 澳门| 东乡县| 台东县| 武隆县| 前郭尔| 惠水县| 平顺县| 通化市| 平定县| 德安县| 扬州市| 嘉善县| 紫云| 女性| 平泉县| 仁化县| 阳春市| 烟台市| 大宁县| 龙州县| 大同市| 新宁县| 醴陵市| 庆云县| 即墨市| 富源县| 南皮县| 青龙| 松江区| 龙陵县| 克拉玛依市| 渝中区| 赣榆县| 邵阳市| 彭州市| 云阳县| 安塞县| 大化|