当前位置:   article > 正文

docker入门(四)—— docker常用命令详解

docker入门(四)—— docker常用命令详解

docker 常用命令

基本命令

# 查看 docker 版本
docker version
# 查看一些 docker 的详细信息
docker info 
  • 1
  • 2
  • 3
  • 4

帮助命令(–help),linux必须要会看帮助文档

docker --help
  • 1
[root@iZbp15293q8kgzhur7n6kvZ /]# docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Common Commands:
  run         Create and run a new container from an image
  exec        Execute a command in a running container
  ps          List containers
  build       Build an image from a Dockerfile
  pull        Download an image from a registry
  push        Upload an image to a registry
  images      List images
  login       Log in to a registry
  logout      Log out from a registry
  search      Search Docker Hub for images
  version     Show the Docker version information
  info        Display system-wide information

Management Commands:
  builder     Manage builds
  buildx*     Docker Buildx (Docker Inc., v0.13.0)
  compose*    Docker Compose (Docker Inc., v2.24.7)
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  plugin      Manage plugins
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Swarm Commands:
  swarm       Manage Swarm

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
  events      Get real time events from the server
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  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
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  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
  wait        Block until one or more containers stop, then print their exit codes

Global 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 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

Run 'docker COMMAND --help' for more information on a command.

For more help on how to use Docker, head to https://docs.docker.com/go/guides/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84

docker COMMAND --help可以继续查看其它命令的帮助文档

镜像命令

展示所有的镜像

docker images
  • 1
[root@iZbp15293q8kgzhur7n6kvZ /]# docker images
# REPOSITORY:镜像仓库
# TAG:标签(版本号),latest 代表最新版
# IMAGE ID:镜像ID,是简化后的
# CREATED:创建时间
# SIZE:镜像大小
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    d2c94e258dcb   10 months ago   13.3kB
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

参数:

  • -a:等价于--all,显示所有的镜像,包括中间层镜像

默认情况下,docker images命令只显示顶层镜像,即那些没有被其他镜像所依赖的镜像。而使用-a参数,会显示所有的镜像,包括中间层镜像。中间层镜像是构建镜像过程中生成的临时镜像,它们可能被其他镜像所依赖。

  • -q:等价于--quiet,只展示镜像的 id

搜索镜像

docker search 镜像名称
  • 1
[root@iZbp15293q8kgzhur7n6kvZ /]# docker search mysql
# STARS:星数,最多的通常为我们要找的官方镜像
NAME                            DESCRIPTION                                     STARS     OFFICIAL
mysql                           MySQL is a widely used, open-source relation…   14944     [OK]
mariadb                         MariaDB Server is a high performing open sou…   5703      [OK]
phpmyadmin                      phpMyAdmin - A web interface for MySQL and M…   960       [OK]
percona                         Percona Server is a fork of the MySQL relati…   626       [OK]
bitnami/mysql                   Bitnami MySQL Docker Image                      110       
bitnami/mysqld-exporter                                                         6         
cimg/mysql                                                                      3         
ubuntu/mysql                    MySQL open source fast, stable, multi-thread…   61        
rapidfort/mysql                 RapidFort optimized, hardened image for MySQL   25        
rapidfort/mysql8-ib             RapidFort optimized, hardened image for MySQ…   9         
google/mysql                    MySQL server for Google Compute Engine          25        
elestio/mysql                   Mysql, verified and packaged by Elestio         0         
rapidfort/mysql-official        RapidFort optimized, hardened image for MySQ…   9         
bitnamicharts/mysql                                                             0         
hashicorp/mysql-portworx-demo                                                   0         
databack/mysql-backup           Back up mysql databases to... anywhere!         111       
linuxserver/mysql               A Mysql container, brought to you by LinuxSe…   41        
mirantis/mysql                                                                  0         
docksal/mysql                   MySQL service images for Docksal - https://d…   0         
linuxserver/mysql-workbench                                                     55        
vitess/mysqlctld                vitess/mysqlctld                                1         
eclipse/mysql                   Mysql 5.7, curl, rsync                          1         
drupalci/mysql-5.5              https://www.drupal.org/project/drupalci         3         
drupalci/mysql-5.7              https://www.drupal.org/project/drupalci         0         
datajoint/mysql                 MySQL image pre-configured to work smoothly …   2         
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

参数:

  • -f:等价于--filter,根据条件搜索想要的镜像
