Docker-Linux-01

一、Docker 簡介

Docker 兩個主要部件:
Docker: 開源的容器虛擬化平臺
Docker Hub: 用于分享、管理 Docker 容器的 Docker SaaS 平臺 -- Docker Hub

Docker 結構

Docker 使用客戶端-服務器 (C/S) 架構模式。Docker 客戶端會與 Docker 守護進程進行通信。Docker 守護進程會處理復雜繁重的任務,例如建立、運行、發布你的 Docker 容器。Docker 客戶端和守護進程可以運行在同一個系統上,當然你也可以使用 Docker 客戶端去連接一個遠程的 Docker 守護進程。Docker 客戶端和守護進程之間通過 socket 或者 RESTful API 進行通信。

1.1 Docker 守護進程

如上圖所示,Docker 守護進程運行在一臺主機上。用戶并不直接和守護進程進行交互,而是通過 Docker 客戶端間接和其通信。

1.2 Docker 客戶端

Docker 客戶端,實際上是 docker 的二進制程序,是主要的用戶與 Docker 交互方式。它接收用戶指令并且與背后的 Docker 守護進程通信,如此來回往復。

1.3 Docker 內部

要理解 Docker 內部構建,需要理解以下三種部件:
Docker 鏡像 - Docker images
Docker 倉庫 - Docker registeries
Docker 容器 - Docker containers

Docker 鏡像

Docker 鏡像是 Docker 容器運行時的只讀模板,每一個鏡像由一系列的層 (layers) 組成。Docker 使用 UnionFS 來將這些層聯合到單獨的鏡像中。UnionFS 允許獨立文件系統中的文件和文件夾(稱之為分支)被透明覆蓋,形成一個單獨連貫的文件系統。正因為有了這些層的存在,Docker 是如此的輕量。當你改變了一個 Docker 鏡像,比如升級到某個程序到新的版本,一個新的層會被創建。因此,不用替換整個原先的鏡像或者重新建立(在使用虛擬機的時候你可能會這么做),只是一個新 的層被添加或升級了。現在你不用重新發布整個鏡像,只需要升級,層使得分發 Docker 鏡像變得簡單和快速。

Docker 倉庫

Docker 倉庫用來保存鏡像,可以理解為代碼控制中的代碼倉庫。同樣的,Docker 倉庫也有公有和私有的概念。公有的 Docker 倉庫名字是 Docker Hub。Docker Hub 提供了龐大的鏡像集合供使用。這些鏡像可以是自己創建,或者在別人的鏡像基礎上創建。Docker 倉庫是 Docker 的分發部分。

Docker 容器

Docker 容器和文件夾很類似,一個Docker容器包含了所有的某個應用運行所需要的環境。每一個 Docker 容器都是從 Docker 鏡像創建的。Docker 容器可以運行、開始、停止、移動和刪除。每一個 Docker 容器都是獨立和安全的應用平臺,Docker 容器是 Docker 的運行部分。

1.4 libcontainer

Docker 從 0.9 版本開始使用 libcontainer 替代 lxc,libcontainer 和 Linux 系統的交互圖如下:

圖片(2)

圖片來源: Docker 0.9: introducing execution drivers and libcontainer

1.5 命名空間「Namespaces」

pid namespace

不同用戶的進程就是通過 pid namespace 隔離開的,且不同 namespace 中可以有相同 PID。具有以下特征:
每個 namespace 中的 pid 是有自己的 pid=1 的進程(類似 /sbin/init 進程)
每個 namespace 中的進程只能影響自己的同一個 namespace 或子 namespace 中的進程
因為 /proc 包含正在運行的進程,因此在 container 中的 pseudo-filesystem 的 /proc 目錄只能看到自己 namespace 中的進程
因為 namespace 允許嵌套,父 namespace 可以影響子 namespace 的進程,所以子 namespace 的進程可以在父 namespace 中看到,但是具有不同的 pid

參考文檔:Introduction to Linux namespaces – Part 3: PID

mnt namespace

類似 chroot,將一個進程放到一個特定的目錄執行。mnt namespace 允許不同 namespace 的進程看到的文件結構不同,這樣每個 namespace 中的進程所看到的文件目錄就被隔離開了。同 chroot 不同,每個 namespace 中的 Container 在 /proc/mounts 的信息只包含所在 namespace 的 mount point。

net namespace

