Docker安装及常用指令详解

1 docker安装

​ docker官网地址:https://www.docker.com/

​ centos官方安装指南:https://docs.docker.com/engine/install/centos/

1.1 安装前置条件

​ 在安装docker之前先进行下面两个前置条件的安装。

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
yum makecache fast

1.2 查看docker版本

​ 按照时间降序的方式查看docker的版本信息。

[root@localhost ~]# yum list docker-ce --showduplicates | sort -r

1.3 安装docker

​ 可以使用下面的指令安装最新的docker组件:

yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

​ 可以通过下面的指令指定版本安装docker组件:

yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io docker-compose-plugin

​ 本例子采用19.03.9-3.el7进行演示:

[root@localhost ~]# yum install -y docker-ce-19.03.9-3.el7 docker-ce-cli-19.03.9-3.el7

​ 启动docker:

[root@localhost ~]# systemctl start docker

​ 查看docker状态:

[root@localhost ~]# systemctl status docker

1.4 配置镜像加速器

​ 登录阿里云,搜素容器镜像服务:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MkoaSvAL-1655735592737)(D:\developsoftware\mayun\note\study-note\docker\images\image-20220620083635595.png)]

​ 按照说明安装即可:

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://自己的.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

1.5 设置docker开机启动

​ 通过下面的指令设置docker开机启动:

[root@localhost ~]# systemctl enable docker

1.6 查看docker信息

​ 可以通过下面三个指令查询docker的一些信息:

[root@localhost ~]# docker -v
[root@localhost ~]# docker version
[root@localhost ~]# docker info

2 docker常用命令

​ docker命令网址:https://docs.docker.com/engine/reference/commandline/run/

​ 从docker命令使用出发,梳理出如下命令结构图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ul0nH15c-1655735592741)(D:\developsoftware\mayun\note\study-note\docker\images\image-20220620155518252.png)]

2.1 镜像相关操作

2.1.1 pull命令

​ 命令pull,是下载镜像的命令。镜像从远程镜像仓库服务的仓库中下载。默认情况下,镜像会从 Docker Hub 的仓库中拉取。通过下载过程,可以看到,一个镜像一般是由多个层组成,类似 f7e2b71213 这样的串表示层的唯一 ID。

​ 当我们执行 docker pull tomcat:9.0.20-jre8命令时,实际上相当于 docker pull docker.io/tomcat:9.0.20-jre8,如果未自定义配置仓库,则默认在下载的时候,会在镜像前面加上DockerHub 的地址。Docker 通过前缀地址的不同,来保证不同仓库中,重名镜像的唯一性。实际上完整的 ID 包括了 256 个 bit, 64 个十六进制字符组成的。

​ 使用方式如下:

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

​ 常用参数:

​ 1,–all-tags,-a (true,false):是否获取仓库中所有镜像,默认为否;

​ 2, --disable-content-trust: 跳过镜像内容的校验,默认为 true;

[root@localhost ~]# docker pull tomcat:9.0.20-jre8
9.0.20-jre8: Pulling from library/tomcat
c5e155d5a1d1: Pull complete 
221d80d00ae9: Pull complete 
4250b3117dca: Pull complete 
d1370422ab93: Pull complete 
deb6b03222ca: Pull complete 
9cdea8d70cc3: Pull complete 
968505be14db: Pull complete 
04b5c270ac81: Pull complete 
301d76fcab1f: Pull complete 
f4d49608235a: Pull complete 
f4c6404fd6f8: Pull complete 
Digest: sha256:4ec7984f3e609ca49df96f2738d2de74bd311fa4122c558b7a615d6a0b07dc60
Status: Downloaded newer image for tomcat:9.0.20-jre8
docker.io/library/tomcat:9.0.20-jre8

2.1.2 images命令

​ 可用通过下面两个命令查询本机已有镜像列表:

