栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Docker常用命令

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Docker常用命令

基本命令

Docker 的帮助命令是一个万能命令,可以用来查看 Docker 的所有命令。

docker [命令] --help

查看镜像相关命令

[root@sail ~]# docker images --help
Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show image IDs

查看容器相关命令

[root@sail ~]# docker ps --help
Usage:  docker ps [OPTIONS]
List containers
Options:
  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print containers using a Go template
  -n, --last int        Show n last created containers (includes all states) (default -1)
  -l, --latest          Show the latest created container (includes all states)
      --no-trunc        Don't truncate output
  -q, --quiet           Only display container IDs
  -s, --size            Display total file sizes

查看 Docker 的基本信息。

[root@sail ~]# docker version

查看 Docker 的系统信息。

[root@sail ~]# docker info
镜像命令

查看所有本地主机上的镜像

# 该命令等价于 docker image ls
docker images [参数] [镜像[:标签]]
    -a:显示所有镜像。
    -q:只显示 ID。
# 显示所有镜像的 ID
docker images -aq

对镜像进行过滤

[root@sail ~]# docker images java
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@sail ~]# docker images hello-world
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB
[root@sail ~]# docker images hello-world:latest
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB

# 结果分析
REPOSITORY:镜像名(镜像仓库源)。
TAG:镜像的标签。
IMAGE ID:镜像的 ID。
CREATED:镜像的创建时间。
SIZE:镜像的大小。

搜索镜像

docker search [参数]
	-f / --filter:根据过滤条件搜索。
	
# 搜索出 Stars 大于 3000 的
docker search mysql -f=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   11789     [OK]       
mariadb   MariaDB Server is a high performing open sou…   4488      [OK]

拉取镜像

# 不输入参数默认拉取最新的
docker pull [参数] 镜像名[:标签]
    -a:拉取镜像的所有标签。
    -q:抑制详细输出。
    
[root@sail ~]# docker pull redis
Using default tag: latest # 默认最新版标签
latest: Pulling from library/redis
e5ae68f74026: Pull complete # 分层下载,docker image的核心:联合文件系统
37c4354629da: Pull complete 
b065b1b1fa0f: Pull complete 
6954d19bb2e5: Pull complete 
6333f8baaf7c: Pull complete 
f9772c8a44e7: Pull complete 
Digest: sha256:2f502d27c3e9b54295f1c591b3970340d02f8a5824402c8179dcd20d4076b796 #防伪签名
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest # 真实地址,docker pull redis 等价于 docker pull docker.io/library/redis:latest

# 指定版本下载
docker pull redis:5.0

删除镜像

docker rmi [参数] 镜像 [镜像...]
	-f:强制删除。
	