網絡隔離是通過 net namespace 實現的, 每個 net namespace 有獨立的 network devices, IP addresses, IP routing tables, /proc/net 目錄。這樣每個 container 的網絡就能隔離開來。 docker 默認采用 veth 的方式將 container 中的虛擬網卡同 host 上的一個 docker bridge 連接在一起。
參考文檔:Introduction to Linux namespaces – Part 5: NET

uts namespace

UTS ("UNIX Time-sharing System") namespace 允許每個 container 擁有獨立的 hostname 和 domain name, 使其在網絡上可以被視作一個獨立的節點而非 Host 上的一個進程。
參考文檔:Introduction to Linux namespaces – Part 1: UTS

ipc namespace

container 中進程交互還是采用 Linux 常見的進程間交互方法 (interprocess communication - IPC), 包括常見的信號量、消息隊列和共享內存。然而同 VM 不同,container 的進程間交互實際上還是 host 上具有相同 pid namespace 中的進程間交互,因此需要在IPC資源申請時加入 namespace 信息 - 每個 IPC 資源有一個唯一的 32bit ID。
參考文檔:Introduction to Linux namespaces – Part 2: IPC

user namespace

每個 container 可以有不同的 user 和 group id, 也就是說可以以 container 內部的用戶在 container 內部執行程序而非 Host 上的用戶。
有了以上 6 種 namespace 從進程、網絡、IPC、文件系統、UTS 和用戶角度的隔離,一個 container 就可以對外展現出一個獨立計算機的能力,并且不同 container 從 OS 層面實現了隔離。 然而不同 namespace 之間資源還是相互競爭的,仍然需要類似 ulimit 來管理每個 container 所能使用的資源 - cgroup。
Reference
Docker Getting Start: Related Knowledge
Docker 介紹以及其相關術語、底層原理和技術

1.6 資源配額「cgroups」

cgroups 實現了對資源的配額和度量。

cgroups 的使用非常簡單,提供類似文件的接口,在 /cgroup 目錄下新建一個文件夾即可新建一個 group,在此文件夾中新建 task 文件,并將 pid 寫入該文件,即可實現對該進程的資源控制。具體的資源配置選項可以在該文件夾中新建子 subsystem ,{子系統前綴}.{資源項} 是典型的配置方法,如 memory.usageinbytes 就定義了該 group 在 subsystem memory 中的一個內存限制選項。 另外,cgroups 中的 subsystem 可以隨意組合,一個 subsystem 可以在不同的 group 中,也可以一個 group 包含多個 subsystem - 也就是說一個 subsystem。

memory內存相關的限制

cpu在 cgroup 中,并不能像硬件虛擬化方案一樣能夠定義 CPU 能力,但是能夠定義 CPU 輪轉的優先級,因此具有較高 CPU 優先級的進程會更可能得到 CPU 運算。 通過將參數寫入 cpu.shares ,即可定義改 cgroup 的 CPU 優先級 - 這里是一個相對權重,而非絕對值

blkioblock

IO 相關的統計和限制,byte/operation 統計和限制 (IOPS 等),讀寫速度限制等,但是這里主要統計的都是同步 IO

devices設備權限限制

參考文檔:how to use cgroup

二、Docker 安裝

docker 的相關安裝方法這里不作介紹,具體安裝參考 官檔
獲取當前 docker 版本

$ sudo docker versionClient 
version: 1.3.2Client API
 version: 1.15Go 
version (client): go1.3.3Git 
commit (client): 39fa2fa/1.3.2OS/Arch (client): linux/amd64Server version: 1.3.2Server API version: 1.15Go version (server): go1.3.3Git commit (server): 39fa2fa/1.3.2

三、Docker 基礎用法

Docker HUB : Docker鏡像首頁,包括官方鏡像和其它公開鏡像
因為國情的原因,國內下載 Docker HUB 官方的相關鏡像比較慢,可以使用 docker.cn 鏡像,鏡像保持和官方一致,關鍵是速度塊,推薦使用。
3.1 Search images

$ sudo docker search ubuntu

3.2 Pull images

$ sudo docker pull ubuntu 

獲取 ubuntu 官方鏡像

 $ sudo docker images 

查看當前鏡像列表

3.3 Running an interactive shell

$ sudo docker run -i -t ubuntu:14.04 /bin/bash

docker run - 運行一個容器
-t - 分配一個(偽)tty (link is external)
-i - 交互模式 (so we can interact with it)
ubuntu:14.04 - 使用 ubuntu 基礎鏡像 14.04
/bin/bash - 運行命令 bash shell