[root@localhost ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat              9.0.20-jre8         e24825d32965        3 years ago         464MB
[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat              9.0.20-jre8         e24825d32965        3 years ago         464MB

REPOSITORY:表示镜像的仓库源;TAG:镜像的标签;IMAGE ID:镜像ID;CREATED:镜像创建时间;SIZE:镜像大小。

2.1.3 save命令

​ save命令用于将镜像文件打成tar包,然后可以通过load命令加载出镜像。

​ 单个镜像文件打包方式:

root@localhost ~]# docker save tomcat:9.0.20-jre8 -o tomcat9.0.tar
[root@localhost ~]# docker save tomcat:9.0.20-jre8 > tomcat9.1.tar
[root@localhost ~]# ll
总用量 935588
-rw-------. 1 root root 479015936 620 12:54 tomcat9.0.tar
-rw-r--r--. 1 root root 479015936 620 12:55 tomcat9.1.tar

​ 多个镜像文件打包方式:

[root@localhost ~]# docker save tomcat:9.0.20-jre8 nginx:latest > tomcat-nginx.tar

2.1.4 load命令

​ load命令用于将save打包的镜像文件加载出来。查看本机的images,并删除:

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              605c77e624dd        5 months ago        141MB
tomcat              9.0.20-jre8         e24825d32965        3 years ago         464MB
[root@localhost ~]# docker rmi nginx
[root@localhost ~]# docker rmi e24825d32965

​ 执行完上面的操作之后,再次执行查询本机镜像的指令,发现已不存在任何镜像信息:

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

​ 通过下面的方法加载文件到镜像:

[root@localhost ~]# docker load < tomcat-nginx.tar 
f94641f1fe1f: Loading layer [==================================================>]  105.5MB/105.5MB
ec62f19bb3aa: Loading layer [==================================================>]  24.08MB/24.08MB
2c719774c1e1: Loading layer [==================================================>]  8.005MB/8.005MB
1b958b53b256: Loading layer [==================================================>]  2.172MB/2.172MB
7d63f8777ebf: Loading layer [==================================================>]  3.584kB/3.584kB
fe60061c6c4e: Loading layer [==================================================>]  1.536kB/1.536kB
d38f3d5a39fb: Loading layer [==================================================>]    317MB/317MB
2b6c38ff3137: Loading layer [==================================================>]   2.56kB/2.56kB
f0e1731fd286: Loading layer [==================================================>]  1.894MB/1.894MB
2e33d763e75d: Loading layer [==================================================>]  20.25MB/20.25MB
aa6264ff0dcd: Loading layer [==================================================>]   2.56kB/2.56kB
Loaded image: tomcat:9.0.20-jre8
2edcec3590a4: Loading layer [==================================================>]  83.86MB/83.86MB
e379e8aedd4d: Loading layer [==================================================>]     62MB/62MB
b8d6e692a25e: Loading layer [==================================================>]  3.072kB/3.072kB
f1db227348d0: Loading layer [==================================================>]  4.096kB/4.096kB
32ce5f6a5106: Loading layer [==================================================>]  3.584kB/3.584kB
d874fd2bc83b: Loading layer [==================================================>]  7.168kB/7.168kB
Loaded image: nginx:latest
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              605c77e624dd        5 months ago        141MB
tomcat              9.0.20-jre8         e24825d32965        3 years ago         464MB

​ 当然也可以才用下面的方式load镜像文件:

[root@localhost ~]# docker load -i tomcat-nginx.tar

2.1.5 search命令

​ search命令用来根据指定的条件搜索镜像,使用方式如下:

docker search [OPTIONS] TERM

​ 选项参数有如下几个:

​ 1, --filter,-f 过滤条件

​ 2,–format 输出内容格式

​ 3,–limit 最大搜索结果条数

​ 4,–no-trunc 不截断输出内容

​ 下面命令查询是stars数量大于等于3的tomcat镜像列表

[root@localhost ~]# docker search --filter=stars=3 --no-trunc tomcat
NAME                          DESCRIPTION                                                                                            STARS               OFFICIAL            AUTOMATED
tomcat                        Apache Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies   3343                [OK]                
tomee                         Apache TomEE is an all-Apache Java EE certified stack where Apache Tomcat is top dog.                  97                  [OK]                
bitnami/tomcat                Bitnami Tomcat Docker Image                                                                            45                                      [OK]
kubeguide/tomcat-app          Tomcat image for Chapter 1                                                                             35                                      
arm32v7/tomcat                Apache Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies   11                                      
arm64v8/tomcat                Apache Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies   8                                       
rightctrl/tomcat              CentOS , Oracle Java, tomcat application ssl https                                                     7                                       [OK]
amd64/tomcat                  Apache Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies   4                                       
jelastic/tomcat               An image of the Tomcat Java application server maintained by Jelastic PaaS.                            4                                       
tomcat2111/pisignage-server   PiSignage Server                                                                                       3                                       [OK]

​ 通过下面指令查询是否官方:

[root@localhost ~]# docker search --filter is-official=true --filter stars=3 busybox
NAME                DESCRIPTION           STARS               OFFICIAL            AUTOMATED
busybox             Busybox base image.   2639                [OK]                

​ --format这个参数主要用来设置打印输出的格式化,它有下面几个参数可选:

​ 1,.Name 镜像名字

​ 2,.Description 镜像描述

​ 3, .StarCount star数量

​ 4, .IsOfficial 是否官方

​ 5,.IsAutomated 是否自动化

​ 例如我们可以采用下面的指令格式化输出:

[root@localhost ~]# docker search --format "table {{.Name}}\t{{.IsAutomated}}\t{{.IsOfficial}}" nginx
NAME                                              AUTOMATED           OFFICIAL
nginx                                                                 [OK]
linuxserver/nginx                                                     
bitnami/nginx                                     [OK]                
ubuntu/nginx                                                          
bitnami/nginx-ingress-controller                  [OK]                
rancher/nginx-ingress-controller                                      
clearlinux/nginx                                                      
ibmcom/nginx-ingress-controller                                       
bitnami/nginx-ldap-auth-daemon                                        
rancher/nginx-ingress-controller-defaultbackend                       
bitnami/nginx-exporter                                                
vmware/nginx                                                          
circleci/nginx                                                        
bitnami/nginx-intel                                                   
vmware/nginx-photon                                                   
rancher/nginx                                                         
rapidfort/nginx                                                       
wallarm/nginx-ingress-controller                                      
kasmweb/nginx                                                         
ibmcom/nginx-ingress-controller-ppc64le                               
rancher/nginx-conf                                                    
continuumio/nginx-ingress-ws                                          
rancher/nginx-ingress-controller-amd64                                
ibmcom/nginx-ppc64le                                                  
rancher/nginx-ssl                    

2.1.6 inspect命令

​ inspect指令用来获取镜像的详细信息,包括创建者,各层的数字摘要等,使用方式如下:

docker inspect [OPTIONS] NAME|ID [NAME|ID...]

​ 选项参数有如下几个:

​ 1,–format,-f 格式化输出

​ 2,–size,-s 大小

​ 3,–type 输出类型

[root@localhost ~]# docker inspect nginx
[root@localhost ~]# docker inspect -f {{".Size"}} nginx
141479488
[root@localhost ~]# docker inspect --format='{{.Size}}' nginx
141479488

2.1.7 history命令

​ 从前面的命令中,我们了解到,一个镜像是由多个层组成的,那么,我们要如何知道各个层的具体内容了?可以通过 docker history命令,可以列出各个层的创建信息,例如:查看 nginx的各层信息。

[root@localhost ~]# docker history nginx
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
605c77e624dd        5 months ago        /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           5 months ago        /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B                  
<missing>           5 months ago        /bin/sh -c #(nop)  EXPOSE 80                    0B                  
<missing>           5 months ago        /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B                  
<missing>           5 months ago        /bin/sh -c #(nop) COPY file:09a214a3e07c919a…   4.61kB              
<missing>           5 months ago        /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7…   1.04kB              
<missing>           5 months ago        /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0…   1.96kB              
<missing>           5 months ago        /bin/sh -c #(nop) COPY file:65504f71f5855ca0…   1.2kB               
<missing>           5 months ago        /bin/sh -c set -x     && addgroup --system -…   61.1MB              
<missing>           5 months ago        /bin/sh -c #(nop)  ENV PKG_RELEASE=1~bullseye   0B                  
<missing>           5 months ago        /bin/sh -c #(nop)  ENV NJS_VERSION=0.7.1        0B                  
<missing>           5 months ago        /bin/sh -c #(nop)  ENV NGINX_VERSION=1.21.5     0B                  
<missing>           6 months ago        /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
<missing>           6 months ago        /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           6 months ago        /bin/sh -c #(nop) ADD file:09675d11695f65c55…   80.4MB  

2.1.8 tag命令

​ 标记本地镜像,将其归入某一仓库。先简单熟悉一下tag命令,后边将会讲解。

[root@localhost ~]# docker tag nginx dream21th/nginx
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
dream21th/nginx     latest              605c77e624dd        5 months ago        141MB
nginx               latest              605c77e624dd        5 months ago        141MB
tomcat              9.0.20-jre8         e24825d32965        3 years ago         464MB

2.1.9 rmi命令

​ rmi指令是用来删除镜像的,执行方式如下:

docker rmi [OPTIONS] IMAGE [IMAGE...]

​ 常用参数如下:

​ 1,-f, -force : 强制删除镜像,即便有容器引用该镜像;

​ 2,-no-prune : 不要删除未带标签的父镜像;

root@localhost ~]# docker tag nginx dream21th/nginx
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
dream21th/nginx     latest              605c77e624dd        5 months ago        141MB
nginx               latest              605c77e624dd        5 months ago        141MB
tomcat              9.0.20-jre8         e24825d32965        3 years ago         464MB
[root@localhost ~]# docker rmi nginx:latest 
Untagged: nginx:latest
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
dream21th/nginx     latest              605c77e624dd        5 months ago        141MB
tomcat              9.0.20-jre8         e24825d32965        3 years ago         464MB
[root@localhost ~]# docker rmi nginx
Error: No such image: nginx
[root@localhost ~]# docker rmi dream21th/nginx:latest 
Untagged: dream21th/nginx:latest
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat              9.0.20-jre8         e24825d32965        3 years ago         464MB

