Docker run :創建一個新的容器並運行一個命令
語法
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS說明:
01.[root@www ~]# docker run --help 02. 03.Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 04. 05.Run a command in a new container 06. 07. -a, --attach=[] Attach to STDIN, STDOUT or STDERR 08. --add-host=[] Add a custom host-to-IP mapping (host:ip) 09. --blkio-weight=0 Block IO (relative weight), between 10 and 1000 10. --cpu-shares=0 CPU shares (relative weight) 11. --cap-add=[] Add Linux capabilities 12. --cap-drop=[] Drop Linux capabilities 13. --cgroup-parent= Optional parent cgroup for the container 14. --cidfile= Write the container ID to the file 15. --cpu-period=0 Limit CPU CFS (Completely Fair Scheduler) period 16. --cpu-quota=0 Limit CPU CFS (Completely Fair Scheduler) quota 17. --cpuset-cpus= CPUs in which to allow execution (0-3, 0,1) 18. --cpuset-mems= MEMs in which to allow execution (0-3, 0,1) 19. -d, --detach=false Run container in background and print container ID(后台運行) 20. --device=[] Add a host device to the container 21. --disable-content-trust=true Skip image verification 22. --dns=[] Set custom DNS servers 23. --dns-opt=[] Set DNS options 24. --dns-search=[] Set custom DNS search domains 25. -e, --env=[] Set environment variables(設置環境變量) 26. --entrypoint= Overwrite the default ENTRYPOINT of the image 27. --env-file=[] Read in a file of environment variables 28. --expose=[] Expose a port or a range of ports 29. --group-add=[] Add additional groups to join 30. -h, --hostname= Container host name 31. --help=false Print usage 32. -i, --interactive=false Keep STDIN open even if not attached(保持容器運行) 33. --ipc= IPC namespace to use 34. --kernel-memory= Kernel memory limit 35. -l, --label=[] Set meta data on a container 36. --label-file=[] Read in a line delimited file of labels 37. --link=[] Add link to another container(容器之間的通訊) 38. --log-driver= Logging driver for container 39. --log-opt=[] Log driver options 40. --lxc-conf=[] Add custom lxc options 41. -m, --memory= Memory limit 42. --mac-address= Container MAC address (e.g. 92:d0:c6:0a:29:33) 43. --memory-reservation= Memory soft limit 44. --memory-swap= Total memory (memory + swap), '-1' to disable swap 45. --memory-swappiness=-1 Tuning container memory swappiness (0 to 100) 46. --name= Assign a name to the container(指定容器名稱) 47. --net=default Set the Network for the container 48. --oom-kill-disable=false Disable OOM Killer 49. -P, --publish-all=false Publish all exposed ports to random ports 50. -p, --publish=[] Publish a container's port(s) to the host(端口映射 80:8080) 51. --pid= PID namespace to use 52. --privileged=false Give extended privileges to this container 53. --read-only=false Mount the container's root filesystem as read only 54. --restart=no Restart policy to apply when a container exits 55. --rm=false Automatically remove the container when it exits 56. --security-opt=[] Security Options 57. --shm-size= Size of /dev/shm, default value is 64MB 58. --sig-proxy=true Proxy received signals to the process 59. --stop-signal=SIGTERM Signal to stop a container, SIGTERM by default 60. -t, --tty=false Allocate a pseudo-TTY 61. -u, --user= Username or UID (format: <name|uid>[:<group|gid>]) 62. --ulimit=[] Ulimit options 63. --uts= UTS namespace to use 64. -v, --volume=[] Bind mount a volume(掛載目錄 /root:/opt/temp) 65. --volume-driver= Optional volume driver for the container 66. --volumes-from=[] Mount volumes from the specified container(s) 67. -w, --workdir= Working directory inside the container
實例
使用docker鏡像nginx:latest以后台模式啟動一個容器,並將容器命名為mynginx。
docker run --name mynginx -d nginx:latest
使用鏡像nginx:latest以后台模式啟動一個容器,並將容器的80端口映射到主機隨機端口。
docker run -P -d nginx:latest
使用鏡像nginx:latest以后台模式啟動一個容器,將容器的80端口映射到主機的80端口,主機的目錄/data映射到容器的/data。
docker run -p 80:80 -v /data:/data -d nginx:latest
使用鏡像nginx:latest以交互模式啟動一個容器,在容器內執行/bin/bash命令。
runoob@runoob:~$ docker run -it nginx:latest /bin/bash root@b8573233d675:/# Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 02. 03. -d, --detach=false 指定容器運行於前台還是后台,默認為false 04. -i, --interactive=false 打開STDIN,用於控制台交互 05. -t, --tty=false 分配tty設備,該可以支持終端登錄,默認為false 06. -u, --user="" 指定容器的用戶 07. -a, --attach=[] 登錄容器(必須是以docker run -d啟動的容器) 08. -w, --workdir="" 指定容器的工作目錄 09. -c, --cpu-shares=0 設置容器CPU權重,在CPU共享場景使用 10. -e, --env=[] 指定環境變量,容器中可以使用該環境變量 11. -m, --memory="" 指定容器的內存上限 12. -P, --publish-all=false 指定容器暴露的端口 13. -p, --publish=[] 指定容器暴露的端口 14. -h, --hostname="" 指定容器的主機名 15. -v, --volume=[] 給容器掛載存儲卷,掛載到容器的某個目錄 16. --volumes-from=[] 給容器掛載其他容器上的卷,掛載到容器的某個目錄 17. --cap-add=[] 添加權限,權限清單詳見:http://linux.die.net/man/7/capabilities 18. --cap-drop=[] 刪除權限,權限清單詳見:http://linux.die.net/man/7/capabilities 19. --cidfile="" 運行容器后,在指定文件中寫入容器PID值,一種典型的監控系統用法 20. --cpuset="" 設置容器可以使用哪些CPU,此參數可以用來容器獨占CPU 21. --device=[] 添加主機設備給容器,相當於設備直通 22. --dns=[] 指定容器的dns服務器 23. --dns-search=[] 指定容器的dns搜索域名,寫入到容器的/etc/resolv.conf文件 24. --entrypoint="" 覆蓋image的入口點 25. --env-file=[] 指定環境變量文件,文件格式為每行一個環境變量 26. --expose=[] 指定容器暴露的端口,即修改鏡像的暴露端口 27. --link=[] 指定容器間的關聯,使用其他容器的IP、env等信息 28. --lxc-conf=[] 指定容器的配置文件,只有在指定--exec-driver=lxc時使用 29. --name="" 指定容器名字,后續可以通過名字進行容器管理,links特性需要使用名字 30. --net="bridge" 容器網絡設置: 31. bridge 使用docker daemon指定的網橋 32. host //容器使用主機的網絡 33. container:NAME_or_ID >//使用其他容器的網路,共享IP和PORT等網絡資源 34. none 容器使用自己的網絡(類似--net=bridge),但是不進行配置 35. --privileged=false 指定容器是否為特權容器,特權容器擁有所有的capabilities 36. --restart="no" 指定容器停止后的重啟策略: 37. no:容器退出時不重啟 38. on-failure:容器故障退出(返回值非零)時重啟 39. always:容器退出時總是重啟 40. --rm=false 指定容器停止后自動刪除容器(不支持以docker run -d啟動的容器) 41. --sig-proxy=true 設置由代理接受並處理信號,但是SIGCHLD、SIGSTOP和SIGKILL不能被代理