Docker的學習--命令使用詳解


使用命令查看一下docker都有那些命令:

docker -h

你將得到如下結果:

A self-sufficient runtime for linux containers.

Options:

  --api-cors-header=                   Set CORS headers in the remote API
  -b, --bridge=                        Attach containers to a network bridge
  --bip=                               Specify network bridge IP
  -D, --debug=false                    Enable debug mode
  -d, --daemon=false                   Enable daemon mode
  --default-gateway=                   Container default gateway IPv4 address
  --default-gateway-v6=                Container default gateway IPv6 address
  --default-ulimit=[]                  Set default ulimits for containers
  --dns=[]                             DNS server to use
  --dns-search=[]                      DNS search domains to use
  -e, --exec-driver=native             Exec driver to use
  --exec-opt=[]                        Set exec driver options
  --exec-root=/var/run/docker          Root of the Docker execdriver
  --fixed-cidr=                        IPv4 subnet for fixed IPs
  --fixed-cidr-v6=                     IPv6 subnet for fixed IPs
  -G, --group=docker                   Group for the unix socket
  -g, --graph=/var/lib/docker          Root of the Docker runtime
  -H, --host=[]                        Daemon socket(s) to connect to
  -h, --help=false                     Print usage
  --icc=true                           Enable inter-container communication
  --insecure-registry=[]               Enable insecure registry communication
  --ip=0.0.0.0                         Default IP when binding container ports
  --ip-forward=true                    Enable net.ipv4.ip_forward
  --ip-masq=true                       Enable IP masquerading
  --iptables=true                      Enable addition of iptables rules
  --ipv6=false                         Enable IPv6 networking
  -l, --log-level=info                 Set the logging level
  --label=[]                           Set key=value labels to the daemon
  --log-driver=json-file               Default driver for container logs
  --log-opt=map[]                      Set log driver options
  --mtu=0                              Set the containers network MTU
  -p, --pidfile=/var/run/docker.pid    Path to use for daemon PID file
  --registry-mirror=[]                 Preferred Docker registry mirror
  -s, --storage-driver=                Storage driver to use
  --selinux-enabled=false              Enable selinux support
  --storage-opt=[]                     Set storage driver options
  --tls=false                          Use TLS; implied by --tlsverify
  --tlscacert=~/.docker/ca.pem         Trust certs signed only by this CA
  --tlscert=~/.docker/cert.pem         Path to TLS certificate file
  --tlskey=~/.docker/key.pem           Path to TLS key file
  --tlsverify=false                    Use TLS and verify the remote
  --userland-proxy=true                Use userland proxy for loopback traffic
  -v, --version=false                  Print version information and quit

Commands:
    attach    Attach to a running container
    build     Build an image from a Dockerfile
    commit    Create a new image from a container's changes
    cp        Copy files/folders from a container's filesystem to the host path
    create    Create a new container
    diff      Inspect changes on a container's filesystem
    events    Get real time events from the server
    exec      Run a command in a running container
    export    Stream the contents of a container as a tar archive
    history   Show the history of an image
    images    List images
    import    Create a new filesystem image from the contents of a tarball
    info      Display system-wide information
    inspect   Return low-level information on a container or image
    kill      Kill a running container
    load      Load an image from a tar archive
    login     Register or log in to a Docker registry server
    logout    Log out from a Docker registry server
    logs      Fetch the logs of a container
    pause     Pause all processes within a container
    port      Lookup the public-facing port that is NAT-ed to PRIVATE_PORT
    ps        List containers
    pull      Pull an image or a repository from a Docker registry server
    push      Push an image or a repository to a Docker registry server
    rename    Rename an existing container
    restart   Restart a running container
    rm        Remove one or more containers
    rmi       Remove one or more images
    run       Run a command in a new container
    save      Save an image to a tar archive
    search    Search for an image on the Docker Hub
    start     Start a stopped container
    stats     Display a stream of a containers' resource usage statistics
    stop      Stop a running container
    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
    wait      Block until a container stops, then print its exit code

Run 'docker COMMAND --help' for more information on a command.

下面來詳細說明一些命令的使用,在此之前,說一下如果不理解理解 Docker 和 Docker Hub 及兩者關系,可以類比 Git 和 GitHub 理解。

1. docker version

顯示 Docker 版本信息。

2. docker info

顯示 Docker 系統信息,包括鏡像和容器數。

3. docker search

從 Docker Hub 中搜索符合條件的鏡像。

docker search -s 3 --automated --no-trunc django

上面命令的意思是搜索處收藏數不小於 3 ,並且能夠自動化構建的 django 鏡像,並且完整顯示鏡像描述。

參數:

  --automated=false    Only show automated builds 只列出 automated build類型的鏡像--no-trunc=false     Don't truncate output 顯示完整的鏡像描述
  -s, --stars=0        Only displays with at least x stars 只列出不低於x個收藏的鏡像

4. docker pull

從 Docker Hub 中拉取或者更新指定鏡像。

docker pull ubuntu:latest

上面命令的意思是拉取ubuntu最新的鏡像。

參數:

