目录

一:什么是容器

二:容器常用操作命令

2.1容器列表

2.2:启动新容器

2.3:启动已终止容器

2.4:重启容器

2.5:停止容器

2.6:强制停止容器

2.7:删除容器

2.8:进入容器

2.9:查看容器日志

2.10:查看容器内运行的进程

2.11:查看容器详细信息

2.12:复制容器内部的文件或文件夹(不推荐)

2.13: 查看所有容器资源使用情况

2.14: 查看所有容器明细(比较实用)

2.15: 容器其他操作

三:容器run参数解读


一:什么是容器

容器的实质是进程,但与直接在宿主执行的进程不同,容器进程运行于属于自己的独立的 命名空间

因此容器可以拥有自己的 root 文件系统、自己的网络配置、自己的进程空间,甚至自己的用户 ID 空间。

容器内的进程是运行在一个隔离的环境里,使用起来,就好像是在一个独立于宿主的系统下操作一样。

二:容器常用操作命令

宿主机直接操作容器内部命令(不进入容器)

docker container exec -t 容器id cat /etc/os-release

2.1容器列表

docker container ls : 默认展示运行中的  -a 展示所有的

2.2:启动新容器

docker container run -itd -p 5000:80 --restart=always --name name nginx
    -> -t 选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上
    -> -i 则让容器的标准输入保持打开。
    -> -d 则让容器守护态运行
    -> -p: 发布容器对外端口到主机  -p 宿主机IP:容器内IP
    -> -P: 随机分配宿主机端口

2.3:启动已终止容器

docker container start 容器id

2.4:重启容器

docker container restart 容器id1 容器id2

2.5:停止容器

docker container stop 容器id

2.6:强制停止容器

docker container kill 容器id

2.7:删除容器

删除已停止的容器:docker container rm 容器id
删除运行中的容器:先停止再删除或强制删除:docker container rm -f 容器id

docker container rm -f $(docker container ps -q -a)

2.8:进入容器

docker container exec -it 容器id bash

2.9:查看容器日志

docker container logs 容器id
   docker container logs -tf --tail 10 33d977a9da0f 实时打印最后10条内容
   日志存储位置:ls /var/lib/docker/containers/容器ID/xxx-json.log

2.10:查看容器内运行的进程

docker container top 容器id

2.11:查看容器详细信息

docker container inspect  容器id

2.12:复制容器内部的文件或文件夹(不推荐)

docker container cp 4d6865824cc2:/tmp/11.txt /tmp

2.13: 查看所有容器资源使用情况

ocker container stats   查看所有容器使用情况
          -a 容器id  查看单个容器的使用情况
查看容器服务器资源使用情况
CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT   MEM %     NET I/O       BLOCK I/O         PIDS
79294570cbe5   n2        0.00%     27.97MiB / 512MiB   5.46%     2.21kB / 0B   24.6kB / 16.4kB   33

2.14: 查看所有容器明细(比较实用)

docker container inspect 容器名称|容器ID
    -> 可以看到网络Networks
    -> 可以看到磁盘挂载Volumes、Mounts

2.15: 容器其他操作

[root@B32 docker]# docker container --help
Commands:
  attach      附加本地标准输入,输出和错误到一个运行的容器
  commit      创建一个新镜像来自一个容器 (不推荐)
  cp          拷贝文件、文件夹到一个容器 (不推荐)

  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  exec        进入容器,在运行容器中执行命令
  export      Export a container's filesystem as a tar archive
  inspect     显示一个或多个容器的详细信息
  kill        Kill one or more running containers
  logs        获取一个容器的日志
  ls          列出容器
  pause       Pause all processes within one or more containers
  port        列出或指定容器端口映射
  prune       Remove all stopped containers
  rename      Rename a container
  restart     重启一个或多个容器
  rm          删除一个或多个容器
  run         Run a command in a new container
  start       启动一个或多个容器
  stop        停止一个或多个容器
  stats       显示容器资源使用情况  -a 容器ID:显示单个
  top         显示一个容器运行的进程 docker top nginx 显示nginx中的一个进程
  unpause     Unpause all processes within one or more containers
  update      更新一个或多个容器配置
  wait        Block until one or more containers stop, then print their exit codes

三:容器run参数解读

docker container run 参数解读
[root@B32 docker]# docker container run --help

