Docker-學習二(容器基本操作)

內容提要

docker容器相關操作,包括容器啓停,查看,刪除,創建,導入導出,內存配置等。

1.創建並啓動容器

1.新建容器

可以使用docker create命令新建一個容器,例如:

 docker create -it ubuntu:latest

2.啓動容器

使用docker create
命令新建的容器處於停止狀態需要配合使用docker start 命令啓動。

docker start ssf

3.新建並啓動容器

可以使用docker run 命令來直接新建並啓動容器。(如果本地鏡像有則啓動,沒有則獲取最新鏡像)
這裏用ubuntu鏡像echo一個hello word

 docker run ubuntu /bin/echo 'Hello world'

下面的命令啓動一個bash終端,允許用戶進行交互 並在後臺運行:

 docker run -it -d ubuntu:14.04 /bin/bash

—P參數是隨機指定一個32768~61000的主機端口與容器綁定,而-p則是指定一個端口與容器綁定

docker run -d -p 80:80 nginx:v1.0

命令詳解

-a, --attach=[]             Attach to STDIN, STDOUT or 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)
--cpuset-mems=              MEMs 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
--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
--init=                     Run container following specified init system container method (systemd)
--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
--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
-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

2.終止容器

1.容器終止

可以使用docker stop來終止運行中的容器,格式爲:
docker stop[-t|–time[=10]][CONTAINER…]
首先會向容器發送SIGTERM信號,等待一段超時時間(默認爲 10秒)後,再發送 SIGKILL 信號來終止容器。

 docker stop ce5

使用 docker kill 命令直接強行終止容器。

docker kill ce5

2.容器重啓

終止狀態的容器可以使用docker start 啓動

docker start ce5

在運行狀態的容器可以使用docker restart重啓

docker restart ce5

3.進入容器

進入容器有三種方法,其中主要推薦使用第一種exec方法,其他方法瞭解作用即可。

1.exec命令(重點)

Docker從1.3.0版本起提供了一個更加方便的exec命令,可以在容器內直接執行任意命令。該命令的基本格式爲:
docker exec [-d|–detach] [–detach-keys[=[]]] [-i|–interactive] [–privileged]
[-t|–tty] [-u|–user[=USER]] CONTAINER COMMAND
[ARG…] 。

docker exec -it 243c32535da7 /bin/bash

通過指定- it 參數來保持標準輸入打開,並且分配一個僞終端。

相關參數

·-i,--interactive=true| false:打開標準輸入接受用戶輸入命令,默認爲false;
·--privileged=true| false:是否給執行命令以高權限,默認爲false;
·-t,--tty=true| false:分配僞終端,默認爲false;
·- u,--user="":執行命令的用戶名或ID。

2.attach命令

命令格式:docker attach [–detach-keys[=[]]] [–no-stdin] [–sig-proxy[=true]] CONTAINER
支持三個主要選項:

·--detach-keys[=[]]:指定退出attach 模式的快捷鍵序列,默認是 CTRL-p CTRL-q;
·--no-stdin=true| false:是否關閉標準輸入,默認是保持打開;
·--sig-proxy=true| false:是否代理收到的系統信號給應用進程,默認爲true

使用示例:

docker attach myredis 

當多個窗口同時用attach命令連到同一個容器的時候,所有窗口都會同步顯示。當某個窗口因命令阻塞時,其他窗口也無法執行操作了。

3.nsenter工具

在util-linux 軟件包版本2.23+中包含nsenter工具。如果系統中的 util-linux包沒有該命令需下載。(略…)

4. 容器操作

在容器中操作時,使用ps 報錯,是因爲ps沒有安裝在基礎wheezy圖像。

apt-get update && apt-get install procps

同樣在容器中安裝vim等工具時,需要先使用apt-get update 同步一下容器內和服務器的更新源,纔可安裝。

4.刪除容器

使用docker rm 命令刪除處於終止或者退出狀態的容器。命令格式爲:docker rm[-f|–force][-l|–link][-v|–volumes]CONTAINER[CONTAINER…]

docker rm ce554267d7a4

默認情況下rm命令只能刪除停止或者退出狀態的容器,但是可以添加-f參數,將容器kill後強行刪除。

docker rm -f ce554267d7a4

###相關參數

·- f,--force=false:是否強行終止並刪除一個運行中的容器;
·- l,--link=false:刪除容器的連接,但保留容器;
·- v,--volumes=false:刪除容器掛載的數據卷。

5.導入導出容器

1.導出容器

命令的格式爲 docker export[-o|-- output[=""]]CONTAINER

docker export -o test_for_run.tar ce5
 或
docker export e81 >test_for_stop.tar

2.導入容器

和導入鏡像一樣可以使用docker import將tar導入變成鏡像,命令格式爲:
docker import [-c|–change[=[]]] [-m|–message[=MESSAGE]] file|URL|-[REPOSITORY [:TAG]]
導入時可以定義標籤信息

 docker import test_for_run.tar - test/ubuntu:v1.0

在使用docker load 導入由docker export保存容器生成的tar時會報異常信息:

open /var/lib/docker/tmp/docker-import-283259618/repositories: no such file or directory

導入容器tar需使用docker import命令

6.容器資源配置

使用docker stats命令來看當前服務器的內存,針對各個場景對相應容器分配合理內存資源。

docker stats

使用docker的update命令來對內存大小進行管理分配

docker update -m 350m  --memory-swap -1  mysqlserver

–memory-swap -1 參數是指不讓容器和宿主機進行內存交換

--automated=true| false:僅顯示自動創建的鏡像,默認爲否;
·--no-trunc=true| false:輸出信息不截斷顯示,默認爲否;
·- s,--stars=X:指定僅顯示評價爲指定星級以上的鏡像,默認爲0,即輸出所有鏡像。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-1TWPwciG-1574869123039)(https://i.loli.net/2019/11/25/csIVayoYXUhRrDp.png)]

參考資料:Docker技術入門與實戰

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章