04-Docker-Container管理操作

04-Docker-Container管理操作

Docker Version: 19.03.5

😄 Written by Zak Zhu

参考

  • Breeze老师的docker培训
  • 马哥docker视频

容器运行须知

  • 容器是为进程任务而生的
  • 容器的生命周期是短暂的

一个容器建议只运行一个进程(容器内PID为1), 而且这个进程需要在容器内以Foreground方式运行, 不能以Background方式运行. 如果进程退出了, 那么容器就发现Foreground Stdout不被占据, 于是容器也随之退出

容器运行过程:

  1. 检查本地是否存在指定的镜像, 如果没有就从指定的仓库下载
  2. 利用镜像启动一个容器
  3. 分配一个文件系统, 并在只读的镜像层上面挂载一层可读写层
  4. 从宿主机配置的网桥接口中桥接一个虚拟接口到容器中去
  5. 从地址池配置一个IP给容器
  6. 执行用户指定的程序
  7. 执行完毕后停止容器

容器生命周期

0

容器管理命令

1


1. 生命周期

  • run

    Run a command in a new container

    Usage:

    docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]

    OPTION COMMENT
    --detach Run container in background and print container ID
    --interactive Keep STDIN open even if not attached
    --tty Allocate a pseudo-TTY
    --rm Automatically remove the container when it exits
    ... ...

    Often use:

    • 在后台运行容器及应用

      docker container run --detach IMAGE [COMMAND] [ARG...]
      
    • 在前台运行容器并交互

      docker container run --interactive --tty IMAGE /bin/sh
      

  • start

    Start one or more stopped containers

    Usage:

    docker container start [OPTIONS] CONTAINER [CONTAINER...]

    Often use:

    • 启动容器

      docker container start CONTAINER [CONTAINER...]
      

  • stop

    Stop one or more running containers

    Usage:

    docker container stop [OPTIONS] CONTAINER [CONTAINER...]

    Often use:

    • 停止容器

      docker container stop CONTAINER [CONTAINER...]
      

  • kill

    Kill one or more running containers

    Usage:

    docker container kill [OPTIONS] CONTAINER [CONTAINER...]

    Often use:

    • 杀掉容器

      docker container kill CONTAINER [CONTAINER...]
      

  • restart

    Restart one or more containers

    Usage:

    docker container restart [OPTIONS] CONTAINER [CONTAINER...]

    Often use:

    • 重启容器

      docker container restart CONTAINER [CONTAINER...]
      

  • rm

    Remove one or more containers

    Usage:

    docker container rm [OPTIONS] CONTAINER [CONTAINER...]

    OPTION COMMENT
    --force Force the removal of a running container (uses SIGKILL)
    --volumes Remove the volumes associated with the container
    ... ...

    Often use:

    • 删除已退出的容器

      docker container rm CONTAINER [CONTAINER...]
      
    • 删除正运行的容器

      docker container rm --force CONTAINER [CONTAINER...]
      
    • 删除容器及挂载卷

      docker container rm --volumes CONTAINER [CONTAINER...]
      


2. 查看操作

  • ls

    List containers

    Usage:

    docker container ls [OPTIONS]

    OPTION COMMENT
    --all Show all containers (default shows just running)
    --no-trunc Don't truncate output
    --quiet Only display numeric IDs
    --size Display total file sizes
    --latest Show the latest created container (includes all states)
    ... ...

    Often use:

    • 列出所有容器及其状态

      docker container ls [ --all ]
      
    • 显示最近一次创建容器

      docker container ls --latest
      
    • 列出所有容器的ID

      docker container ls --quiet [ --no-trunc ]
      
    • 列出所有容器的SIZE

      docker container ls --size
      

  • inspect

    Display detailed information on one or more containers

    Usage:

    docker container inspect [OPTIONS] CONTAINER [CONTAINER...]

    Often use:

    • 显示容器详细信息

      docker container inspect CONTAINER [CONTAINER...]
      

  • logs

    Fetch the logs of a container

    Usage:

    docker container logs [OPTIONS] CONTAINER

    OPTION COMMENT
    --details Show extra details provided to logs
    --follow Follow log output
    --since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
    --tail string Number of lines to show from the end of the logs (default "all")
    --timestamps Show timestamps
    --until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)

  • top

    Display the running processes of a container

    Usage:

    docker container top CONTAINER [ps OPTIONS]

    Often use:

    • 在宿主机上查看容器应用进程

      docker container top CONTAINER 
      

  • stats

    Display a live stream of container(s) resource usage statistics

    Usage:

    docker container stats [OPTIONS] [CONTAINER...]

    OPTION COMMENT
    --all Show all containers (default shows just running)
    --no-stream Disable streaming stats and only pull the first result
    ... ...

    Often use:

    • 查看所有容器的资源使用情况统计

      docker container stats [ --all ] [ --no-stream ]
      
    • 查看一个容器的资源使用情况统计

      docker container stats [ --no-stream ] CONTAINER
      


3. 命令执行

  • exec

    Run a command in a running container

    Usage:

    docker container exec [OPTIONS] CONTAINER COMMAND [ARG...]

    OPTION COMMENT
    --env list Set environment variables
    --interactive Keep STDIN open even if not attached
    --privileged Give extended privileges to the command
    --tty Allocate a pseudo-TTY
    --user string Username or UID (format: <name|uid>[:<group|gid>])
    --workdir string Working directory inside the container
    ... ...

    Often use:

    • 执行容器中的命令

      docker container exec CONTAINER COMMAND [ARG...]
      
    • 进入容器的SHELL

      docker container exec --interactive --tty CONTAINER /bin/sh
      


4. 文件复制

  • cp

    Copy files/folders between a container and the local filesystem

    Usage:

    docker container cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH

    docker container cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH

    OPTION COMMENT
    --archive Archive mode (copy all uid/gid information)
    --follow-link Always follow symbol link in SRC_PATH


容器用途分类

容器按用途大致可分为两类:

  • 服务类容器

    如 web server、database等

    E.G. 运行httpd容器

    docker container run --name="web-server" --detach httpd
    

    2

  • 工具类容器

    如curl容器,redis-cli容器

    E.G. 运行curl容器

    docker container run --interactive --tty --rm appropriate/curl curl 172.17.0.5
    

    3

posted @ 2020-01-07 22:24  ZakZhu  阅读(296)  评论(0编辑  收藏  举报