注: ubuntu 會有多個版本,通過指定 tag 來啟動特定的版本 [image]:[tag]
$ sudo docker ps # 查看當前運行的容器, ps -a 列出當前系統所有的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES6c9129e9df10 ubuntu:14.04 /bin/bash 6 minutes ago Up 6 minutes cranky_babbage

3.4 相關快捷鍵
退出:Ctrl-Dorexit
detach:Ctrl-P + Ctrl-Q
attach:docker attach CONTAINER-ID

四、Docker 命令幫助

4.1 docker help
docker command
$ sudo docker # docker 命令幫助Commands: attach Attach to a running container # 當前 shell 下 attach 連接指定運行鏡像 build Build an image from a Dockerfile # 通過 Dockerfile 定制鏡像 commit Create a new image from a container's changes # 提交當前容器為新的鏡像 cp Copy files/folders from the containers filesystem to the host path # 從容器中拷貝指定文件或者目錄到宿主機中 create Create a new container # 創建一個新的容器,同 run,但不啟動容器 diff Inspect changes on a container's filesystem # 查看 docker 容器變化 events Get real time events from the server # 從 docker 服務獲取容器實時事件 exec Run a command in an existing container # 在已存在的容器上運行命令 export Stream the contents of a container as a tar archive # 導出容器的內容流作為一個 tar 歸檔文件[對應 import ] history Show the history of an image # 展示一個鏡像形成歷史 images List images # 列出系統當前鏡像 import Create a new filesystem image from the contents of a tarball # 從tar包中的內容創建一個新的文件系統映像[對應 export] info Display system-wide information # 顯示系統相關信息 inspect Return low-level information on a container # 查看容器詳細信息 kill Kill a running container # kill 指定 docker 容器 load Load an image from a tar archive # 從一個 tar 包中加載一個鏡像[對應 save] login Register or Login to the docker registry server # 注冊或者登陸一個 docker 源服務器 logout Log out from a Docker registry server # 從當前 Docker registry 退出 logs Fetch the logs of a container # 輸出當前容器日志信息 port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口對應的容器內部源端口 pause Pause all processes within a container # 暫停容器 ps List containers # 列出容器列表 pull Pull an image or a repository from the docker registry server # 從docker鏡像源服務器拉取指定鏡像或者庫鏡像 push Push an image or a repository to the docker registry server # 推送指定鏡像或者庫鏡像至docker源服務器 restart Restart a running container # 重啟運行的容器 rm Remove one or more containers # 移除一個或者多個容器 rmi Remove one or more images # 移除一個或多個鏡像[無容器使用該鏡像才可刪除,否則需刪除相關容器才可繼續或 -f 強制刪除] run Run a command in a new container # 創建一個新的容器并運行一個命令 save Save an image to a tar archive # 保存一個鏡像為一個 tar 包[對應 load] search Search for an image on the Docker Hub # 在 docker hub 中搜索鏡像 start Start a stopped containers # 啟動容器 stop Stop a running containers # 停止容器 tag Tag an image into a repository # 給源中鏡像打標簽 top Lookup the running processes of a container # 查看容器中運行的進程信息 unpause Unpause a paused container # 取消暫停容器 version Show the docker version information # 查看 docker 版本號 wait Block until a container stops, then print its exit code # 截取容器停止時的退出狀態值Run 'docker COMMAND --help' for more information on a command.

