docker笔记之初体验(容器管理)

版权声明:from 瑾川(fakehydra.xyz) https://blog.csdn.net/fake_hydra/article/details/83792852

容器基本操作:

创建一个容器

 [root@ct7-mb ~]# docker run -itd ubuntu
 3635f4dd3ae30a4377fc4f2eafc191b7890bbe5873bb4e5f9e5077f794466ec4

查看容器进程

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

开启容器:

 [root@ct7-mb ~]# docker start  romantic_lamarr
 romantic_lamarr

进入容器进行操作:

 [root@ct7-mb ~]# docker attach romantic_lamarr
 root@3635f4dd3ae3:/# ls
 bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
 boot  etc  lib   media  opt  root  sbin  sys  usr
 root@3635f4dd3ae3:/# exit
 exit

停止容器

 [root@ct7-mb ~]# docker stop romantic_lamarr
 romantic_lamarr

删除容器

 [root@ct7-mb ~]# docker rm romantic_lamarr
 romantic_lamarr
 [root@ct7-mb ~]# docker start romantic_lamarr
 Error response from daemon: No such container: romantic_lamarr
 Error: failed to start containers: romantic_lamarr

PS:使用rm需要先停止使用容器(stop),强行删除可以加 -r 参数选项

重命名容器

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

容器更多操作:

查看容器详细信息(截取部分)

 [root@ct7-mb ~]# 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"

不进入容器执行命令(截取部分)

 [root@ct7-mb ~]# docker exec jinc ls
 bin
 boot
 dev
 etc

容器中运行的进程

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

端口映射信息(没做映射,所以这里没有显示)

 [root@ct7-mb ~]# docker port jinc

拷贝文件到容器

 [root@ct7-mb ~]# touch a.txt
 [root@ct7-mb ~]# docker cp a.txt jinc:/home
 [root@ct7-mb ~]# docker exec jinc ls /home
 a.txt

从容器拉取文件到本机

 [root@ct7-mb ~]# docker cp jinc:/home/a.txt /tmp/
 [root@ct7-mb ~]# ls /tmp/
 a.txt

查看读写层操作(新增文件)

 [root@ct7-mb ~]# docker diff jinc
 C /home
 A /home/a.txt

查看容器标准输出日志

 [root@ct7-mb ~]# docker logs jinc

动态查看容器利用率

 [root@ct7-mb ~]# 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

静态查看容器利用率

 [root@ct7-mb ~]# 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

动态修改容器硬件参数

 [root@ct7-mb ~]# 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

实时监控容器操作日志

 [root@ct7-mb ~]# docker events 

猜你喜欢

转载自blog.csdn.net/fake_hydra/article/details/83792852
今日推荐