2.1.10 清理镜像

​ 我们在使用 Docker 一段时间后,系统一般都会残存一些临时的、没有被使用的镜像文件,可以通过以下命令进行清理。执行完命令后,还是告诉我们释放了多少存储空间!

[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

​ 常用参数:

​ 1,-a, --all : 删除所有没有用的镜像,而不仅仅是临时文件;

​ 2,-f, --force :强制删除镜像文件,无需弹出提示确认;

2.2 容器相关操作

​ 容器是镜像的运行时实例。正如从虚拟机模板上启动 VM 一样,用户也同样可以从单个镜像上启动一个或多个容器。虚拟机和容器最大的区别是容器更快并且更轻量级——与虚拟机运行在完整的操作系统之上相比,容器会共享其所在主机的操作系统/内核。下图为使用单个 Docker镜像启动多个容器的示意图。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0zjw9QRx-1655735592743)(D:\developsoftware\mayun\note\study-note\docker\images\image-20220620210936034.png)]

​ Docker容器类似于一个轻量级的沙箱,Docker利用容器来运行和隔离应用。容器是镜像的一个运行实例。可以将其启动、开始、停止、删除,而这些容器都是彼此相互隔离的、互不可见的。可以把容器看做是一个简易版的Linux系统环境(包括root用户权限、进程空间、用户空间和网络空间等)以及运行在其中的应用程序打包而成的盒子。

