03-Docker命令大全

docker参数 常用的指令 指令 描述 ls 列出镜像 build 构建镜像来自Dockerfile history 查看镜像历史 inspect 显示一个或多个镜像详细信息 pull 从镜像仓库拉取镜像 push 推送一个镜像到镜像仓库 rm 移除一个或多个镜像 prune 移除未使用的镜像。没有被标记或被任何容器引用的。 tag 创建一个引用源镜...

docker参数

 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
[root@localhost ~]# docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -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
  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:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a 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
  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
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  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
  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

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

常用的指令

指令 描述 ls 列出镜像 build 构建镜像来自Dockerfile history 查看镜像历史 inspect 显示一个或多个镜像详细信息 pull 从镜像仓库拉取镜像 push 推送一个镜像到镜像仓库 rm 移除一个或多个镜像 prune 移除未使用的镜像。没有被标记或被任何容器引用的。 tag 创建一个引用源镜像标记目标镜像 export 导出容器文件系统到tar归档文件 import 导入容器文件系统tar归档文件创建镜像 save 保存一个或多个镜像到一个tar归档文件 load 加载镜像来自tar归档或标准输入

查看容器

1
[root@localhost ~]# docker container ls

查看镜像

1
docker image ls

查看历史

1
docker history nginx

镜像详情

1
docker image inspect nginx 

下载镜像

1
docker pull centos:7

上传镜像

1
docker push dockerhub.xx.com/xxx/ubuntu:16.04

删除镜像

1
2
3
4
[root@localhost ~]# docker image rm -f nginx
Untagged: nginx:latest
Untagged: nginx@sha256:c6bcc3f6f4dfee535dc0cbdaa7f32901727dd93f92c8a45eacd5c6a6d080a9ad
[root@localhost ~]# 

移除为使用镜像

 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
[root@localhost ~]# docker image prune 
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
[root@localhost ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bb776ce48575        3 days ago          109MB
centos              7                   9f38484d220f        4 weeks ago         202MB
[root@localhost ~]# docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: centos:7
untagged: centos@sha256:2dc7ed6df9dcfa3e0129d8613a1e1b17f5683425d8fe81dd0c4166e6c211ac9f
untagged: centos@sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861
deleted: sha256:9f38484d220fa527b1fb19747638497179500a1bed8bf0498eb788229229e6e1
deleted: sha256:d69483a6face4499acb974449d1303591fcbb5cdce5420f36f8a6607bda11854
untagged: nginx:latest
deleted: sha256:bb776ce48575796501bcc53e511563116132b789ab0552d520513da8c738cba2
deleted: sha256:43e4bf6ebb72bc17f5f35af0ace5e5d5db31b2b631fac2cca3d4be2420ca9758
deleted: sha256:2ceeec5cb749c4154ae2390bc3e0c2a4dee8663ca7012ca37a293734de83d498
deleted: sha256:5dacd731af1b0386ead06c8b1feff9f65d9e0bdfec032d2cd0bc03690698feda

Total reclaimed space: 311.1MB
[root@localhost ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

打tag

1
2
3
4
5
6
7
8
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bb776ce48575        3 days ago          109MB
[root@localhost ~]# docker tag nginx caimengzhi_nginx
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
caimengzhi_nginx    latest              bb776ce48575        3 days ago          109MB
nginx               latest              bb776ce48575        3 days ago          109MB

容器导入导出

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
caimengzhi_nginx    latest              bb776ce48575        3 days ago          109MB
nginx               latest              bb776ce48575        3 days ago          109MB
[root@localhost ~]# docker run -itd -p88:80 caimengzhi_nginx
8670f902dc094eb1de78eeccac90a4618ab59a8ba787e6da6324589c1039daa7
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
8670f902dc09        caimengzhi_nginx    "nginx -g 'daemon of…"   2 seconds ago       Up 2 seconds        0.0.0.0:88->80/tcp   compassionate_gates
[root@localhost ~]# docker export 8670f902dc09 > caimengzhi_nginx.tar
[root@localhost ~]# du -sh caimengzhi_nginx.tar 
107M    caimengzhi_nginx.tar

导出后在本地可以看到有一个caimengzhi_nginx.tar的容器快照。

导入某个容器,有了容器快照之后,我们可以在想要的时候随时导入。导入快照使用docker import命令。

1
2
3
4
5
6
7
8
9
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bb776ce48575        3 days ago          109MB
[root@localhost ~]# cat caimengzhi_nginx.tar | docker import - caimengzhi_nginx 
sha256:a2855f206c05c76a2ef1bc369b24dded80df110bf4fef27e2f5107633be1226b
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
caimengzhi_nginx    latest              a2855f206c05        1 second ago        108MB
nginx               latest              bb776ce48575        3 days ago          109MB

保存

 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
[root@localhost ~]# 
[root@localhost ~]# docker save nginx>nginx.tar
[root@localhost ~]# du -sh nginx.tar 
108M    nginx.tar
You have new mail in /var/spool/mail/root
[root@localhost ~]# docker image rm -f nginx
Untagged: nginx:latest
Untagged: nginx@sha256:c6bcc3f6f4dfee535dc0cbdaa7f32901727dd93f92c8a45eacd5c6a6d080a9ad

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
6cdfdebc0d56        bb776ce48575        "nginx -g 'daemon of…"   3 hours ago         Up 3 hours          0.0.0.0:88->80/tcp   youthful_saha
[root@localhost ~]# docker container rm -f 6cdfdebc0d56
6cdfdebc0d56
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              bb776ce48575        3 days ago          109MB
centos              7                   9f38484d220f        4 weeks ago         202MB
[root@localhost ~]# docker image rm -f bb776ce48575
Deleted: sha256:bb776ce48575796501bcc53e511563116132b789ab0552d520513da8c738cba2
Deleted: sha256:43e4bf6ebb72bc17f5f35af0ace5e5d5db31b2b631fac2cca3d4be2420ca9758
Deleted: sha256:2ceeec5cb749c4154ae2390bc3e0c2a4dee8663ca7012ca37a293734de83d498
Deleted: sha256:5dacd731af1b0386ead06c8b1feff9f65d9e0bdfec032d2cd0bc03690698feda
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   9f38484d220f        4 weeks ago         202MB

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   9f38484d220f        4 weeks ago         202MB
[root@localhost ~]# docker load < nginx.tar 
5dacd731af1b: Loading layer [==================================================>]  58.45MB/58.45MB
b0a13438d0d3: Loading layer [==================================================>]  54.55MB/54.55MB
19d384dcffcc: Loading layer [==================================================>]  3.584kB/3.584kB
Loaded image: nginx:latest
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bb776ce48575        3 days ago          109MB
centos              7                   9f38484d220f        4 weeks ago         202MB

docker-compose 命令

Docker-compose命令格式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]


-f --file FILE指定Compose模板文件,默认为docker-compose.yml

-p --project-name NAME 指定项目名称,默认使用当前所在目录为项目名

--verbose  输出更多调试信息

-v,-version 打印版本并退出

--log-level LEVEL 定义日志等级(DEBUG, INFO, WARNING, ERROR, CRITICAL)

常用命令

 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
docker-compose up -d nginx                     构建建启动nignx容器

docker-compose exec nginx bash            登录到nginx容器中

docker-compose down                              删除所有nginx容器,镜像

docker-compose ps                                   显示所有容器

docker-compose restart nginx                   重新启动nginx容器

docker-compose run --no-deps --rm php-fpm php -v  在php-fpm中不启动关联容器,并容器执行php -v 执行完成后删除容器

docker-compose build nginx                     构建镜像 。        

docker-compose build --no-cache nginx   不带缓存的构建。

docker-compose logs  nginx                     查看nginx的日志 

docker-compose logs -f nginx                   查看nginx的实时日志

docker-compose config  -q                        验证(docker-compose.yml)文件配置,当配置正确时,不输出任何内容,当文件配置错误,输出错误信息。 

docker-compose events --json nginx       以json的形式输出nginx的docker日志

docker-compose pause nginx                 暂停nignx容器

docker-compose unpause nginx             恢复ningx容器

docker-compose rm nginx                       删除容器(删除前必须关闭容器)

docker-compose stop nginx                    停止nignx容器

docker-compose start nginx                    启动nignx容器
最后更新于 2025年01月05日
使用 Hugo 构建
主题 Stack 由 Jimmy 设计