$ sudo docker Commands: attach Attach to a running container --將終端依附到容器上 1> 運行一個交互型容器 [root@localhost ~]# docker run -i -t centos /bin/bash [root@f0a02b473067 /]# 2> 在另外一個窗口上查看該容器的狀態 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d4a75f165ce6 centos "/bin/bash" 5 seconds ago Up 5 seconds cranky_mahavira 3> 退出第一步中運行的容器 [root@d4a75f165ce6 /]# exit exit 4> 查看該容器的狀態 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d4a75f165ce6 centos "/bin/bash" 2 minutes ago Exited (0) 23 seconds ago cranky_mahavira 可見此時容器的狀態是Exited,那麼,如何再次運行這個容器呢?可使用docker start命令 5> 再次運行該容器 [root@localhost ~]# docker start cranky_mahavira cranky_mahavira 6> 再次查看該容器的狀態 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d4a75f165ce6 centos "/bin/bash" 6 minutes ago Up 29 seconds cranky_mahavira 由於該容器是交互型的,但此刻咱們發現沒有具體的終端能夠與之交互,這時可以使用attach命令。 7> 經過attach命令進行交互 [root@localhost ~]# docker attach cranky_mahavira [root@d4a75f165ce6 /]# build Build an image from a Dockerfile --經過Dockerfile建立鏡像 commit Create a new image from a container's changes --經過容器建立本地鏡像 注意:若是是要push到docker hub中,注意生成鏡像的命名 [root@localhost ~]# docker commit centos_v1 centos:v1 68ad49c999496cff25fdda58f0521530a143d3884e61bce7ada09bdc22337638 [root@localhost ~]# docker push centos:v1 You cannot push a "root" repository. Please rename your repository to <user>/<repo> (ex: <user>/centos) 用centos:v1就不行,由於它push到docker hub中時,是推送到相應用戶下,必須指定用戶名。譬如個人用戶名是ivictor,則新生成的本地鏡像命名爲: docker push victor/centos:v1,其中v1是tag,可不寫,默認是latest cp Copy files/folders from a container to a HOSTDIR or to STDOUT --在宿主機和容器之間相互COPY文件 cp的用法以下: Usage: docker cp [OPTIONS] CONTAINER:PATH LOCALPATH|- docker cp [OPTIONS] LOCALPATH|- CONTAINER:PATH 如:容器mysql中/usr/local/bin/存在docker-entrypoint.sh文件,可以下方式copy到宿主機 # docker cp mysql:/usr/local/bin/docker-entrypoint.sh /root 修改完畢後,將該文件從新copy回容器 # docker cp /root/docker-entrypoint.sh mysql:/usr/local/bin/ create Create a new container --建立一個新的容器,注意,此時,容器的status只是Created diff Inspect changes on a container's filesystem --查看容器內發生改變的文件,以個人mysql容器爲例 [root@localhost ~]# docker diff mysqldb C /root A /root/.bash_history A /test1.txt A /test.tar A /test.txt C /run C /run/mysqld A /run/mysqld/mysqld.pid A /run/mysqld/mysqld.sock 不難看出,C對應的均是目錄,A對應的均是文件 events Get real time events from the server --實時輸出Docker服務器端的事件,包括容器的建立,啓動,關閉等。 譬如: [root@localhost ~]# docker events 2015-09-08T17:40:13.000000000+08:00 d2a2ef5ddb90b505acaf6b59ab43eecf7eddbd3e71f36572436c34dc0763db79: (from wordpress) create 2015-09-08T17:40:14.000000000+08:00 d2a2ef5ddb90b505acaf6b59ab43eecf7eddbd3e71f36572436c34dc0763db79: (from wordpress) die 2015-09-08T17:42:10.000000000+08:00 839866a338db6dd626fa8eabeef53a839e4d2e2eb16ebd89679aa722c4caa5f7: (from mysql) start exec Run a command in a running container --用於容器啓動以後,執行其它的任務 經過exec命令能夠建立兩種任務:後臺型任務和交互型任務 後臺型任務:docker exec -d cc touch 123 其中cc是容器名 交互型任務: [root@localhost ~]# docker exec -i -t cc /bin/bash root@1e5bb46d801b:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var export Export a container's filesystem as a tar archive --將容器的文件系統打包成tar文件 有兩種方式(mysqldb爲容器名): docker export -o mysqldb1.tar mysqldb docker export mysqldb > mysqldb.tar history Show the history of an image --顯示鏡像製做的過程,至關於dockfile images List images --列出本機的全部鏡像 import Import the contents from a tarball to create a filesystem image --根據tar文件的內容新建一個鏡像,與以前的export命令相對應 [root@localhost ~]# docker import mysqldb.tar mysql:v1 eb81de183cd94fd6f0231de4ff29969db822afd3a25841d2dc9cf3562d135a10 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE mysql v1 eb81de183cd9 21 seconds ago 281.9 MB 譬以下面一例: [root@localhost volume2]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9cb07559cc17 docker.io/ubuntu "/bin/bash" 22 hours ago Up 22 hours naughty_bartik [root@localhost volume2]# docker export gigantic_goldwasser > wanghui.tar [root@localhost volume2]# docker import wanghui.tar wanghui:v1 sha256:b6cbbaf69a58149f337dcc439a21ed185dcdf96fd7f72ddf45e102d27f47c4ae [root@localhost volume2]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE wanghui v1 b6cbbaf69a58 5 seconds ago 450.9 MB [root@localhost volume2]# docker run -i -t wanghui:v1 /bin/bash [root@78f4ac39972d /]# ps -ef info Display system-wide information --查看docker的系統信息 [root@localhost ~]# docker info Containers: 3 --當前有3個容器 Images: 298 Storage Driver: devicemapper Pool Name: docker-253:0-34402623-pool Pool Blocksize: 65.54 kB Backing Filesystem: xfs Data file: /dev/loop0 Metadata file: /dev/loop1 Data Space Used: 8.677 GB --對應的是下面Data loop file大小 Data Space Total: 107.4 GB Data Space Available: 5.737 GB Metadata Space Used: 13.4 MB --對應的是下面Metadata loop file大小 Metadata Space Total: 2.147 GB Metadata Space Available: 2.134 GB Udev Sync Supported: true Deferred Removal Enabled: false Data loop file: /var/lib/docker/devicemapper/devicemapper/data Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata Library Version: 1.02.93-RHEL7 (2015-01-28) Execution Driver: native-0.2 Logging Driver: json-file Kernel Version: 3.10.0-229.el7.x86_64 Operating System: CentOS Linux 7 (Core) CPUs: 2 Total Memory: 979.7 MiB Name: localhost.localdomain ID: TFVB:BXGQ:VVOC:K2DJ:LECE:2HNK:23B2:LEVF:P3IQ:L7D5:NG2V:UKNL WARNING: bridge-nf-call-iptables is disabled WARNING: bridge-nf-call-ip6tables is disabled inspect Return low-level information on a container or image --用於查看容器的配置信息,包含容器名、環境變量、運行命令、主機配置、網絡配置和數據卷配置等。 kill Kill a running container --強制終止容器 關於stop和kill的區別,docker stop命令給容器中的進程發送SIGTERM信號,默認行爲是會致使容器退出,固然, 容器內程序能夠捕獲該信號並自行處理,例如能夠選擇忽略。而docker kill則是給容器的進程發送SIGKILL信號,該信號將會使容器必然退出。 load Load an image from a tar archive or STDIN --與下面的save命令相對應,將下面sava命令打包的鏡像經過load命令導入 login Register or log in to a Docker registry --登陸到本身的Docker register,需有Docker Hub的註冊帳號 [root@localhost ~]# docker login Username: ivictor Password: Email: xxxx@foxmail.com WARNING: login credentials saved in /root/.docker/config.json Login Succeeded logout Log out from a Docker registry --退出登陸 [root@localhost ~]# docker logout Remove login credentials for https://index.docker.io/v1/ logs Fetch the logs of a container --用於查看容器的日誌,它將輸出到標準輸出的數據做爲日誌輸出到docker logs命令的終端上。經常使用於後臺型容器 pause Pause all processes within a container --暫停容器內的全部進程, 此時,經過docker stats能夠觀察到此時的資源使用狀況是固定不變的, 經過docker logs -f也觀察不到日誌的進一步輸出。 port List port mappings or a specific mapping for the CONTAINER --輸出容器端口與宿主機端口的映射狀況 譬如: [root@localhost ~]# docker port blog 80/tcp -> 0.0.0.0:80 容器blog的內部端口80映射到宿主機的80端口,這樣可經過宿主機的80端口查看容器blog提供的服務 ps List containers --列出全部容器,其中docker ps用於查看正在運行的容器,ps -a則用於查看全部容器。 pull Pull an image or a repository from a registry --從docker hub中下載鏡像 push Push an image or a repository to a registry --將本地的鏡像上傳到docker hub中 前提是你要先用docker login登陸上,否則會報如下錯誤 [root@localhost ~]# docker push ivictor/centos:v1 The push refers to a repository [docker.io/ivictor/centos] (len: 1) unauthorized: access to the requested resource is not authorized rename Rename a container --更改容器的名字 restart Restart a running container --重啓容器 rm Remove one or more containers --刪除容器,注意,不能夠刪除一個運行中的容器,必須先用docker stop或docker kill使其中止。 固然能夠強制刪除,必須加-f參數 若是要一次性刪除全部容器,可以使用 docker rm -f `docker ps -a -q`,其中,-q指的是隻列出容器的ID rmi Remove one or more images --刪除鏡像 run Run a command in a new container --讓建立的容器馬上進入運行狀態,該命令等同於docker create建立容器後再使用docker start啓動容器 save Save an image(s) to a tar archive --將鏡像打包,與上面的load命令相對應 譬如: docker save -o nginx.tar nginx search Search the Docker Hub for images --從Docker Hub中搜索鏡像 start Start one or more stopped containers --啓動容器 stats Display a live stream of container(s) resource usage statistics --動態顯示容器的資源消耗狀況,包括:CPU、內存、網絡I/O stop Stop a running container --中止一個運行的容器 tag Tag an image into a repository --對鏡像進行重命名 top Display the running processes of a container --查看容器中正在運行的進程 unpause Unpause all processes within a container --恢復容器內暫停的進程,與pause參數相對應 version Show the Docker version information --查看docker的版本 wait Block until a container stops, then print its exit code --捕捉容器中止時的退出碼 執行此命令後,該命令會「hang」在當前終端,直到容器中止,此時,會打印出容器的退出碼。
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
[root@localhost ~]# docker run --help : docker run [OPTIONS] IMAGE [COMMAND] [ARG...] Run a command in a new container -a, --attach=[] Attach to STDIN, STDOUT or STDERR --add-host=[] Add a custom host-to-IP mapping (host:ip) 增長一個定製的'主機-IP'映射 --blkio-weight=0 Block IO (relative weight), between 10 and 1000 -c, --cpu-shares=0 CPU shares (relative weight) --cap-add=[] Add Linux capabilities 增長linux能力 --cap-drop=[] Drop Linux capabilities --cgroup-parent= Optional parent cgroup for the container --cidfile= Write the container ID to the file 把容器的ID寫入文件 --cpu-period=0 Limit CPU CFS (Completely Fair Scheduler) period --cpu-quota=0 Limit the CPU CFS quota --cpuset-cpus= CPUs in which to allow execution (0-3, 0,1) --cpuset-mems= MEMs in which to allow execution (0-3, 0,1) -d, --detach=false Run container in background and print container ID 在後臺運行容器並打印容器ID --device=[] Add a host device to the container 把一個主機設備添加到容器 --dns=[] Set custom DNS servers 設置定製的域名服務器 --dns-search=[] Set custom DNS search domains 設置定製的域名服務器的搜索域 -e, --env=[] Set environment variables 設置環境變量 --entrypoint= Overwrite the default ENTRYPOINT of the image 覆蓋鏡像的默認進入點 --env-file=[] Read in a file of environment variables 讀入一個包含環境變量的文件 --expose=[] Expose a port or a range of ports 暴露一個端口、端口範圍 -h, --hostname= Container host name 容器的主機名 -i, --interactive=false Keep STDIN 標準輸入 --ipc= IPC namespace to use 使用的IPC命名空間 --pid= PID namespace to use 使用的PID命名空間 --uts= UTS namespace to use -l, --label=[] Set meta data on a container 在容器上,設置元數據 --label-file=[] Read in a line delimited file of labels --link=[] Add link to another container 添加一個到另外一個容器的鏈接 --log-driver= Logging driver for container 容器的日誌驅動 --log-opt=[] Log driver options --lxc-conf=[] Add custom lxc options 添加定製的lxc選項 -m, --memory= Memory limit 內存限制 --mac-address= Container MAC address (e.g. 92:d0:c6:0a:29:33) 容器的MAC地址 --memory-swap= Total memory (memory + swap), '-1' to disable swap 容器的總內存(物理內容+交換區) --name= Assign a name to the container 爲容器分配一個名字 --net=bridge Set the Network mode for the container 爲容器設置網絡模式 --oom-kill-disable=false Disable OOM Killer -P, --publish-all=false Publish all exposed ports to random ports 把通氣端口發佈的主機。即容器端口映射到宿主機的任意端口上。 -p, --publish=[] Publish a container's port(s) to the host 把容器的端口發佈到主機,即容器端口映射到宿主機的具體端口上。可加上多個-p --privileged=false Give extended privileges to this container 賦予容器擴展權限 --read-only=false Mount the container's root filesystem as read only 以只讀的方式裝載容器的根文件系統 --restart=no Restart policy to apply when a container exits --rm=false Automatically remove the container when it exits 當容器存在時,自動移除容器 --security-opt=[] Security Options 安全選項 --sig-proxy=true Proxy received signals to the process -t, --tty=false Allocate a pseudo-TTY 分配一個僞終端 -u, --u-user= Username or UID (format: <name|uid>[:<group|gid>]) --ulimit=[] Ulimit options -v, --volume=[] Bind mount a volume --volumes-from=[] Mount volumes from the specified container(s) -w, --workdir= Working directory inside the container -------------------------------------------- 當運行docker run命令時,Docker會啓動一個進程,併爲這個進程分配其獨佔的文件系統、網絡資源和以此進程爲根進程的進程組。 在容器啓動時,鏡像可能已經定義了要運行的二進制文件、暴露的網絡端口等,可是用戶能夠經過docker run命令從新定義(docker run能夠控制一個容器運行時的行爲,它能夠覆蓋docker build在構建鏡像時的一些默認配置),這也是爲何run命令相比於其它命令有如此多的參數的緣由。 使用方法: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS總起來講能夠分爲兩類: a)設置運行方式: 決定容器的運行方式,前臺執行仍是後臺執行; 設置containerID; 設置網絡參數; 設置容器的CPU和內存參數; 設置權限和LXC參數; b)設置鏡像的默認資源,也就是說用戶可使用該命令來覆蓋在鏡像構建時的一些默認配置。 docker run [OPTIONS]可讓用戶徹底控制容器的生命週期,並容許用戶覆蓋執行docker build時所設定的參數,甚至也能夠修改自己由Docker所控制的內核級參數。 Operator exclusive options 當執行docker run時能夠設置如下參數: 1.Detached vs Foreground Detached (-d) - Foreground 2.Container Identification Name (--name) - PID Equivalent 3.IPC Setting 4.Network Settings 5.Clean Up (--rm) 6.Runtime Constraints on CPU and Memory 7.Runtime Privilege, Linux Capabilities, and LXC Configuration ---------------------------------------------------------------------------------------------- 1.Detached vs foreground 當咱們啓動一個容器時,首先須要肯定這個容器是運行在前臺仍是運行在後臺。 -d=false, 沒有附加標準輸入、輸出、錯誤 ---- 運行在後臺 Detached (-d) docker run -d -d=false --detach=false 那麼容器將會運行在後臺模式。 此時全部I/O數據只能經過網絡資源或者共享卷組來進行交互,由於容器再也不監聽你執行docker run的這個終端命令行窗口。 但你能夠經過執行docker attach來從新附着到該容器的回話中。 須要注意的是,容器運行在後臺模式下,是不能使用--rm選項的。 2.Foregroud 不指定-d參數(爲明確給-d選項指定值,取默認值false) --在前臺模式下 Docker會在容器中啓動進程,同時將當前的命令行窗口附着到容器的標準輸入、標準輸出和標準錯誤中 --- 把當前的命令行窗口附着到容器的標準輸入、輸出、錯誤上. 也就是說容器中全部的輸出均可以在當前窗口中看到。甚至它均可以虛擬出一個TTY窗口,來執行信號中斷。 這一切都是能夠配置的: -a=[], --attach=[] 把容器的標準輸入、輸出、錯誤附着到當前的命令行窗口 -t=false, --tty=false 分配一個僞終端 -i=false, --interactive=false 附着標準輸入到當前命令行 -------特別注意--------- 注意: -i 選項取默認值(false) docker run 沒有-i選項,至關於docker run -i=false,即非交互式運行 docker run -i 指定-i選項,即以交互式運行 若是在執行run命令時沒有指定-a參數,那麼Docker默認會掛載全部標準數據流,包括輸入輸出和錯誤,你能夠單獨指定掛載哪一個標準流。 # docker run -a=[stdin, stdout] -i -t ubuntu /bin/bash 若是要進行交互式操做(例如Shell腳本),那咱們必須使用-i -t參數同容器進行數據交互。 可是當經過管道同容器進行交互時,就不須要使用-t參數,例以下面的命令: # echo test | docker run -i busybox cat
1.Name(--name) 能夠經過三種方式爲容器命名: 1)使用UUID長命名("f78375b1c487e03c9438c729345e54db9d20cfa2ac1fc3494b6eb60872e74778") 2)使用UUID短命令("f78375b1c487") 3)使用Name("evil_ptolemy") 這個UUID標示是由Docker deamon生成的。 若是你在執行docker run時沒有指定--name,那麼deamon會自動生成一個隨機字符串UUID。 可是對於一個容器來講有個name會很是方便,當你須要鏈接其它容器時或者相似須要區分其它容器時,使用容器名稱能夠簡化操做。不管容器運行在前臺或者後臺,這個名字都是有效的。 PID equivalent 若是在使用Docker時有自動化的需求,你能夠將containerID輸出到指定的文件中(PIDfile),相似於某些應用程序將自身ID輸出到文件中,方便後續腳本操做。 --cidfile="": Write the container ID to the file 2.Image[:tag] 當一個鏡像的名稱不足以分辨這個鏡像所表明的含義時,你能夠經過tag將版本信息添加到run命令中,以執行特定版本的鏡像。例如:docker run ubuntu:14.04 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/uifd/ui-for-docker latest 312812aadc64 34 hours ago 8.096 MB docker.io/nginx latest 5e69fe4b3c31 5 days ago 182.5 MB 192.168.1.23:5000/tomcat7 latest 47c5123914a1 6 days ago 562.3 MB docker.io/ubuntu latest 0ef2e08ed3fa 4 weeks ago 130 MB docker.io/centos latest 67591570dd29 3 months ago 191.8 MB docker.io/tomcat latest ebb17717bed4 5 months ago 355.4 MB 3.IPC Settings 默認狀況下,全部容器都開啓了IPC命名空間。 --ipc="" : Set the IPC mode for the container, 'container:<name|id>': reuses another container's IPC namespace 'host': use the host's IPC namespace inside the container IPC(POSIX/SysV IPC)命名空間提供了相互隔離的命名共享內存、信號燈變量和消息隊列。 共享內存能夠提升進程數據的交互速度。 共享內存通常用在數據庫和高性能應用(C/OpenMPI、C++/using boost libraries)上或者金融服務上。 若是須要容器中部署上述類型的應用,那麼就應該在多個容器直接使用共享內存了。