​ 容器是基于镜像启动起来的,容 器中可以运行一个或多个进程。镜像是Docker生命周期 中的构建或打包阶段,而容器则是启动或执行阶段。镜像自身是只读的。容器从镜像启动的时候,会在镜像的最上层创建一个可写层。

2.2.1 新建并启动容器

​ 可以通过下面的方式启动一个容器:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

​ 下面指令是运行一个tomcat镜像:

[root@localhost ~]# docker run -it --rm -p 8080:8080 tomcat:9.0.20-jre8

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PmiMld1a-1655735592744)(D:\developsoftware\mayun\note\study-note\docker\images\image-20220620211710313.png)]

​ 常用参数列表:

​ docker run命令常用参数比较多,这里仅仅列出开发岗常用参数,想获得更多参数信息可以查看https://docs.docker.com/engine/reference/commandline/run/,里面有详细的参数介绍和使用说明:

​ 1,-d, --detach=false: 后台运行容器,并返回容器ID;

​ 2,-i, --interactive=false: 以交互模式运行容器,通常与 -t 同时使用;

​ 3,-P, --publish-all=false: 随机端口映射,容器内部端口随机映射到主机的端口,不推荐使用;

​ 4,-p, --publish=[]: 指定端口映射,格式为:主机(宿主)端口:容器端口,推荐各位小伙伴们使用

