1. 程式人生 > >42 【docker】run命令

42 【docker】run命令

最常用的兩個option是,網路埠對映,和檔案共享

 

最基本的啟動命令(從image建立一個container並啟動):docker run -d <image-name>

-d:表示守護程序方式啟動

-v:本地host的檔案(資料夾)對映到容器內的檔案(資料夾)

想起的-v引數是:-v [[host-dir:] container-dir [:OPTIONS]]

-v的第一個引數是host-dir,這個引數是可選引數,如果沒設定,docker會在host主機中建立該資料夾

第二個引數是container-dir,這個引數是必選引數,是容器內部的資料夾

第三個引數是OPTIONS,有幾種選項:

[rw|ro],讀寫或者只讀

[z|Z],不太瞭解(這裡是為了適應需要有標籤的OS-SElinux系統的,z表示和宿主機共享標籤,Z表示container是私有標籤)

[[r]shared|[r]slave|[r]private],不太瞭解,(預設是[r]private,docker內部對共享的dir的操作,在host主機上是不可見的;[r]shared,host和container上的修改都是共享的,雙方的修改對於對方都可見;[r]slave,表明host上做的修改在container是可見的,但是container上的改動,host上是不可見的)

[nocopy],不允許container中,從其他資料夾下拷貝檔案到該資料夾下

 

-p:埠對映

貼一下man原文

       -p, --publish=[]
          Publish a container's port, or range of ports, to the host.

       Format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort Both hostPort and containerPort can be specified as a range of ports.  When
       specifying ranges 
for both, the number of container ports in the range must match the number of host ports in the range. (e.g., docker run -p 1234-1236:1222-1224 --name thisWorks -t busybox but not docker run -p 1230-1236:1230-1240 --name RangeContainerPortsBiggerThanRangeHostPorts -t busybox) With ip: docker run -p 127.0.0.1:$HOSTPORT:$CONTAINERPORT --name CONTAINER -t someimage Use docker port to see the actual mapping: docker port CONTAINER $CONTAINERPORT

-p選項,把container中的埠或者一段埠釋出到主機上

格式如下:ip:hostPort:containerPort  | ip::containerPort | hostPort:containerPort | containerPort

格式1:把containerPort釋出到ip:hostPort(原因是有的host是多個IP的,這個命令可以指定釋出到一個ip上)

格式2:ip::containerPort(和格式1類似,不過hostPort和containerPort一致時可以不用再寫hostPort)

格式3:hostPort:containerPort(主機是單個IP時,不用再額外指定ip,直接使用hostPort+containerPort即可)

格式4:containerPort(主機IP單一,並且hostPort=containerPort時,可以忽略hostPort)