docker option
Usage of docker: --api-enable-cors=false Enable CORS headers in the remote API # 遠程 API 中開啟 CORS 頭 -b, --bridge="" Attach containers to a pre-existing network bridge # 橋接網絡 use 'none' to disable container networking --bip="" Use this CIDR notation address for the network bridge's IP, not compatible with -b # 和 -b 選項不兼容,具體沒有測試過 -d, --daemon=false Enable daemon mode # daemon 模式 -D, --debug=false Enable debug mode # debug 模式 --dns=[] Force docker to use specific DNS servers # 強制 docker 使用指定 dns 服務器 --dns-search=[] Force Docker to use specific DNS search domains # 強制 docker 使用指定 dns 搜索域 -e, --exec-driver="native" Force the docker runtime to use a specific exec driver # 強制 docker 運行時使用指定執行驅動器 --fixed-cidr="" IPv4 subnet for fixed IPs (ex: 10.20.0.0/16) this subnet must be nested in the bridge subnet (which is defined by -b or --bip) -G, --group="docker" Group to assign the unix socket specified by -H when running in daemon mode use '' (the empty string) to disable setting of a group -g, --graph="/var/lib/docker" Path to use as the root of the docker runtime # 容器運行的根目錄路徑 -H, --host=[] The socket(s) to bind to in daemon mode # daemon 模式下 docker 指定綁定方式[tcp or 本地 socket] specified using one or more tcp://host:port, unix:///path/to/socket, fd://* or fd://socketfd. --icc=true Enable inter-container communication # 跨容器通信 --insecure-registry=[] Enable insecure communication with specified registries (no certificate verification for HTTPS and enable HTTP fallback) (e.g., localhost:5000 or 10.20.0.0/16) --ip="0.0.0.0" Default IP address to use when binding container ports # 指定監聽地址,默認所有 ip --ip-forward=true Enable net.ipv4.ip_forward # 開啟轉發 --ip-masq=true Enable IP masquerading for bridge's IP range --iptables=true Enable Docker's addition of iptables rules # 添加對應 iptables 規則 --mtu=0 Set the containers network MTU # 設置網絡 mtu if no value is provided: default to the default route MTU or 1500 if no default route is available -p, --pidfile="/var/run/docker.pid" Path to use for daemon PID file # 指定 pid 文件位置 --registry-mirror=[] Specify a preferred Docker registry mirror -s, --storage-driver="" Force the docker runtime to use a specific storage driver # 強制 docker 運行時使用指定存儲驅動 --selinux-enabled=false Enable selinux support # 開啟 selinux 支持 --storage-opt=[] Set storage driver options # 設置存儲驅動選項 --tls=false Use TLS; implied by tls-verify flags # 開啟 tls --tlscacert="/root/.docker/ca.pem" Trust only remotes providing a certificate signed by the CA given here --tlscert="/root/.docker/cert.pem" Path to TLS certificate file # tls 證書文件位置 --tlskey="/root/.docker/key.pem" Path to TLS key file # tls key 文件位置 --tlsverify=false Use TLS and verify the remote (daemon: verify client, client: verify daemon) # 使用 tls 并確認遠程控制主機 -v, --version=false Print version information and quit # 輸出 docker 版本信息

4.2 docker search
$ sudo docker search --helpUsage: docker search TERMSearch the Docker Hub for images # 從 Docker Hub 搜索鏡像 --automated=false Only show automated builds --no-trunc=false Don't truncate output -s, --stars=0 Only displays with at least xxx stars

示例:
$ sudo docker search -s 100 ubuntu # 查找 star 數至少為 100 的鏡像,找出只有官方鏡像 start 數超過 100,默認不加 s 選項找出所有相關 ubuntu 鏡像 NAME DESCRIPTION STARS OFFICIAL AUTOMATEDubuntu Official Ubuntu base image 425 [OK]

4.3 docker info
$ sudo docker info Containers: 1 # 容器個數 Images: 22 # 鏡像個數 Storage Driver: devicemapper # 存儲驅動 Pool Name: docker-8:17-3221225728-pool Pool Blocksize: 65.54 kB Data file: /data/docker/devicemapper/devicemapper/data Metadata file: /data/docker/devicemapper/devicemapper/metadata Data Space Used: 1.83 GB Data Space Total: 107.4 GB Metadata Space Used: 2.191 MB Metadata Space Total: 2.147 GB Library Version: 1.02.84-RHEL7 (2014-03-26) Execution Driver: native-0.2 # 存儲驅動 Kernel Version: 3.10.0-123.el7.x86_64Operating System: CentOS Linux 7 (Core)

4.4 docker pull && docker push
$ sudo docker pull --help # pull 拉取鏡像 Usage: docker pull [OPTIONS] NAME[:TAG] Pull an image or a repository from the registry -a, --all-tags=false Download all tagged images in the repository $ sudo docker push # push 推送指定鏡像 Usage: docker push NAME[:TAG] Push an image or a repository to the registry

示例:
$ sudo docker pull ubuntu # 下載官方 ubuntu docker 鏡像,默認下載所有 ubuntu 官方庫鏡像 $ sudo docker pull ubuntu:14.04 # 下載指定版本 ubuntu 官方鏡像

$ sudo docker push 192.168.0.100:5000/ubuntu # 推送鏡像庫到私有源[可注冊 docker 官方賬戶,推送到官方自有賬戶] $ sudo docker push 192.168.0.100:5000/ubuntu:14.04 # 推送指定鏡像到私有源