# 过滤出星数不小于500的镜像
docker search mysql -f=STARS=500
  • 1
  • 2
[root@iZbp15293q8kgzhur7n6kvZ /]# docker search mysql -f=STARS=500
NAME         DESCRIPTION                                     STARS     OFFICIAL
mysql        MySQL is a widely used, open-source relation…   14944     [OK]
mariadb      MariaDB Server is a high performing open sou…   5703      [OK]
phpmyadmin   phpMyAdmin - A web interface for MySQL and M…   960       [OK]
percona      Percona Server is a fork of the MySQL relati…   626       [OK]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

拉取镜像

docker pull 镜像名称:版本号
# 如果不加版本号,则拉取最新版本的镜像
  • 1
  • 2
[root@iZbp15293q8kgzhur7n6kvZ /]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete 
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
688ba7d5c01a: Pull complete 
00e060b6d11d: Pull complete 
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
# 签名校验id,主要是保证不会被掉包
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

# 下载指定版本
[root@iZbp15293q8kgzhur7n6kvZ /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists 
93619dbc5b36: Already exists 
99da31dd6142: Already exists 
626033c43d70: Already exists 
37d5d7efb64e: Already exists 
ac563158d721: Already exists 
d2ba16033dad: Already exists 
0ceb82207cd7: Pull complete 
37f2405cae96: Pull complete 
e2482e017e53: Pull complete 
70deed891d42: Pull complete 
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

删除镜像

docker rmi 镜像名称或镜像ID
  • 1
[root@iZbp15293q8kgzhur7n6kvZ /]# docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 578ad7d53c37 is using its referenced image d2c94e258dcb
  • 1
  • 2

这里删除失败是因为 hello-world 镜像有容器正在运行,如果要删除只能强制删除

参数:

  • -f:等价于--force,强制删除镜像,即使有容器正在运行
# 强制删除
[root@iZbp15293q8kgzhur7n6kvZ /]# docker rmi -f hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:6352af1ab4ba4b138648f8ee88e63331aae519946d3b67dae50c313c6fc8200f
Deleted: sha256:d2c94e258dcb3c5ac2798d32e1249e42ef01cba4841c2234249495f87264ac5a
# 根据镜像ID删除镜像
[root@iZbp15293q8kgzhur7n6kvZ /]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql        5.7       c20987f18b13   2 years ago   448MB
mysql        latest    3218b38490ce   2 years ago   516MB
[root@iZbp15293q8kgzhur7n6kvZ /]# docker rmi -f 3218b38490ce
Untagged: mysql:latest
Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

这里镜像ID不一定要是完整的,只要有唯一的一个镜像ID开头满足你输入的ID即可

# 只输入开头的c20即可删除
[root@iZbp15293q8kgzhur7n6kvZ /]# docker rmi -f c20
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76
Deleted: sha256:6567396b065ee734fb2dbb80c8923324a778426dfd01969f091f1ab2d52c7989
Deleted: sha256:0910f12649d514b471f1583a16f672ab67e3d29d9833a15dc2df50dd5536e40f
Deleted: sha256:6682af2fb40555c448b84711c7302d0f86fc716bbe9c7dc7dbd739ef9d757150
Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92
Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba
Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7
Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da
Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838
Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0
Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa
Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

删除所有镜像,慎用

重新拉取几个镜像进行测试

删除命令如下:

docker rmi -f $(docker images -aq)
  • 1
[root@iZbp15293q8kgzhur7n6kvZ /]# docker rmi -f $(docker images -aq)
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
  • 1
  • 2
  • 3
  • 4
  • 5

容器命令

有镜像才能创建容器,我们这里使用 centos 的镜像来测试

根据镜像创建并运行一个容器

docker run 镜像名称
  • 1
[root@iZbp15293q8kgzhur7n6kvZ /]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@iZbp15293q8kgzhur7n6kvZ /]# docker run centos
# docker run 执行时自带了 docker pull命令
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

参数:

  • --name:为容器指定一个名称,不指定则随机一个名字
  • -d:等价于--detach,后台启动容器并打印容器id
  • -i:等价于--interactive,让容器用交互的方式启动
  • -t:等价于--tty,给容器分配一个伪终端,例如/bin/bash
  • -p:等价于--publish,指定端口映射(主机访问的端口:容器端口)
  • -P:大写P,等价于--publish-all,将所有公开的端口发布到随机端口