-a, --all-tags=false    Download all tagged images in the repository 拉取所有 tagged 鏡像

5. docker login

按步驟輸入在 Docker Hub 注冊的用戶名、密碼和郵箱即可完成登錄。

6. docker logout

運行后從指定服務器登出,默認為官方服務器。

7. docker images
列出本地所有鏡像。對鏡像名稱進行關鍵詞查詢。

docker images ubuntu

上面命令的意思是列出本地鏡像名為 ubuntu 的所有鏡像。不加 ubuntu,就列出所有本地鏡像。

參數:

  -a, --all=false      Show all images (default hides intermediate images) 列出所有鏡像(含中間映像層,默認情況下,過濾掉中間映像層)
  --digests=false      Show digests 展示鏡像的摘要
  -f, --filter=[]      Filter output based on conditions provided 過濾鏡像,如: -f ['dangling=true'] 只列出滿足dangling=true 條件的鏡像
  --no-trunc=false     Don't truncate output 顯示完整的鏡像ID
  -q, --quiet=false    Only show numeric IDs 僅列出鏡像ID

8. docker ps

列出所有運行中容器。

參數:

  -a, --all=false       Show all containers (default shows just running) 列出所有容器(含沉睡鏡像)
  --before=             Show only container created before Id or Name 列出在某一容器之前創建的容器,接受容器名稱和ID作為參數
  -f, --filter=[]       Filter output based on conditions provided -f [exited=<int>] 列出滿足exited=<int> 條件的容器-l, --latest=false    Show the latest created container, include non-running 僅列出最新創建的一個容器
  -n=-1                 Show n last created containers, include non-running 列出最近創建的n個容器
  --no-trunc=false      Don't truncate output 顯示完整的容器ID
  -q, --quiet=false     Only display numeric IDs 僅列出容器ID
  -s, --size=false      Display total file sizes 顯示容器大小
  --since=              Show created since Id or Name, include non-running 列出在某一容器之后創建的容器,接受容器名稱和ID作為參數

9. docker rmi
從本地移除一個或多個指定的鏡像。

docker rmi nginx:latest ubuntu:14.04

上面命令的意思是移除 nginx 最新版本的鏡像和ubuntu 14.04 版本的鏡像。

參數:

  -f, --force=false    Force removal of the image 強行移除該鏡像,即使其正被使用
  --no-prune=false     Do not delete untagged parents 不移除該鏡像的過程鏡像,默認移除

10. docker rm

從本地移除一個或多個指定的容器。

docker rm harrysun/lnmp
docker rm -l webapp/redis

上面命令的意思分別是移除 harrysun/lnmp 的本地容器和移除 webapp/redis 容器的網絡連接。

參數:

  -f, --force=false      Force the removal of a running container (uses SIGKILL) 強行移除該容器,即使其正在運行-l, --link=false       Remove the specified link 移除容器間的網絡連接,而非容器本身
  -v, --volumes=false    Remove the volumes associated with the container 移除與容器關聯的空間

11. docker history

查看指定鏡像的創建歷史。

docker history -H harrysun/lnmp:0.1

上面命令的意思是查看 harrysun/lnmp:0.1 鏡像的歷史。

  -H, --human=true     Print sizes and dates in human readable format 以可讀的格式打印鏡像大小和日期
  --no-trunc=false     Don't truncate output 顯示完整的提交記錄
  -q, --quiet=false    Only show numeric IDs 僅列出提交記錄ID

12. docker start|stop|restart

啟動、停止和重啟一個或多個指定容器。

docker start -i b5e08e1435b3

上面命令的意思是啟動一個 ID 為 b5e08e1435b3 的容器,並進入交互模式。

參數:

  -a, --attach=false         Attach STDOUT/STDERR and forward signals 啟動一個容器並打印輸出結果和錯誤
  -i, --interactive=false    Attach container's STDIN 啟動一個容器並進入交互模式
  -t, --time=10      Seconds to wait for stop before killing the container 停止或者重啟容器的超時時間(秒),超時后系統將殺死進程。

13. docker kill

殺死一個或多個指定容器進程。

docker kill -s KILL 94c6b3c3f04a

上面命令的意思是殺死一個 ID 為 94c6b3c3f04a 的容器,並向容器發送 KILL 信號。

參數:

  -s, --signal=KILL    Signal to send to the container 自定義發送至容器的信號

14. docker events

從服務器拉取個人動態,可選擇時間區間。

docker events --since="20150720" --until="20150808"

上面命令的意思是拉取個人從 2015/07/20 到 2015/08/08 的個人動態。

參數:

  -f, --filter=[]    Filter output based on conditions provided--since=           Show all events created since timestamp 開始時間
  --until=           Stream events until this timestamp 結束時間

15. docker save

將指定鏡像保存成 tar 歸檔文件, docker load 的逆操作。保存后再加載(saved-loaded)的鏡像不會丟失提交歷史和層,可以回滾。

docker save -o ubuntu14.04.tar ubuntu:14.04

上面命令的意思是將鏡像 ubuntu:14.04 保存為 ubuntu14.04.tar 文件。

