docker 命令介紹
docker --help
管理命令:
container 管理容器
image 管理鏡像
network 管理網絡
命令:
attach 介入到一個正在運行的容器
build 根據 Dockerfile 構建一個鏡像
commit 根據容器的更改創建一個新的鏡像
cp 在本地文件系統與容器中複製 文件/文件夾
create 創建一個新容器
diff 檢查容器文件系統上的文件或目錄的更改
exec 在容器中執行一條命令
images 列出鏡像
kill 殺死一個或多個正在運行的容器
logs 取得容器的日誌
pause 暫停一個或多個容器的所有進程
ps 列出所有容器
pull 拉取一個鏡像或倉庫到 registry
push 推送一個鏡像或倉庫到 registry
rename 重命名一個容器
restart 重新啓動一個或多個容器
rm 刪除一個或多個容器
rmi 刪除一個或多個鏡像
run 在一個新的容器中執行一條命令
search 在 Docker Hub 中搜索鏡像
start 啓動一個或多個已經停止運行的容器
stats 顯示一個容器的實時資源佔用
stop 停止一個或多個正在運行的容器
tag 爲鏡像創建一個新的標籤
top 顯示一個容器內的所有進程
inspect 查看容器的詳細信息
unpause 恢復一個或多個容器內所有被暫停的進程
更詳細的功能參數配置
--api-enable-cors=false
開放遠程API調用的 CORS 頭信息。這個接口開關對想進行二次開
發的上層應用提供了支持.
-b, --bridge=""
掛載已經存在的網橋設備到 Docker 容器裏。注意,使用 none
可以停用容器裏的網絡.
--bip=""
使用 CIDR 地址來設定網絡橋的 IP。注意,此參數和 -b 不能一起使用.
-D, --debug=false
開啓Debug模式。例如:docker -d -D
-d, --daemon=false
開啓Daemon模式.
--dns=[]
強制容器使用DNS服務器.例如: docker -d --dns 8.8.8.8
--dns-search=[]
強制容器使用指定的DNS搜索域名.例如: docker -d --dns-search
example.com
-e, --exec-driver="native"
強制容器使用指定的運行時驅動.例如:docker -d -e lxc
-G, --group="docker"
在後臺運行模式下,賦予指定的Group到相應的unix socket上。
注意,當此參數 --group 賦予空字符串時,將去除組信息。
-g, --graph="/var/lib/docker"
配置Docker運行時根目錄
-H, --host=[]
在後臺模式下指定socket綁定,可以綁定一個或多個
tcp://host:port, unix:///path/to/socket, fd://* 或
fd://socketfd。例如:$ docker -H tcp://0.0.0.0:2375 ps
或者 $ export DOCKER_HOST="tcp://0.0.0.0:2375" $ docker ps
--icc=true
啓用內聯容器的通信.
--ip="0.0.0.0"
容器綁定IP時使用的默認IP地址.
--ip-forward=true
啓動容器的 net.ipv4.ip_forward.
--iptables=true
啓動Docker容器自定義的iptable規則.
--mtu=0
設置容器網絡的MTU值,如果沒有這個參數,選用默認 route MTU,
如果沒有默認route,就設置成常量值 1500.
-p, --pidfile="/var/run/docker.pid"
後臺進程PID文件路徑.
-r, --restart=true
重啓之前運行中的容器.
-s, --storage-driver=""
強制容器運行時使用指定的存儲驅動,例如,指定使用devicemapper,
可以這樣:docker -d -s devicemapper
--selinux-enabled=false
啓用selinux支持
--storage-opt=[]
配置存儲驅動的參數
--tls=false
啓動TLS認證開關
--tlscacert="/Users/dxiao/.docker/ca.pem"
通過CA認證過的的certificate文件路徑
--tlscert="/Users/dxiao/.docker/cert.pem"
TLS的certificate文件路徑
--tlskey="/Users/dxiao/.docker/key.pem"
TLS的key文件路徑
--tlsverify=false
使用TLS並做後臺進程與客戶端通訊的驗證
-v, --version=false
顯示版本信息
*注意:其中帶有[] 的啓動參數可以指定多次,例如
docker run -a stdin -a stdout -a stderr -i -t ubuntu /bin/bash
docker run
$ docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--add-host list Add a custom host-to-IP mapping
(host:ip)
-a, --attach list Attach to STDIN, STDOUT or STDERR
--blkio-weight uint16 Block IO (relative weight),
between 10 and 1000, or 0 to
disable (default 0)
--blkio-weight-device list Block IO weight (relative device
weight) (default [])
--cap-add list Add Linux capabilities
--cap-drop list Drop Linux capabilities
--cgroup-parent string Optional parent cgroup for the
container
--cidfile string Write the container ID to the file # 把容器 id 寫入到指定文件
--cpu-period int Limit CPU CFS (Completely Fair
Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair
Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in
microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in
microseconds
-c, --cpu-shares int CPU shares (relative weight) # 設置 cpu 使用權重
--cpus decimal Number of CPUs
--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)
-d, --detach Run container in background and
print container ID # 後臺運行容器和打印容器ID
--detach-keys string Override the key sequence for
detaching a container
--device list Add a host device to the container
--device-cgroup-rule list Add a rule to the cgroup allowed
devices list
--device-read-bps list Limit read rate (bytes per second)
from a device (default [])
--device-read-iops list Limit read rate (IO per second)
from a device (default [])
--device-write-bps list Limit write rate (bytes per
second) to a device (default [])
--device-write-iops list Limit write rate (IO per second)
to a device (default [])
--disable-content-trust Skip image verification (default true)
--dns list Set custom DNS servers
--dns-option list Set DNS options
--dns-search list Set custom DNS search domains # 設置 dns 域搜索
--entrypoint string Overwrite the default ENTRYPOINT
of the image
-e, --env list Set environment variables # 定義環境變量
--env-file list Read in a file of environment variables # 從指定文件讀取變量值
--expose list Expose a port or a range of ports # 指定對外提供服務端口
--group-add list Add additional groups to join
--health-cmd string Command to run to check health
--health-interval duration Time between running the check
(ms|s|m|h) (default 0s)
--health-retries int Consecutive failures needed to
report unhealthy
--health-start-period duration Start period for the container to
initialize before starting
health-retries countdown
(ms|s|m|h) (default 0s)
--health-timeout duration Maximum time to allow one check to
run (ms|s|m|h) (default 0s)
--help Print usage
-h, --hostname string Container host name # 設置容器主機名
--init Run an init inside the container
that forwards signals and reaps
processes
-i, --interactive Keep STDIN open even if not attached # 保持標準輸出開啓即使沒有
--ip string IPv4 address (e.g., 172.30.100.104)
--ip6 string IPv6 address (e.g., 2001:db8::33)
--ipc string IPC mode to use
--isolation string Container isolation technology
--kernel-memory bytes Kernel memory limit
-l, --label list Set meta data on a container
--label-file list Read in a line delimited file of labels
--link list Add link to another container # 添加鏈接到另外一個容器
--link-local-ip list Container IPv4/IPv6 link-local
addresses
--log-driver string Logging driver for the container
--log-opt list Log driver options
--mac-address string Container MAC address (e.g.,
92:d0:c6:0a:29:33)
-m, --memory bytes Memory limit # 內存限制
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus
swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness
(0 to 100) (default -1)
--mount mount Attach a filesystem mount to the
container
--name string Assign a name to the container # 設置容器名
--network string Connect a container to a network
(default "default")
--network-alias list Add network-scoped alias for the
container
--no-healthcheck Disable any container-specified
HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj int Tune host's OOM preferences (-1000
to 1000)
--pid string PID namespace to use
--pids-limit int Tune container pids limit (set -1
for unlimited)
--privileged Give extended privileges to this
container
-p, --publish list Publish a container's port(s) to
the host # 指定端口映射
-P, --publish-all Publish all exposed ports to
random ports # 自動映射容器對外提供服務的端口
--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 # 如果退出自動刪除容器和 -d 選項衝突
--runtime string Runtime to use for this container
--security-opt list Security Options
--shm-size bytes Size of /dev/shm
--sig-proxy Proxy received signals to the
process (default true)
--stop-signal string Signal to stop a container
(default "15")
--stop-timeout int Timeout (in seconds) to stop a
container
--storage-opt list Storage driver options for the
container
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory
-t, --tty Allocate a pseudo-TTY # 分配僞終端
--ulimit ulimit Ulimit options (default [])
-u, --user string Username or UID (format:
<name|uid>[:<group|gid>]) # 指定運行容器的用戶 uid 或者用戶名
--userns string User namespace to use
--uts string UTS namespace to use
-v, --volume list Bind mount a volume # 掛載卷
--volume-driver string Optional volume driver for the
container
--volumes-from list Mount volumes from the specified
container(s) # 從指定容器掛載卷
-w, --workdir string Working directory inside the container # 指定容器工作目錄
示例:
$ sudo docker images ubuntu
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 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 NAMES
a424ca613c9f ubuntu:14.04 /bin/bash 6 seconds ago Up 5 seconds docker_test1 $ sudo docker attach docker_test1
root@test1:/# pwd /
root@test1:/# exit exit
docker build
語法:docker build [OPTIONS] PATH | URL | -
OPTIONS說明:
--build-arg=[] :設置鏡像創建時的變量;
--tag, -t: 鏡像的名字及標籤,通常 name:tag 或者 name 格式;可以在一次構建中爲一個鏡像設置多個標籤。
--network: 默認 default。在構建期間設置RUN指令的網絡模式
-f :指定要使用的Dockerfile路徑;
--no-cache :創建鏡像的過程不使用緩存;
--pull :嘗試去更新鏡像的新版本;
--quiet, -q :安靜模式,成功後只輸出鏡像 ID;
--rm :設置鏡像成功後刪除中間容器;
--cpu-shares :設置 cpu 使用權重;
--cpu-period :限制 CPU CFS週期;
--cpu-quota :限制 CPU CFS配額;
--cpuset-cpus :指定使用的CPU id;
--cpuset-mems :指定使用的內存 id;
--disable-content-trust :忽略校驗,默認開啓;
--force-rm :設置鏡像過程中刪除中間容器;
--isolation :使用容器隔離技術;
--label=[] :設置鏡像使用的元數據;
-m :設置內存最大值;
--memory-swap :設置Swap的最大值爲內存+swap,"-1"表示不限swap;
--shm-size :設置/dev/shm的大小,默認值是64M;
--ulimit :Ulimit配置。
實例
1.使用當前目錄的 Dockerfile 創建鏡像,標籤爲 runoob/ubuntu:v1。docker build -t runoob/ubuntu:v1 .
2.使用URL github.com/creack/docker-firefox 的 Dockerfile 創建鏡像。docker build github.com/creack/docker-firefox
3.也可以通過 -f Dockerfile 文件的位置:$ docker build -f /path/to/a/Dockerfile .
4.在 Docker 守護進程執行 Dockerfile 中的指令前,首先會對 Dockerfile 進行語法檢查,有語法錯誤時會返回:
$ docker build -t test/myapp .
Sending build context to Docker daemon 2.048 kB
Error response from daemon: Unknown instruction: RUNCMD
更多命令請參閱:http://www.docker.org.cn/dockerppt/106.html
參考資料
官方英文資源
· Docker官網:http://www.docker.com
· Docker windows入門:https://docs.docker.com/windows/
· Docker Linux 入門:https://docs.docker.com/linux/
· Docker mac 入門:https://docs.docker.com/mac/
· Docker 用戶指引:https://docs.docker.com/engine/userguide/
· Docker 官方博客:http://blog.docker.com/
· Docker Hub: https://hub.docker.com/
· Docker開源: https://www.docker.com/open-source
中文資源
· Docker中文網站:http://www.docker.org.cn
· Docker中文文檔:http://www.dockerinfo.net/document
· Docker安裝手冊:http://www.docker.org.cn/book/install.html
· 一小時Docker教程 :https://blog.csphere.cn/archives/22
· Docker中文指南:http://www.widuu.com/chinese_docker/index.html