4.5 docker images
列出當前系統鏡像
$ sudo docker images --helpUsage: docker images [OPTIONS] [NAME] List images -a, --all=false Show all images (by default filter out the intermediate image layers) # -a 顯示當前系統的所有鏡像,包括過渡層鏡像,默認 docker images 顯示最終鏡像,不包括過渡層鏡像 -f, --filter=[] Provide filter values (i.e. 'dangling=true') --no-trunc=false Don't truncate output -q, --quiet=false Only show numeric IDs

示例:
$ sudo docker images # 顯示當前系統鏡像,不包括過渡層鏡像 $ sudo docker images -a # 顯示當前系統所有鏡像,包括過渡層鏡像 $ sudo docker images ubuntu # 顯示當前系統 docker ubuntu 庫中的所有鏡像 REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEubuntu 12.04 ebe4be4dd427 4 weeks ago 210.6 MBubuntu 14.04 e54ca5efa2e9 4 weeks ago 276.5 MBubuntu 14.04-ssh 6334d3ac099a 7 weeks ago 383.2 MB

4.6 docker rmi
刪除一個或者多個鏡像
$ sudo docker rmi --helpUsage: docker rmi IMAGE [IMAGE...] Remove one or more images -f, --force=false Force removal of the image # 強制移除鏡像不管是否有容器使用該鏡像 --no-prune=false Do not delete untagged parents # 不要刪除未標記的父鏡像

4.7 docker run
$ sudo docker run --helpUsage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] Run a command in a new container -a, --attach=[] Attach to stdin, stdout or stderr. -c, --cpu-shares=0 CPU shares (relative weight) # 設置 cpu 使用權重 --cap-add=[] Add Linux capabilities --cap-drop=[] Drop Linux capabilities --cidfile="" Write the container ID to the file # 把容器 id 寫入到指定文件 --cpuset="" CPUs in which to allow execution (0-3, 0,1) # cpu 綁定 -d, --detach=false Detached mode: Run container in the background, print new container id # 后臺運行容器 --device=[] Add a host device to the container (e.g. --device=/dev/sdc:/dev/xvdc) --dns=[] Set custom dns servers # 設置 dns --dns-search=[] Set custom dns search domains # 設置 dns 域搜索 -e, --env=[] Set environment variables # 定義環境變量 --entrypoint="" Overwrite the default entrypoint of the image # ? --env-file=[] Read in a line delimited file of ENV variables # 從指定文件讀取變量值 --expose=[] Expose a port from the container without publishing it to your host # 指定對外提供服務端口 -h, --hostname="" Container host name # 設置容器主機名 -i, --interactive=false Keep stdin open even if not attached # 保持標準輸出開啟即使沒有 attached --link=[] Add link to another container (name:alias) # 添加鏈接到另外一個容器 --lxc-conf=[] (lxc exec-driver only) Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1" -m, --memory="" Memory limit (format: <number><optional unit>, where unit = b, k, m or g) # 內存限制 --name="" Assign a name to the container # 設置容器名 --net="bridge" Set the Network mode for the container # 設置容器網絡模式 'bridge': creates a new network stack for the container on the docker bridge 'none': no networking for this container 'container:<name|id>': reuses another container network stack 'host': use the host network stack inside the container. Note: the host mode gives the container full access to local system services such as D-bus and is therefore considered insecure. -P, --publish-all=false Publish all exposed ports to the host interfaces # 自動映射容器對外提供服務的端口 -p, --publish=[] Publish a container's port to the host # 指定端口映射 format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort (use 'docker port' to see the actual mapping) --privileged=false Give extended privileges to this container # 提供更多的權限給容器 --restart="" Restart policy to apply when a container exits (no, on-failure[:max-retry], always) --rm=false Automatically remove the container when it exits (incompatible with -d) # 如果容器退出自動移除和 -d 選項沖突 --security-opt=[] Security Options --sig-proxy=true Proxify received signals to the process (even in non-tty mode). SIGCHLD is not proxied. -t, --tty=false Allocate a pseudo-tty # 分配偽終端 -u, --user="" Username or UID # 指定運行容器的用戶 uid 或者用戶名 -v, --volume=[] Bind mount a volume (e.g., from the host: -v /host:/container, from docker: -v /container) # 掛載卷 --volumes-from=[] Mount volumes from the specified container(s) # 從指定容器掛載卷 -w, --workdir="" Working directory inside the container # 指定容器工作目錄

