1. 程式人生 > >docker筆記之初體驗(容器管理)

docker筆記之初體驗(容器管理)

容器基本操作:

建立一個容器

 [[email protected] ~]# docker run -itd ubuntu
 3635f4dd3ae30a4377fc4f2eafc191b7890bbe5873bb4e5f9e5077f794466ec4

檢視容器程序

 [[email protected] ~]# docker ps 
 CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
 3635f4dd3ae3        ubuntu              "/bin/bash"         8 seconds ago       Up 6 seconds                            romantic_lamarr
 [
[email protected]
~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3635f4dd3ae3 ubuntu "/bin/bash" 15 seconds ago Up 13 seconds romantic_lamarr [
[email protected]
~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3635f4dd3ae3 ubuntu "/bin/bash" About a minute ago Up About a minute

開啟容器:

 [[email protected]
~]# docker start romantic_lamarr romantic_lamarr

進入容器進行操作:

 [[email protected] ~]# docker attach romantic_lamarr
 [email protected]:/# ls
 bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
 boot  etc  lib   media  opt  root  sbin  sys  usr
 [email protected]:/# exit
 exit

停止容器

 [[email protected] ~]# docker stop romantic_lamarr
 romantic_lamarr

刪除容器

 [[email protected] ~]# docker rm romantic_lamarr
 romantic_lamarr
 [[email protected] ~]# docker start romantic_lamarr
 Error response from daemon: No such container: romantic_lamarr
 Error: failed to start containers: romantic_lamarr

PS:使用rm需要先停止使用容器(stop),強行刪除可以加 -r 引數選項

重新命名容器

 [[email protected] ~]# docker run -itd ubuntu
 992dec8c882cbc3115045be8d7e76b496733e5996e4e083aebaa00e1ccef2a23
 [[email protected] ~]# docker ps
 CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
 992dec8c882c        ubuntu              "/bin/bash"         5 seconds ago       Up 3 seconds                            reverent_hamilton
 [[email protected] ~]# docker rename reverent_hamilton jinc
 [[email protected] ~]# docker ps
 CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
 992dec8c882c        ubuntu              "/bin/bash"         22 seconds ago      Up 21 seconds                           jinc

容器更多操作:

檢視容器詳細資訊(擷取部分)

 [[email protected] ~]# docker inspect jinc
 [
     {
         "Id": "992dec8c882cbc3115045be8d7e76b496733e5996e4e083aebaa00e1ccef2a23",
         "Created": "2018-11-06T09:26:25.660163089Z",
         "Path": "/bin/bash",
         "Args": [],
         "State": {
             "Status": "running",
             "Running": true,
             "Paused": false,
             "Restarting": false,
             "OOMKilled": false,
             "Dead": false,
             "Pid": 55541,
             "ExitCode": 0,
             "Error": "",
             "StartedAt": "2018-11-06T09:26:26.727348683Z",
             "FinishedAt": "0001-01-01T00:00:00Z"

不進入容器執行命令(擷取部分)

 [[email protected] ~]# docker exec jinc ls
 bin
 boot
 dev
 etc

容器中執行的程序

 [[email protected] ~]# docker top jinc
 UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
 root                55541               55527               0                   17:26               pts/0               00:00:00            /bin/bash

埠對映資訊(沒做對映,所以這裡沒有顯示)

 [[email protected] ~]# docker port jinc

拷貝檔案到容器

 [[email protected] ~]# touch a.txt
 [[email protected] ~]# docker cp a.txt jinc:/home
 [[email protected] ~]# docker exec jinc ls /home
 a.txt

從容器拉取檔案到本機

 [[email protected] ~]# docker cp jinc:/home/a.txt /tmp/
 [[email protected] ~]# ls /tmp/
 a.txt

檢視讀寫層操作(新增檔案)

 [[email protected] ~]# docker diff jinc
 C /home
 A /home/a.txt

檢視容器標準輸出日誌

 [[email protected] ~]# docker logs jinc

動態檢視容器利用率

 [[email protected] ~]# docker stats jinc
 
 CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
 992dec8c882c        jinc                0.00%               404KiB / 1.779GiB   0.02%               648B / 0B           0B / 0B             1

靜態檢視容器利用率

 [[email protected] ~]# docker stats --no-stream jinc
 CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
 992dec8c882c        jinc                0.00%               404KiB / 1.779GiB   0.02%               648B / 0B           0B / 0B             1

動態修改容器硬體引數

 [[email protected] ~]# docker update --help
 
 Usage:	docker update [OPTIONS] CONTAINER [CONTAINER...]
 
 Update configuration of one or more containers
 
 Options:
       --blkio-weight uint16        Block IO (relative weight), between 10 and
                                    1000, or 0 to disable (default 0)
       --cpu-period int             Limit CPU CFS (Completely Fair Scheduler) period
       --cpu-quota int              Limit CPU CFS (Completely Fair Scheduler) quota
       --cpu-rt-period int          Limit the CPU real-time period in microseconds
       --cpu-rt-runtime int         Limit the CPU real-time runtime in microseconds
   -c, --cpu-shares int             CPU shares (relative weight)
       --cpus decimal               Number of CPUs
       --cpuset-cpus string         CPUs in which to allow execution (0-3, 0,1)
       --cpuset-mems string         MEMs in which to allow execution (0-3, 0,1)
       --kernel-memory bytes        Kernel memory limit
   -m, --memory bytes               Memory limit
       --memory-reservation bytes   Memory soft limit
       --memory-swap bytes          Swap limit equal to memory plus swap: '-1' to
                                    enable unlimited swap
       --restart string             Restart policy to apply when a container exits

實時監控容器操作日誌

 [[email protected] ~]# docker events