Options: 
  -i, --interactive                    交互式          
  -t, --tty                            分配一个伪终端 
  -d, --detach                         在后台分离运行容器并打印容器ID  
  -e, --env list                       设置环境变量
          docker container run -itd -e a=123 --name n1 nginx
          docker exec -it n1 bash 
          root@5bd4b92f76ec:/# echo $a
          123
      --env-file list                  从文件读取环境变量
  -p, --publish list                   发布容器对外端口到主机
       docker container run -itd -p 5000:80
  -P, --publish-all                    Publish all exposed ports to random ports
      --pull string                    Pull image before running ("always"|"missing"|"never") (default "missing")
      --read-only                      Mount the container's root filesystem as read only
      --restart string                 容器退出时重启策略,默认no  [always|]
                                       --restart=always   
      --rm                             Automatically remove the container when it exits
      --runtime string                 Runtime to use for this container
      --security-opt list              Security Options
      --shm-size bytes                 Size of /dev/shm
      --sig-proxy                      Proxy received signals to the process (default true)
      --stop-signal string             Signal to stop a container (default "SIGTERM")
      --stop-timeout int               Timeout (in seconds) to stop a container
      --storage-opt list               Storage driver options for the container
      --sysctl map                     Sysctl options (default map[])
      --tmpfs list                     Mount a tmpfs directory
  -h, --hostname string                设置容器主机名
  
  
  
  -a, --attach list                    Attach to STDIN, STDOUT or STDERR
      --blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
      --blkio-weight-device list       Block IO weight (relative device weight) (default [])
      --cap-add list                   Add Linux capabilities
      --cap-drop list                  Drop Linux capabilities
      --cgroup-parent string           Optional parent cgroup for the container
      --cgroupns string                Cgroup namespace to use (host|private)
                                       'host':    Run the container in the Docker host's cgroup namespace
                                       'private': Run the container in its own private cgroup namespace
                                       '':        Use the cgroup namespace as configured by the
                                                  default-cgroupns-mode option on the daemon (default)
      --cidfile string                 Write the container ID to the file
      --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 CPU real-time period in microseconds
      --cpu-rt-runtime int             Limit CPU real-time runtime in microseconds
  -c, --cpu-shares int                 此值设置为大于或小于默认1024值,以增加或减少容器的权重,
                                       并使其可以访问主机CPU周期的更大或更小比例
      --cpus decimal                   限制容器可以使用多少可用的cpu资源
      --cpuset-cpus string             限制容器可以使用特定的cpu  (0-3, 0,1)
      --cpuset-mems string             MEMs in which to allow execution (0-3, 0,1)

  -l, --label list                     Set meta data on a container
      --label-file list                Read in a line delimited file of labels
      --link list                      Add link to another container
      --link-local-ip list             Container IPv4/IPv6 link-local addresses
      --log-driver string              Logging driver for the container
      --log-opt list                   Log driver options
      --mac-address string             Container MAC address (e.g., 92:d0:c6:0a:29:33)
  -m, --memory bytes                   容器可以使用的最大内存量
      --memory-reservation bytes       
          内存软限制,Docker检测主机容器争用或内存不足时所激活的软限制,
          使用此选项,值必须设置低于-memory, 以使其优先
      --memory-swap bytes              允许交换到磁盘的内存量(一般是内存2倍)
      --memory-swappiness int          容器使用swap分区交换的百分比(0-100)(default -1)
         
      --mount mount                    Attach a filesystem mount to the container
      --name string                    Assign a name to the container
      --network network                Connect a container to a network
      --network-alias list             Add network-scoped alias for the container
      --no-healthcheck                 Disable any container-specified HEALTHCHECK
      --oom-kill-disable               
            当宿主机内存不足时,内核会杀死容器中的进程。
            建议设置了-memory选项在禁用oom。如果没有设置,主机可能会耗尽内存。
            
      --oom-score-adj int              Tune host's OOM preferences (-1000 to 1000)
      --pid string                     PID namespace to use
      --pids-limit int                 Tune container pids limit (set -1 for unlimited)
      --platform string                Set platform if server is multi-platform capable
      --privileged                     Give extended privileges to this container
  
  
  
  -u, --user string                    Username or UID (format: <name|uid>[:<group|gid>])
      --userns string                  User namespace to use
      --uts string                     UTS namespace to use
  -v, --volume list                    Bind mount a volume
      --volume-driver string           Optional volume driver for the container
      --volumes-from list              Mount volumes from the specified container(s)
  -w, --workdir string                 Working directory inside the container