用交互的方式启动容器,并进入容器内部

docker run -it 镜像名称
  • 1
[root@iZbp15293q8kgzhur7n6kvZ /]# docker run -it centos
[root@c98e20511eb4 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
  • 1
  • 2
  • 3

退出容器

# 命令,退出后容器停止运行
exit
# 快捷键,退出后容器不会停止
Ctrl + P + Q
  • 1
  • 2
  • 3
  • 4

查看正在运行中的容器

docker ps
  • 1
[root@iZbp15293q8kgzhur7n6kvZ /]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED       STATUS       PORTS     NAMES
c98e20511eb4   centos    "/bin/bash"   2 hours ago   Up 2 hours             vibrant_rosalind
  • 1
  • 2
  • 3

参数:

  • -a:等价于--all,显示所有容器,包括已经停止的
  • -q:等价于--quiet,只展示容器ID

容器的启动、重启、停止命令

# 启动一个已经停止的容器
docker start 容器名或容器ID
# 重启一个容器,包括已经停止的容器
docker restart 容器名或容器ID
# 停止一个容器
docker stop 容器名或容器ID
# 强制停止一个容器
docker kill 容器名或容器ID
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

docker stopdocker kill区别:

docker stop命令会向容器发送一个优雅的停止信号(SIGTERM),让容器有机会进行清理和停止。容器将会接收到该信号并尝试优雅地停止运行中的进程。如果容器在一定时间内没有停止,docker 将会发送一个强制停止信号(SIGKILL)来强制终止容器。这个命令给容器一个正常停止的机会,并且容器可以执行一些清理操作,如保存数据、关闭连接等。

docker kill命令会立即向容器发送一个强制终止信号(SIGKILL),不给容器进行任何清理操作的机会。容器将会立即停止,正在运行的进程将会被强制中断。这个命令是一种强制终止容器的方式,适用于需要立即停止容器的情况。

删除容器

docker rm 容器ID或容器名
  • 1
[root@iZbp15293q8kgzhur7n6kvZ /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
7d1b6cececcc   centos         "/bin/bash"   11 minutes ago   Up 11 minutes                         happy_ardinghelli
962ead7ba991   centos         "/bin/bash"   11 minutes ago   Up 11 minutes                         nifty_lovelace
c98e20511eb4   centos         "/bin/bash"   2 hours ago      Up 2 hours                            vibrant_rosalind
d6afbc8233e8   centos         "/bin/bash"   3 hours ago      Exited (0) 3 hours ago                admiring_galois
ba50251e5f7e   centos         "/bin/bash"   3 hours ago      Exited (0) 3 hours ago                nervous_euler
578ad7d53c37   d2c94e258dcb   "/hello"      5 hours ago      Exited (0) 12 minutes ago             gallant_bhaskara
[root@iZbp15293q8kgzhur7n6kvZ /]# docker rm 578a
578a
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

参数:

  • -f:等价于--force,强制删除容器
# 删除所有容器,包括正在运行和已经停止的,慎用
docker rm -f $(docker ps -aq)
  • 1
  • 2
[root@iZbp15293q8kgzhur7n6kvZ /]# docker rm -f $(docker ps -aq)
7d1b6cececcc
962ead7ba991
c98e20511eb4
d6afbc8233e8
ba50251e5f7e
[root@iZbp15293q8kgzhur7n6kvZ /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

以后台方式启动容器 -d

docker run -d centos
  • 1
[root@iZbp15293q8kgzhur7n6kvZ /]# docker run -d centos
de7eb928d553c5df52f94e27acf604745fa15eb24c40fee60e4059e2bb60b5dd
[root@iZbp15293q8kgzhur7n6kvZ /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
  • 1
  • 2
  • 3
  • 4

以后台方式启动容器,查看正在运行时的容器发现没有刚刚启动的容器,这是因为这个镜像 centos 启动的容器里并没有前台进程,所以刚启动一下子就停止了,如果想查询到这个容器,需要加上-a参数

[root@iZbp15293q8kgzhur7n6kvZ /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
de7eb928d553   centos    "/bin/bash"   2 minutes ago   Exited (0) 2 minutes ago             happy_ride
  • 1
  • 2
  • 3

可以使用-it参数以交互方式启动 centos,这样就有了一个前台进程,容器就不会停止了

[root@iZbp15293q8kgzhur7n6kvZ /]# docker run -itd centos 
b82d98613fd44437b1aa4a1bbff63075604a547ce878e0df9ff76ee13d1b56aa
[root@iZbp15293q8kgzhur7n6kvZ /]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
b82d98613fd4   centos    "/bin/bash"   2 seconds ago   Up 2 seconds             beautiful_ramanujan
  • 1
  • 2
  • 3
  • 4
  • 5

查看日志

docker logs 容器名或容器id
  • 1

参数:

  • -t:等价于--timestamps,打印时间戳
  • -f:等价于--follow,打印最新日志并继续输出
  • -n:等价于--tail,从日志末尾输出的行数

这里我们启动一个可以输出日志的容器

# shell 脚本(一个死循环,每隔1s,输出akuya)
docker run -d centos /bin/sh -c "while true;do echo akuya;sleep 1;done"
  • 1
  • 2
[root@iZbp15293q8kgzhur7n6kvZ /]# docker run -d centos /bin/sh -c "while true;do echo akuya;sleep 1;done"
5cbf1cd1a21bee0de30fdf53f2bbfc0b690b90b06ce0cd90d4a3e0962de32eab
[root@iZbp15293q8kgzhur7n6kvZ /]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
5cbf1cd1a21b   centos    "/bin/sh -c 'while t…"   4 seconds ago   Up 4 seconds             romantic_villani
b82d98613fd4   centos    "/bin/bash"              5 minutes ago   Up 5 minutes             beautiful_ramanujan
# 先打印最新5行日志,然后继续输出新的日志,使用快捷键 ctrl+c 停止
[root@iZbp15293q8kgzhur7n6kvZ /]# docker logs -ft -n 5 5cbf1cd1a21b
2024-03-19T13:15:50.993510891Z akuya
2024-03-19T13:15:51.995897632Z akuya
2024-03-19T13:15:52.998156257Z akuya
2024-03-19T13:15:54.000528678Z akuya
2024-03-19T13:15:55.002960756Z akuya
2024-03-19T13:15:56.005416926Z akuya
2024-03-19T13:15:57.007909978Z akuya
2024-03-19T13:15:58.010417478Z akuya
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

查看容器相关的进程(top)

docker top 容器名或容器id
  • 1
[root@iZbp15293q8kgzhur7n6kvZ /]# docker top 5cbf1cd1a21b
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                15397               15376               0                   21:11               ?                   00:00:00            /bin/sh -c while true;do echo akuya;sleep 1;done
root                15975               15397               0                   21:20               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
  • 1
  • 2
  • 3
  • 4

查看容器的元数据(inspect)

docker inspect 容器名或容器id
  • 1
[root@iZbp15293q8kgzhur7n6kvZ /]# docker inspect 5cbf1cd1a21b
[
    {
        "Id": "5cbf1cd1a21bee0de30fdf53f2bbfc0b690b90b06ce0cd90d4a3e0962de32eab",
        "Created": "2024-03-19T13:11:32.102190519Z",
        "Path": "/bin/sh",
        ......
]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

进入一个正在进行的容器

docker exec -it 容器名或容器id shell脚本程序(例如/bin/sh或/bin/bash,如果不加默认进入正在执行的终端)
docker attach 容器名或容器id
  • 1
  • 2
# 不加/bin/bash则会进入到正在运行的/bin/sh中,也就是那个死循环的程序
[root@iZbp15293q8kgzhur7n6kvZ /]# docker exec -it 5cbf1cd1a21b /bin/bash
[root@5cbf1cd1a21b /]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 13:11 ?        00:00:00 /bin/sh -c while true;do echo akuya;sleep 1;done
root      1138     0  0 13:30 pts/0    00:00:00 /bin/bash
root      1161     1  0 13:30 ?        00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root      1162  1138  0 13:30 pts/0    00:00:00 ps -ef
# 进入到正在执行的程序中,也就是死循环,需要重新启动一下服务器
[root@iZbp15293q8kgzhur7n6kvZ /]# docker attach 5cbf1cd1a21b
akuya
akuya
akuya
akuya
akuya
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

区别:

  • execdocker exec命令用于在正在运行的容器中执行一个新的命令。它会创建一个新的进程,并在容器中执行指定的命令。该命令会在容器的新进程中执行,不会影响容器原有的主进程。使用docker exec可以在容器内部执行命令、进入容器的交互式终端,并且可以在后台运行的容器中执行任务。
  • attachdocker attach命令用于连接到正在运行的容器的标准输入(stdin)、输出(stdout)和错误输出(stderr)。该命令会将你的终端会话附加到容器的主进程上,并将容器的输出直接显示在你的终端上。使用docker attach可以实时查看和参与到容器内部正在运行的进程中,但是当你退出终端会话时,容器将会停止运行。

拷贝容器内的文件到主机上

docker cp 容器名或容器id:容器内文件地址 主机地址
  • 1
[root@iZbp15293q8kgzhur7n6kvZ ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
5cbf1cd1a21b   centos    "/bin/sh -c 'while t…"   23 minutes ago   Up 23 minutes             romantic_villani
b82d98613fd4   centos    "/bin/bash"              28 minutes ago   Up 28 minutes             beautiful_ramanujan
[root@iZbp15293q8kgzhur7n6kvZ ~]# docker attach b82d98613fd4
[root@b82d98613fd4 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@b82d98613fd4 /]# cd /home
[root@b82d98613fd4 home]# touch a.txt
# 快捷键退出容器
[root@b82d98613fd4 home]# read escape sequence
[root@iZbp15293q8kgzhur7n6kvZ ~]# docker cp b82d98613fd4:/home/a.txt /home
Successfully copied 1.54kB to /home
[root@iZbp15293q8kgzhur7n6kvZ ~]# cd /home
[root@iZbp15293q8kgzhur7n6kvZ home]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 19 21:40 a.txt
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

常用命令总结

在这里插入图片描述

常用命令:

docker --help 

attach    Attach to a running container                 # 当前 shell 下 attach 连接指定运行镜像
build     Build an image from a Dockerfile              # 通过 Dockerfile 定制镜像
commit    Create a new image from a container changes   # 提交当前容器为新的镜像
cp        Copy files/folders from the containers filesystem to the host path   #从容器中拷贝指定文件或者目录到宿主机中
create    Create a new container                        # 创建一个新的容器,同 run,但不启动容器
diff      Inspect changes on a container's filesystem   # 查看 docker 容器变化
events    Get real time events from the server          # 从 docker 服务获取容器实时事件
exec      Run a command in an existing container        # 在已存在的容器上运行命令
export    Stream the contents of a container as a tar archive   # 导出容器的内容流作为一个 tar 归档文件[对应 import ]
history   Show the history of an image                  # 展示一个镜像形成历史
images    List images                                   # 列出系统当前镜像
import    Create a new filesystem image from the contents of a tarball # 从tar包中的内容创建一个新的文件系统映像[对应export]
info      Display system-wide information               # 显示系统相关信息
inspect   Return low-level information on a container   # 查看容器详细信息
kill      Kill a running container                      # kill 指定 docker 容器
load      Load an image from a tar archive              # 从一个 tar 包中加载一个镜像[对应 save]
login     Register or Login to the docker registry server    # 注册或者登陆一个 docker 源服务器
logout    Log out from a Docker registry server          # 从当前 Docker registry 退出
logs      Fetch the logs of a container                 # 输出当前容器日志信息
port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT    # 查看映射端口对应的容器内部源端口
pause     Pause all processes within a container        # 暂停容器
ps        List containers                               # 列出容器列表
pull      Pull an image or a repository from the docker registry server   # 从docker镜像源服务器拉取指定镜像或者库镜像
push      Push an image or a repository to the docker registry server    # 推送指定镜像或者库镜像至docker源服务器
restart   Restart a running container                   # 重启运行的容器
rm        Remove one or more containers                 # 移除一个或者多个容器
rmi       Remove one or more images             # 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
run       Run a command in a new container              # 创建一个新的容器并运行一个命令
save      Save an image to a tar archive                # 保存一个镜像为一个 tar 包[对应 load]
search    Search for an image on the Docker Hub         # 在 docker hub 中搜索镜像
start     Start a stopped containers                    # 启动容器
stop      Stop a running containers                     # 停止容器
tag       Tag an image into a repository                # 给源中镜像打标签
top       Lookup the running processes of a container   # 查看容器中运行的进程信息
unpause   Unpause a paused container                    # 取消暂停容器
version   Show the docker version information           # 查看 docker 版本号
wait      Block until a container stops, then print its exit code   # 截取容器停止时的退出状态值
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/300412
推荐阅读
相关标签
  

闽ICP备14008679号