Docker 常用容器命令
一、新建并启动容器
1 | docker run [OPTIONS] IMAGE [COMMAND] [ARG...] |
由于启动命令太多了,我们就选取几个常用的进行演示
[OPTIONS] 常用值
--name="容器新名字": 为容器指定一个别名
-d: 后台运行容器,并返回容器ID,也即启动守护式容器
-i:以交互模式运行容器,通常与 -t 同时使用
-t:为容器重新分配一个伪输入终端,通常与 -i 同时使用
-P(大写 P): 随机端口映射
-p: 指定端口映射,有以下四种格式
ip:hostPort:containerPort
ip::containerPort
hostPort:containerPort (推荐)
containerPort
1、启动 centos 镜像
下面两条命令是等价的
1 2 | docker run -it --name=mycentos centos:centos7. 9.2009 docker run -it --name=mycentos centos:centos7. 9.2009 /bin/bash |
2、交互模式 / 后台模式启动 tomcat
1 2 3 4 5 | // 交互模式启动 tomcat docker run -it --name tomcat01 -p 8088 : 8080 tomcat: 8.5 . 61 -jdk8-corretto // 后台模式启动 tomcat docker run -d --name=tomcat02 -p 8089 : 8080 tomcat: 8.5 . 61 -jdk8-corretto |
启动信息如下:
二、列出正在运行的容器
1 | docker ps [OPTIONS] |
-a :列出当前所有正在运行的容器 + 历史上运行过的
-l :显示最近创建的容器
-n :显示最近n个创建的容器
-q :静默模式,只显示容器编号
-s :显示容器大小
--no-trunc : 不截断输出
三、进入正在启动的容器
1 | docker exec -it 容器 ID /bin/bash |
四、退出容器客户端
1 2 3 4 | // 方式一、退出容器客户端,并且关闭容器 exit // 方式二、退出容器客户端,不关闭容器 CTRL + P + Q |
五、启动容器
当然也可以使用下列命令启动全部的容器
1 | docker start $(docker ps -aq) |
六、关闭容器
关闭容器和启动容器相似,可以关闭一个,也可以关闭多个,也可以批量关闭
当然也可以使用下列命令强制关闭容器
1 | docker kill 容器ID或者容器别名 |
七、删除已停止容器
八、其它重要命令
1、查看容器日志
1 | docker logs -f -t --tail 显示行数 容器ID |
-t: 加入时间戳
-f: 跟随最新的日志打印
-n 500: 显示 500 行
2、查看容器内的进程
1 | docker top 容器 ID |
3、查看容器内的细节
1 | docker inspect 容器 ID |
以 json 串的形式显示容器的信息
4、容器和主机之间文件交互
从容器中拷贝文件到主机
1 | docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH |
5、从宿主机拷贝文件到容器中
1 | docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH |
九、总结
常用命令总结
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 | // 当前 shell 下 attach 连接指定运行镜像 attach Attach to a running container // 通过 Dockerfile 定制镜像 build Build an image from a Dockerfile // 提交当前容器为新的镜像 commit Create a new image from a container changes // 从容器中拷贝指定文件或者目录到宿主机中 cp Copy files/folders from the containers filesystem to the host path // 创建一个新的容器,同 run,但不启动容器 create Create a new container // 查看 docker 容器变化 diff Inspect changes on a container's filesystem // 从 docker 服务获取容器实时事件 events Get real time events from the server // 在已存在的容器上运行命令 exec Run a command in an existing container // 导出容器的内容流作为一个 tar 归档文件[对应 import ] export Stream the contents of a container as a tar archive // 展示一个镜像形成历史 history Show the history of an image // 列出系统当前镜像 images List images // 从tar包中的内容创建一个新的文件系统映像[对应export] import Create a new filesystem image from the contents of a tarball // 显示系统相关信息 info Display system-wide information // 查看容器详细信息 inspect Return low-level information on a container // kill 指定 docker 容器 kill Kill a running container // 从一个 tar 包中加载一个镜像[对应 save] load Load an image from a tar archive // 注册或者登录一个 docker 源服务器 login Register or Login to the docker registry server // 从当前 Docker registry 退出 logout Log out from a Docker registry server // 输出当前容器日志信息 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 // 从 docker 镜像源服务器拉取指定镜像或者库镜像 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 // 重启运行的容器 restart Restart a running container // 移除一个或者多个容器 rm Remove one or more containers // 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除] rmi Remove one or more images // 创建一个新的容器并运行一个命令 run Run a command in a new container // 保存一个镜像为一个 tar 包[对应 load] save Save an image to a tar archive // 在 docker hub 中搜索镜像 search Search for an image on the 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 // 查看 docker 版本号 version Show the docker version information // 截取容器停止时的退出状态值 wait Block until a container stops, then print its exit code |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?