1. 程式人生 > >DOCKER學習之(二) 容器部分常用命令

DOCKER學習之(二) 容器部分常用命令

一、執行容器

Usage: 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)
  -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 --cpuset-cpus= CPUs in which to allow execution (0-3, 0,1) -d, --detach=false Run
container in background and print container 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//見Dockfile中的ENV --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 --help=false Print usage -i, --interactive=false Keep STDIN open even if not attached --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 --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 -P, --publish-all=false Publish all exposed ports to random ports//公開Dockfile 中的保留埠到隨機埠 -p, --publish=[] Publish a container's port(s) to the host //宿主機埠:容器埠(80則宿主機埠隨機,(1)在ps中可以檢視,(2) docker port name/id 埠) --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//檢查容器退出程式碼,自動重啟容器。例如: --restart=on-failure:5 //退出程式碼為非0時,才會自動重啟。--restart=always 總是重啟 --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, --user= Username or UID (format: <name|uid>[:<group|gid>])//見Dockfile中的USER --ulimit=[] Ulimit options -v, --volume=[] Bind mount a volume//載入卷列表 見Dockfile中的VOLUME --volumes-from=[] Mount volumes from the specified container(s) -w, --workdir= Working directory inside the container//指定容器工作目錄 見Dockfile中的WORKDIR

例如1 以互動方式執行一個容器

sudo docker run -it ubuntu /bin/bash 

例如2給容器指定一個命名

sudo docker run --name one-name -it ubuntu /bin/bash 

例如3建立守護式容器

sudo docker run --name one-name -d ubuntu /bin/sh -c "while true;do echo hello world;sleep 1;done"

二、 容器日誌

Usage: docker logs [OPTIONS] CONTAINER

Fetch the logs of a container
引數
  -f, --follow=false        Follow log output
  --help=false              Print usage
  -t, --timestamps=false    Show timestamps
  --tail=all                Number of lines to show from the end of the logs

例如:
sudo docker logs name/id

–tail 0 -f 最新日誌
–tail 10 -f 最後10行記錄

三、 檢視容器內的程序

Usage: docker top [OPTIONS] CONTAINER [ps OPTIONS]

例如:

 sudo docker top name/id

四、 在容器內部執行程序

Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
引數:
  -d, --detach=false         Detached mode: run command in the background
  --help=false               Print usage
  -i, --interactive=false    Keep STDIN open even if not attached
  -t, --tty=false            Allocate a pseudo-TTY

例如:互動式命令

     sudo docker exec -it name/id /bin/bash

建立檔案

 sudo docker exec -d  name/id touch /etc/new_file

五、停止守護容器

Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop a running container by sending SIGTERM and then SIGKILL after a
grace period

  --help=false       Print usage
  -t, --time=10      Seconds to wait for stop before killing it

六、重新啟動已經停止的容器(restart)

Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]

Start one or more stopped containers
引數
  -a, --attach=false         Attach STDOUT/STDERR and forward signals
  --help=false               Print usage
  -i, --interactive=false    Attach container's STDIN

例如:

sudo docker start name/id
 sudo docker restart name/id

七、刪除容器

Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers
引數
  -f, --force=false      Force the removal of a running container (uses SIGKILL)
  --help=false           Print usage
  -l, --link=false       Remove the specified link
  -v, --volumes=false    Remove the volumes associated with the container

八、深入容器資訊

包括配置資訊,名稱,命令、網路配置以及很多有用資料

Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]

Return low-level information on a container or image
引數
  -f, --format=      Format the output using the given go template
  --help=false       Print usage

九、 容器列表

Usage: docker ps [OPTIONS]

List containers
引數:
  -a, --all=false       Show all containers (default shows just running)
  --before=             Show only container created before Id or Name
  -f, --filter=[]       Filter output based on conditions provided
  --help=false          Print usage
  -l, --latest=false    Show the latest created container, include non-running
  -n=-1                 Show n last created containers, include non-running
  --no-trunc=false      Don't truncate output
  -q, --quiet=false     Only display numeric IDs
  -s, --size=false      Display total file sizes
  --since=              Show created since Id or Name, include non-running