​ 5,-t, --tty=false: 为容器重新分配一个伪输入终端,通常与 -i 同时使用

​ 6,–name=“nginx-dream21th”: 为容器指定一个名称

​ 7,-h , --hostname=“dream21th”: 指定容器的hostname

​ 8,-e , --env=[]: 设置环境变量,容器中可以使用该环境变量

​ 9,–net=“bridge”: 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型

​ 10,–link=[]: 添加链接到另一个容器,

​ 11,-v, --volume : 绑定一个卷

​ 12,–privileged=false: 指定容器是否为特权容器,特权容器拥有所有的capabilities

​ 13,**–restart=no:**指定容器停止后的重启策略;no:容器退出时不重启 ;on-failure:容器故障退出(返回值非零)时重启 ; always:容器退出时总是重启

​ 14,–rm=false: 指定容器停止后自动删除容器,不能以docker run -d启动的容器

2.2.2 容器日志

​ docker logs :获取容器的日志,执行的方式:

docker logs [OPTIONS] CONTAINER

​ 执行下面指令:

[root@localhost ~]# docker run -itd -p 8080:8080 tomcat:9.0.20-jre8 
2f66b01d73efc7a12fe43da2253c1d05856c6185b29ec4ff2366c76e53668428
[root@localhost ~]# docker ps 
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS                    NAMES
2f66b01d73ef        tomcat:9.0.20-jre8   "catalina.sh run"   37 seconds ago      Up 36 seconds       0.0.0.0:8080->8080/tcp   gallant_newton
[root@localhost ~]# docker logs -f gallant_newton

​ 可以在控制台输入:

root@localhost ~]# docker logs --help

Usage:	docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
//查看到2022-06-20T10:32:14Z之前的日志	
docker logs -f --until 2022-06-20T10:32:14Z gallant_newton

2.2.3 删除容器

docker rm **:**删除一个或多个容器。执行的指令方式如下:

docker rm [OPTIONS] CONTAINER [CONTAINER...]

​ 选择项的一些参数如下:

​ 1,**f 😗*通过 SIGKILL 信号强制删除一个运行中的容器;

​ 2,**-l 😗*移除容器间的网络连接,而非容器本身;

​ 3,**-v 😗*删除与容器关联的卷。

[root@localhost ~]# docker ps 
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS                    NAMES
2f66b01d73ef        tomcat:9.0.20-jre8   "catalina.sh run"   23 minutes ago      Up 23 minutes       0.0.0.0:8080->8080/tcp   gallant_newton
[root@localhost ~]# docker stop gallant_newton
gallant_newton
[root@localhost ~]# docker rm gallant_newton
gallant_newton

2.2.4 列出容器

​ 列出容器使用指令:

docker ps [OPTIONS]

​ 查询容器的列表:

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS                    NAMES
df2ad52f9203        tomcat:9.0.20-jre8   "catalina.sh run"   58 seconds ago      Up 57 seconds       0.0.0.0:8080->8080/tcp   beautiful_kowalevski
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                COMMAND             CREATED              STATUS              PORTS                    NAMES
df2ad52f9203        tomcat:9.0.20-jre8   "catalina.sh run"   About a minute ago   Up About a minute   0.0.0.0:8080->8080/tcp   beautiful_kowalevski

​ 输出详情介绍:

​ 1,CONTAINER ID: 容器 ID;

​ 2,IMAGE: 使用的镜像;

​ 3,COMMAND: 启动容器时运行的命令;

​ 4,CREATED: 容器的创建时间;

​ 5,STATUS: 容器状态;

​ 状态有7种:created(已创建)restarting(重启中)running(运行中)removing(迁移中)paused(暂停)exited(停止)dead(死亡)

​ 6,PORTS: 容器的端口信息和使用的连接类型(tcp\udp)。

​ 7,NAMES: 自动分配的容器名称。

​ 常用参数说明:

​ 1,**-a 😗*显示所有的容器,包括未运行的;

​ 2,**-q 😗*只显示容器编号。

​ 实用技巧:

停止所有运行容器 
docker stop $(docker ps -qa) 
删除所有的容器 
docker rm $(docker ps -aq) 
docker rm $(docker stop $(docker ps -q)) 
删除所有的镜像 
docker rmi $(docker images -q)

2.2.5 创建容器

docker create **:**创建一个新的容器但不启动它。用法同 docker run命令。

docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
[root@localhost ~]# docker create -it --name tomcat9 -p 8080:8080 tomcat:9.0.20-jre8
f7b45b68607780aa635a3e5996861b30c2ad5ea98be7d4d80d8603ebf72e0900
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS               NAMES
f7b45b686077        tomcat:9.0.20-jre8   "catalina.sh run"   10 seconds ago      Created                                 tomcat9
[root@localhost ~]# 

2.2.6 启动、重启、终止容器

docker start :启动一个或多个已经被停止的容器

docker stop :停止一个运行中的容器

docker restart :重启容器

​ 语法:

docker start [OPTIONS] CONTAINER [CONTAINER...] 
docker stop [OPTIONS] CONTAINER [CONTAINER...] 
docker restart [OPTIONS] CONTAINER [CONTAINER...]

​ 操作命令:

[root@localhost ~]# docker start tomcat9
tomcat9
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS                    NAMES
f7b45b686077        tomcat:9.0.20-jre8   "catalina.sh run"   2 minutes ago       Up 5 seconds        0.0.0.0:8080->8080/tcp   tomcat9
[root@localhost ~]# docker stop tomcat9
tomcat9
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost ~]# docker restart tomcat9
tomcat9
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS                    NAMES
f7b45b686077        tomcat:9.0.20-jre8   "catalina.sh run"   3 minutes ago       Up 2 seconds        0.0.0.0:8080->8080/tcp   tomcat9

2.2.7 进入容器

docker exec **:**在运行的容器中执行命令。早期有attach命令,对于阻塞命令会等待,所以不方便。在Docker 1.3.0后提供了exec 可以在容器内直接执行任意命令。

​ 语法:

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

​ 执行命令:

有bash命令的linux系统:例如centos 
docker run -it --name tomcat9.1 -p 8080:8080 tomcat:9.0.20-jre8-slim 
docker exec -it tomcat9.1 /bin/bash 
没有bash命令的linux系统:例如alpine系统 
docker run -it --name tomcat9.2 -p 8081:8080 tomcat:9.0.20-jre8-alpine 
docker exec -it tomcat9.2 sh

​ 常用参数:

​ 1,**-i 😗*即使没有附加也保持STDIN 打开

​ 2,**-t 😗*分配一个伪终端

2.2.8 查看容器

docker inspect : 获取容器/镜像的元数据。

docker inspect [OPTIONS] NAME|ID [NAME|ID...]

​ 执行命令:

docker run -it --name tomcat9 -p 8081:8080 tomcat:9.0.20-jre8-alpine 
docker inspect tomcat9

​ 常用参数:

​ 1,**-f 😗*指定返回值的模板文件。

​ 2,**-s 😗*显示总的文件大小。

​ 3,**–type 😗*为指定类型返回JSON。

2.2.9 更新容器

docker update :可以动态地更新容器配置。可以更新一个或多个容器配置。多个容器名称或ID之间使用空格分隔。但update命令不是很成熟,有很多配置项不能动态更新。推荐大家还是rm容器后,再重新run一个新的镜像。

docker update [OPTIONS] CONTAINER [CONTAINER...]

​ 执行命令:

docker run -it --name tomcat9 -p 8081:8080 tomcat:9.0.20-jre8-alpine 
更新容器restart策略 
docker update --restart always tomcat9

2.3.0 杀掉容器

docker kill :杀掉一个运行中的容器。

docker kill [OPTIONS] CONTAINER [CONTAINER...]

​ 执行命令:

docker run -it --name tomcat9 -p 8081:8080 tomcat:9.0.20-jre8-alpine 
docker kill tomcat9 
docker ps docker 
ps -a docker start tomcat9

​ 常用参数:

​ **-s 😗*向容器发送一个信号

2.3.1 docker常用命令汇总

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rrjySHU8-1655735592747)(D:\developsoftware\mayun\note\study-note\docker\images\image-20220620222445035.png)]

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dream21st

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值