示例:
$ sudo docker images ubuntuREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEubuntu 14.04 e54ca5efa2e9 4 weeks ago 276.5 MB... ... $ sudo docker run -t -i -c 100 -m 512MB -h test1 -d --name="docker_test1" ubuntu /bin/bash # 創建一個 cpu 優先級為 100,內存限制 512MB,主機名為 test1,名為 docker_test1 后臺運行 bash 的容器 a424ca613c9f2247cd3ede95adfbaf8d28400cbcb1d5f9b69a7b56f97b2b52e5 $ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa424ca613c9f ubuntu:14.04 /bin/bash 6 seconds ago Up 5 seconds docker_test1 $ sudo docker attach docker_test1root@test1:/# pwd /root@test1:/# exit exit

關于cpu優先級:
By default all groups have 1024 shares. A group with 100 shares will get a ~10% portion of the CPU time -archlinux cgroups

4.8 docker start|stop|kill... ...
dockerstart|stop|kill|restart|pause|unpause|rm|commit|inspect|logs
docker start CONTAINER [CONTAINER...]# 運行一個或多個停止的容器

docker stop CONTAINER [CONTAINER...]# 停掉一個或多個運行的容器-t選項可指定超時時間

docker kill [OPTIONS] CONTAINER [CONTAINER...]# 默認 kill 發送 SIGKILL 信號-s可以指定發送 kill 信號類型

docker restart [OPTIONS] CONTAINER [CONTAINER...]# 重啟一個或多個運行的容器-t選項可指定超時時間

docker pause CONTAINER# 暫停一個容器,方便 commit

docker unpause CONTAINER# 繼續暫停的容器

docker rm [OPTIONS] CONTAINER [CONTAINER...]# 移除一個或多個容器
-f, --force=false Force removal of running container
-l, --link=false Remove the specified link and not the underlying container
-v, --volumes=false Remove the volumes associated with the container

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]# 提交指定容器為鏡像
-a, --author="" Author (e.g., "John Hannibal Smith hannibal@a-team.com")
-m, --message="" Commit message
-p, --pause=true Pause container during commit# 默認 commit 是暫停狀態

docker inspect CONTAINER|IMAGE [CONTAINER|IMAGE...]# 查看容器或者鏡像的詳細信息

docker logs CONTAINER# 輸出指定容器日志信息
-f, --follow=false Follow log output# 類似 tail -f

-t, --timestamps=false Show timestamps
--tail="all" Output the specified number of lines at the end of logs (defaults to all logs)

參考文檔:Docker Run Reference
4.9 Docker 1.3 新增特性和命令
Digital Signature Verification
Docker 1.3 版本將使用數字簽名自動驗證所有官方庫的來源和完整性,如果一個官方鏡像被篡改或者被破壞,目前 Docker 只會對這種情況發出警告而并不阻止容器的運行。
Inject new processes withdocker exec
docker exec --helpUsage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in an existing container -d, --detach=false Detached mode: run command in the background -i, --interactive=false Keep STDIN open even if not attached -t, --tty=false Allocate a pseudo-TTY

為了簡化調試,可以使用docker exec命令通過 Docker API 和 CLI 在運行的容器上運行程序。
$ docker exec -it ubuntu_bash bash

上例將在容器 ubuntu_bash 中創建一個新的 Bash 會話。
Tune container lifecycles withdocker create
我們可以通過docker run <image name>命令創建一個容器并運行其中的程序,因為有很多用戶要求創建容器的時候不啟動容器,所以docker create應運而生了。
$ docker create -t -i fedora bash6d8af538ec541dd581ebc2a24153a28329acb5268abe5ef868c1f1a261221752

上例創建了一個可寫的容器層 (并且打印出容器 ID),但是并不運行它,可以使用以下命令運行該容器:
$ docker start -a -i 6d8af538ec5bash-4.2#

Security Options
通過--security-opt選項,運行容器時用戶可自定義 SELinux 和 AppArmor 卷標和配置。
$ docker run --security-opt label:type:svirt_apache -i -t centos \ bash

上例只允許容器監聽在 Apache 端口,這個選項的好處是用戶不需要運行 docker 的時候指定--privileged選項,降低安全風險。
參考文檔:Docker 1.3: signed images, process injection, security options, Mac shared directories
4.10 Docker 1.5 新特性
參考文檔:Docker 1.5 新特性

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,316評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,481評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,241評論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,939評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,697評論 6 409
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,182評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,247評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,406評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,933評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,772評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,973評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,516評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,209評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,638評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,866評論 1 285
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,644評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,953評論 2 373

推薦閱讀更多精彩內容