docker知识4---容器

一、容器简介

容器:通过镜像创建,在镜像分层之上添加可读写层;二者关系类似于程序中类与实例。image负责应用的存储及分发,container负责运行应用;容器和虚拟机都依赖于宿主机才能运行。用户也同样可以从单个镜像上启动一个或多个容器。容器ID仅用于区分不同的容器,可不写全,只要系统能识别即可。

容器类别:
  1)执行一次便退出的容器;
  2)常驻内存的容器;
容器运行方式:
  1)前台运行容器(默认):docker run ...
  2)前台交互式运行容器:docker run -it ...
  3)后台运行容器:docker run -d ...
容器进入方式:
  1)使用docker attach进入Docker容器 #docker attach 容器ID;使用该命令有一个问题。当多个窗口同时使用该命令进入该容器时,所有的窗口都会同步显示。如果有一个窗口阻塞了,那么其他窗口也无法再进行操作。所以docker attach命令不太适合于生产环境,平时自己开发应用时可以使用该命令。
  2)使用SSH进入Docker容器 #在生产环境中排除了使用docker attach命令进入容器之后,相信大家第一个想到的就是ssh。在镜像(或容器)中安装SSH Server,这样就能保证多人进入;但是使用了Docker容器之后不建议使用ssh进入到Docker容器内。容器的目的是隔离应用,优点是小巧而安全;但增加SSH服务器后会有安全问题,你会在必要的时候不得不升级所有使用SSH的容器。
  3)使用nsenter进入Docker容器 #nsenter可以访问另一个进程的名称空间。所以为了连接到某个容器我们还需要获取该容器的第一个进程的PID。可以使用docker inspect命令来拿到该PID。
  4)使用docker exec进入Docker容器 #这种方式相对更简单易用;
  5)使用docker run -ti进入容器 #这种方式相对更简单易用;

 

Usage:  docker container COMMAND

Manage containers

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  ls          List containers
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune       Remove all stopped containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes

docker container ls -a #列出所有容器;
docker container ls #列出运行的容器;  

 

posted on 2021-07-28 09:22  chalon  阅读(92)  评论(0编辑  收藏  举报