參數:

  -o, --output=      Write to an file, instead of STDOUT 輸出到的文件

16. docker load

從 tar 鏡像歸檔中載入鏡像, docker save 的逆操作。保存后再加載(saved-loaded)的鏡像不會丟失提交歷史和層,可以回滾。

docker load -i ubuntu14.04.tar

上面命令的意思是將 ubuntu14.04.tar 文件載入鏡像中。

參數:

  -i, --input=       Read from a tar archive file, instead of STDIN 加載的tar文件

17. docker export

將指定的容器保存成 tar 歸檔文件, docker import 的逆操作。導出后導入(exported-imported))的容器會丟失所有的提交歷史,無法回滾。

docker export -o ubuntu14.04.tar 94c6b3c3f04a

上面命令的意思是將 ID 為 94c6b3c3f04a 容器保存為 ubuntu14.04.tar 文件。

參數:

  -o, --output=      Write to a file, instead of STDOUT

18. docker import

從歸檔文件(支持遠程文件,.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz)創建一個鏡像, export 的逆操作,可為導入鏡像打上標簽。導出后導入(exported-imported))的容器會丟失所有的提交歷史,無法回滾。

cat ./ubuntu14.04.tar | sudo docker import - ubuntu:14.04

上面命令的意思是使用 ./ubuntu14.04.tar 文件創建 ubuntu:14.04 的鏡像,默認會從遠端拉取文件。

19. docker top

查看一個正在運行容器進程,支持 ps 命令參數。

20. docker inspect

檢查鏡像或者容器的參數,默認返回 JSON 格式。 (Template

docker inspect --format '{{.DockerVersion}}' ubuntu:14.04

上面命令的意思是返回 ubuntu:14.04  鏡像的 docker 版本

參數:

  -f, --format=      Format the output using the given go template 指定返回值的模板文件

21. docker pause

暫停某一容器的所有進程。

22. docker unpause

恢復某一容器的所有進程。

23. docker tag

標記本地鏡像,將其歸入某一倉庫。

sudo docker tag 5db5f8471261 harrysun/lnmp:0.2

上面命令的意思是將 ID 為 5db5f8471261 的容器標記為 harrysun/lnmp:0.2 鏡像。

參數:

  -f, --force=false    Force 會覆蓋已有標記

24. docker push

將鏡像推送至遠程倉庫,默認為 Docker Hub 。

docker push harrysun/lnmp:0.2

上面命令的意思是將 harrysun/lnmp:0.2 鏡像推送到遠端。

25. docker logs

獲取容器運行時的輸出日志。

docker logs -f --tail 10 94c6b3c3f04a

上面命令的意思是將追蹤 ID 為 94c6b3c3f04a 的容器最新的10條日志。

參數:

  -f, --follow=false        Follow log output 跟蹤容器日志的最近更新
  --since=                  Show logs since timestamp 開始時間
  -t, --timestamps=false    Show timestamps 顯示容器日志的時間戳
  --tail=all                Number of lines to show from the end of the logs 僅列出最新n條容器日志

26. docker run

啟動一個容器,在其中運行指定命令。

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

上面命令的意思是以 ubuntu:14.04 鏡像啟動一個容器,以交互模式運行,並為容器重新分配一個偽輸入終端。

參數:(這個命令的參數有點多,只說其中一部分) 

  -a, --attach=[]             Attach to STDIN, STDOUT or STDERR 指定標准輸入輸出內容類型,可選 STDIN/STDOUT/STDERR 三項
  --add-host=[]               Add a custom host-to-IP mapping (host: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
  --cap-drop=[]               Drop Linux capabilities
  --cgroup-parent=            Optional parent cgroup for the container
  --cidfile=                  Write the container ID to the file
  --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) 綁定容器到指定CPU運行
  --cpuset-mems=              MEMs in which to allow execution (0-3, 0,1) 綁定容器到指定MEM運行
  -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服務器,默認和宿主一致
  --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 file of environment variables 從指定文件讀入環境變量
  --expose=[]                 Expose a port or a range of ports
  -h, --hostname=             Container host name 指定容器的hostname
  --help=false                Print usage
  -i, --interactive=false     Keep STDIN open even if not attached 以交互模式運行容器,通常與 -t 同時使用
  --ipc=                      IPC 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
  -m, --memory=               Memory limit
  --mac-address=              Container MAC address (e.g. 92:d0:c6:0a:29:33)
  --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  指定容器的網絡連接類型,支持 bridge/host/none/container:<name|id> 四種類型
  --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
  --pid=                      PID namespace to use
  --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 為容器重新分配一個偽輸入終端,通常與 -i 同時使用
  -u, --user=                 Username or UID (format: <name|uid>[:<group|gid>])
  --ulimit=[]                 Ulimit options
  --uts=                      UTS namespace to use
  -v, --volume=[]             Bind mount a volume
  --volumes-from=[]           Mount volumes from the specified container(s)
  -w, --workdir=              Working directory inside the container

 

暫且就先到這里了,有什么問題,歡迎大家隨時留言~~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM