Featured image of post Docker常用操作命令

Docker常用操作命令

一、镜像操作

1.1 获取镜像

docker pull 镜像名:标签。镜像名如nginx、tomcat、centos7、busybox、ubuntu等。一般标签是版本信息,如果没带一般是下载最新版

1
2
3
4
5
6
7
# 获取ubuntu系统18.04版本的镜像
[node1 ~]$ docker pull ubuntu:18.04
18.04: Pulling from library/ubuntu
0c5227665c11: Pull complete 
Digest: sha256:8aa9c2798215f99544d1ce7439ea9c3a6dfd82de607da1cec3a8a2fae005931b
Status: Downloaded newer image for ubuntu:18.04
docker.io/library/ubuntu:18.04

1.2 查看镜像信息

  • -a: 列出所有镜像文件,包括临时文件。
1
2
3
4
5
6
7
# 查看服务器上已经下载的镜像
[node1 ~]$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
tomcat       latest    786217b848fd   2 days ago    482MB
nginx        latest    6efc10a0510f   2 weeks ago   142MB
busybox      latest    7cfbbec8963d   6 weeks ago   4.86MB
ubuntu       18.04     3941d3b032a8   7 weeks ago   63.1MB

1.3 搜索镜像

可以搜索Registry上的镜像:

docker search 关键词。搜索出来的列从左到有分别是:镜像名称、描述、评分、是否时官方镜像。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
[node1 ~]$ docker search nginx
NAME                                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                                             Official build of Nginx.                        18437     [OK]       
linuxserver/nginx                                 An Nginx container, brought to you by LinuxS…   193                  
bitnami/nginx                                     Bitnami nginx Docker Image                      159                  [OK]
ubuntu/nginx                                      Nginx, a high-performance reverse proxy & we…   86                   
privatebin/nginx-fpm-alpine                       PrivateBin running on an Nginx, php-fpm & Al…   72                   [OK]
bitnami/nginx-ingress-controller                  Bitnami Docker Image for NGINX Ingress Contr…   25                   [OK]
rancher/nginx-ingress-controller                                                                  11                   
kasmweb/nginx                                     An Nginx image based off nginx:alpine and in…   6                    
bitnami/nginx-ldap-auth-daemon                                                                    3                    
bitnami/nginx-exporter                                                                            3                    
rapidfort/nginx                                   RapidFort optimized, hardened image for NGINX   3                    

1.4 删除镜像

docker rmi 镜像ID/镜像名称。如果没有指定镜像的标签,默认删除的是latest的镜像

  • -f,-force:强制删除镜像,即使有容器依赖了该镜像
  • -no-prune:不要清理没有带标签的父镜像
1
2
3
4
5
[node1 ~]$ docker rmi busybox       
Untagged: busybox:latest
Untagged: busybox@sha256:b5d6fe0712636ceb7430189de28819e195e8966372edfc2d9409d79402a0dc16
Deleted: sha256:7cfbbec8963d8f13e6c70416d6592e1cc10f47a348131290a55d43c3acab3fb9
Deleted: sha256:baacf561cfff825708763ce7ee4a18293716c533e6ece3bd39009a5fb3c804d2

清理镜像的遗留文件:docker image prune

  • -a:删除所有没用的镜像,不光是临时镜像
  • -filter filter:只清理符合过滤条件的镜像
  • -f:强制删除镜像,不用进行提示确认
1
2
3
4
[node1 ~]$ docker image prune        
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

1.5 导入和导出镜像

1、例如将ubuntu:18.04的镜像导出成ubuntu:18.04.tar

1
[node1 ~]$ docker save -o ubuntu:18.04.tar ubuntu:18.04

2、载入镜像,将docker save导出的镜像载入到本地镜像库中

1
[node1 ~]$ docker load -i ubuntu:18.04.tar

1.6 上传镜像

我们可以把本地镜像推送到Registry,默认是会推送到Docker Hub,可以对Registry进行设置。

1
[node1 ~]$ docker push top.maishuren/nginx:latest
Licensed under CC BY-NC-SA 4.0