# 查看现存镜像
[root@sail ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
redis         5.0       071510791e92   5 days ago     110MB
redis         latest    aea9b698d7d1   6 days ago     113MB
mysql         5.7       738e7101490b   6 days ago     448MB
mysql         latest    bbf6571db497   6 days ago     516MB
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB

# 删除一个。可以通过名称,也可以指定 ID,-f 表示强制删除。
[root@sail ~]# docker rmi -f feb5d9fea6a5

# 删除多个。用空格分隔 ID。
[root@sail ~]# docker rmi -f 738e7101490b bbf6571db497

# 删除所有。先用 docker images -aq 查询出所有镜像,再使用 docker rmi -f 递归删除。
[root@sail ~]# docker rmi -f $(docker images -aq)

运行镜像

docker run [参数] 镜像名
	--name:指定容器的名称,如果正在运行该名称的容器,会报错。
    --rm:用完即删除,通常用来测试。
    -d:后台方式运行。
    -it:使用交互方式运行,可以进入容器查看内容。
    -e:指定运行环境。
    -p:随机指定端口。
    -p:指定容器的端口,如:-p 8080:8080。还可以有以下写法:
        -p ip:主机端口:容器端口
        -p 主机端口:容器端口
        -p 容器端口
        
[root@sail ~]# docker run -it centos /bin/bash
Unable to find image 'centos:latest' locally # 检索本地镜像,发现没有该镜像,则去仓库中搜索。
latest: Pulling from library/centos # 开始从仓库中拉取
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
[root@81c83ea42dc0 /]# ls # 由于是以交互方式运行,且进入 /bin/bash 中,此时的路径即为 centos 容器中的 /bin/bash
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
容器命令

查看容器

docker ps [参数]
	-a:查看所有容器(包括正在运行的和已经停止的)。
    -n:显示最近创建的容器,设置显示个数。
    -q:只显示容器的编号。
    
# 查看正在运行的容器
[root@sail ~]# docker ps

# 查看所有容器
[root@sail ~]# docker ps -a

# 显示最近创建的 2 个容器
[root@sail ~]# docker ps -a -n=2

# 只显示容器的 ID 	
[root@sail ~]# docker ps -aq

退出容器

[root@sail ~]# docker run -it centos /bin/bash
[root@7ac04abd5a1f /]# exit
exit
[root@sail ~]#
[root@sail ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@sail ~]# docker run -it centos /bin/bash
[root@1aaf76d85b9e /]# [root@sail ~]# docker ps # 此时即为使用 Ctrl + P + Q 快捷键的效果
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
1aaf76d85b9e   centos    "/bin/bash"   8 seconds ago   Up 8 seconds             intelligent_proskuriakova

删除容器

docker rm [参数] 容器 [容器...]
	-f:强制删除。
	
# 删除指定容器(不能删除正在运行的容器)
[root@sail ~]# docker rm 1aaf76d85b9e

# 强制删除指定容器
[root@sail ~]# docker rm -f 1aaf76d85b9e

# 删除所有容器。先使用 docker ps -aq 获取所有容器的 ID,再调用 docker rm -f 递归删除。
[root@sail ~]# docker rm -f $(docker ps -aq)

# 删除所有容器。使用管道符 | 获取 Docker 相关的所有容器 ID 并使用 docker rm -f 删除。
[root@sail ~]# docker ps -a -q|xargs docker rm -f

容器运行命令

# 运行关闭的容器
[root@sail ~]# docker start 569026bc0955 

# 停止容器
[root@sail ~]# docker stop 569026bc0955

# 重启容器
[root@sail ~]# docker restart 569026bc0955

# 杀掉容器
[root@sail ~]# docker kill 569026bc0955
常用命令

启动Docker

systemctl start docker

查看日志

 docker logs [参数] 容器
 	-f:日志流动输出。
    -t:展示时间戳。
    --tail:从日志末尾显示的行数。
    
# 容器最后 10 条日志
[root@sail ~]# docker logs -f -t --tail 10 c3d59f55d600

后台启动

# 使用 docker run -d 启动,也并不能保证容器一定能在后台运行,如果没有前台使用,容器启动后发现自己没有提供服务,会立刻停止。
docker run -d 镜像

查看容器信息

# 不管容器是否运行,都可以使用该命令查看。
docker inspect 容器

进入正在运行的容器

docker exec [参数] 容器 路径
	-d:后台运行。
	-it:交互模式进入。
	
# 使用 docker exec 可以进入容器并开启一个新的终端,可以在里面操作。
# 这种进入方式是单独开了一个新进程的方式。
[root@sail ~]# docker exec -it 96ed3fe3e7f1 /bin/bash
[root@96ed3fe3e7f1 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

[root@96ed3fe3e7f1 /]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Dec09 pts/0    00:00:00 /bin/bash
root        15     0  0 03:38 pts/1    00:00:00 /bin/bash
root        30    15  0 03:39 pts/1    00:00:00 ps -ef
# 这种进入方式没有开启新的进程(/bin/bash 是 centos 容器的默认终端)。
docker attach 容器

[root@sail ~]# docker attach 96ed3fe3e7f1
[root@96ed3fe3e7f1 /]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Dec09 pts/0    00:00:00 /bin/bash
root        33     1  0 05:49 pts/0    00:00:00 ps -ef

从容器内拷贝文件到主机

# 进入容器,创建一个文件
[root@sail ~]# docker attach 96ed3fe3e7f1
[root@96ed3fe3e7f1 /]# cd /home
[root@96ed3fe3e7f1 home]# touch test.java
[root@96ed3fe3e7f1 home]# ls
test.java
[root@96ed3fe3e7f1 home]# exit
exit

# 退出容器后,不管容器是否启动,都可以复制容器中的文件到主机上
[root@sail ~]# cd /home
[root@sail home]# docker cp 96ed3fe3e7f1:/home/test.java /home
[root@sail home]# ls
admin  f2  f3  sail  test.java

# 这种方式是一个手动过程,很不方便,推荐使用数据卷技术,可以实现自动同步主机和容器的目录。

查看Docker内存占用

docker stats [参数] [容器...]
	-a:查看所有容器的内存占用(默认只展示运行的容器)。

[root@sail home]# docker stats
CONTAINER ID   NAME       CPU %     MEM USAGE / LIMIT   MEM %     NET I/O   BLOCK I/O   PIDS
96ed3fe3e7f1   centos01   0.00%     524KiB / 1.694GiB   0.03%     0B / 0B   0B / 0B     1

[root@sail home]# docker stats -a
CONTAINER ID   NAME              CPU %     MEM USAGE / LIMIT   MEM %     NET I/O   BLOCK I/O   PIDS
0aee6f74b913   brave_rosalind    0.00%     0B / 0B             0.00%     0B / 0B   0B / 0B     0
c3d59f55d600   musing_poincare   0.00%     0B / 0B             0.00%     0B / 0B   0B / 0B     0
569026bc0955   centos03          0.00%     0B / 0B             0.00%     0B / 0B   0B / 0B     0
71a97b830ec5   centos02          0.00%     0B / 0B             0.00%     0B / 0B   0B / 0B     0
96ed3fe3e7f1   centos01          0.00%     524KiB / 1.694GiB   0.03%     0B / 0B   0B / 0B     1

小结

[root@192 home]# docker --help
Usage:    docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
      --config string      Location of client config
                           files (default "/root/.docker")
  -c, --context string     Name of the context to use to
                           connect to the daemon
                           (overrides DOCKER_HOST env var
                           and default context set with
                           "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level
                           ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by
                           this CA (default
                           "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file
                           (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default
                           "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit
Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes
Commands:
   #当前shell下 attach连接指定运行的镜像
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile # 通过Dockerfile定制镜像
  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 #查看docker容器的变化
  events      Get real time events from the server # 从服务获取容器实时时间
  exec        Run a command in a running container # 在运行中的容器上运行命令
  export      Export a container's filesystem as a tar archive #导出容器文件系统作为一个tar归档文件[对应import]
  history     Show the history of an image # 展示一个镜像形成历史
  images      List images #列出系统当前的镜像
  import      Import the contents from a tarball to create a filesystem image #从tar包中导入内容创建一个文件系统像
  info        Display system-wide information # 显示全系统信息
  inspect     Return low-level information on Docker objects #查看容器详细信息
  kill        Kill one or more running containers # kill指定docker容器
  load        Load an image from a tar archive or STDIN #从一个tar包或标准输入中加载一个镜像[对应save]
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  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
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  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
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/868465.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号