【docker笔记】docker常用命令

1、帮助启动类命令

1.1 启动、重启、查询当前状态、停止

xml 复制代码
systemctl start docker
systemctl stop docker
systemctl restart docker
systemctl status docker

1.2 设置开机启动

xml 复制代码
systemctl enable docker

1.3 查看docker概要信息

xml 复制代码
docker info

1.4 查看docker帮助文档

xml 复制代码
docker --help
docker 具体命令 --help

2、镜像命令

2.1 查看本地镜像(表头:REPOSITORY--仓库源 TAG--镜像标签 IMAGE ID--镜像id CREATED--镜像创建时间 SIZE--镜像大小)

同一个仓库源可以多个TAG版本,代表这个仓库源的不同的版本,使用REPOSITORY:TAG来定义不同的镜像,如果不指定一个镜像的版本标签,docker将默认使用最新的latest镜像

xml 复制代码
docker images
options:
	-a:列出本地所有镜像(含历史镜像)
	-q:只显示镜像id

2.2 搜索某个镜像是否在远程仓库(表头:NAME--名字 DESCRIPTION--描述 STARS--点赞数量 OFFICIAL--是否是官方 AUTOMATED--是否能够自动构建)

xml 复制代码
docker search [options] imagesname
options:
	docker search --limit 5 redis  只列出前5个

2.3 下载镜像

xml 复制代码
docker pull imagesname[:TAG]
such:  docker pull redis:6.0.8
such:  docker pull imagesname  默认拉最新版

2.4 查看镜像/容器/数据卷所占空间

xml 复制代码
docker system df

2.5 移除镜像

xml 复制代码
docker rmi imagesId
docker rmi -f imagesId 删除单个
docker rmi -f imagesname1:TAG imagesname2:TAG 删除多个
docker rmi -f $(docker images -qa) 删除全部 

3、容器命令

有镜像才能创建容器,这是根本前提

3.1 新建+启动容器

xml 复制代码
docker run [options] image [command][arg..]   启动交互式容器(前台命令行)

options说明

xml 复制代码
--name="容器新名字"  为容器指定一个名字
-d:后台运行容器并返回容器ID,也即启动守护式容器(后台运行)

-i:以交互模式运行容器,通常与-t同时使用
-t:为容器重新分配一个伪终端,通常与-i同时使用
docker run -it image:镜像启动之后有进一步的命令请求,需要你返回一个终端让我进一步进行操作

-P:随机端口映射
-p:指定端口映射

3.2 列出当前所有正在运行的容器

xml 复制代码
docker ps [options] 

options说明

xml 复制代码
-a:列出当前所有容器(正在运行+历史运行)
-l:显示最近创建的容器
-n:显示最近n个创建的容器
-q:静默模式,只显示容器编号

3.3 退出容器

shell 复制代码
exit   			run进去容器,exit退出,容器停止
ctrl+p+q  		run进去容器,ctrl+p+q退出,容器不停止

3.4 启动已经停止运行的容器

shell 复制代码
docker start 容器ID或者容器名

3.5 重启容器

shell 复制代码
docker restart 容器ID或者容器名

3.6 停止容器

shell 复制代码
docker stop 容器ID或者容器名

3.7 强制停止容器

shell 复制代码
docker kill 容器ID或者容器名

3.8 删除已经停止的容器

shell 复制代码
docker rm 容器ID或者容器名
docker rm -f 容器ID或者容器名          //强制删除(无需停止)

3.9 启动守护式模式容器(后台服务器)

大部分场景下,我们希望docker的服务是在后台运行的,我们可以通过-d指定容器的后台运行模式

shell 复制代码
docker run -d 容器名

注意事项:

使用docker run -d centos后,容器启动成功后会退出

docker机制:docker容器后台运行,就必须有一个前台进程,容器运行的命令如果不是一直挂起的命令(top...),就会自动退出

3.10 查看容器日志

shell 复制代码
docker logs [OPTIONS] 容器id/容器名称
-details  :显示提供给日志的其他详细信息
-f --follow :查看实施日志
--tail :仅列出最新N条容器日志
-t,--timestamps :显示时间戳加粗样式
docker logs --tail 50 --follow --timestamps nginx1.23.2

3.11 查看容器内运行的进程

shell 复制代码
docker top

3.12 查看容器内部细节

shell 复制代码
docker inspect 容器id

3.13 进入正在运行的容器并以命令行交互

shell 复制代码
docker exec -it 容器ID bashShell
docker attach 容器ID

区别:

attach直接进入容器启动命令的终端,不会启动新的进程,用exit退出会导致容器停止

exec是在容器中打开新的终端,并且可以启动新的进程,用exit退出不会导致容器停止(推荐)

一般用-d后台启动程序,再用exec进入对应容器实例

3.14 从容器内拷贝文件到主机上

shell 复制代码
docker cp 容器ID:容器内路径 目的主机路径

3.15 导入和导出容器

export导出容器的内容留作为一个tar归档文件【对应import命令】

shell 复制代码
docker export 容器ID > 文件名.tar 

import 从tar包中的内容创建一个新的文件系统再导入为镜像【对应export】

shell 复制代码
cat 文件名.tar|docker import -镜像用户/镜像名:镜像版本号

3.16 导入和导出镜像

修改镜像标签

shell 复制代码
docker commit 容器id 镜像名称:tag

导出镜像的内容留作为一个tar归档文件(tar文件会保存在执行当前命令的目录下)

shell 复制代码
docker save -o 压缩文件名称 镜像名称:tag

从tar包中的内容创建一个新的文件系统再导入为镜像

shell 复制代码
docker load -i 压缩文件名称
相关推荐
cui_hao_nan9 小时前
Docker后端部署
运维·docker·容器
大苏打seven10 小时前
Docker学习笔记:Docker网络
笔记·学习·docker
小张是铁粉10 小时前
docker在Linux的安装遇到的问题
linux·docker·容器
没有名字的小羊12 小时前
8.Docker镜像讲解
运维·docker·容器·tomcat
企鹅侠客15 小时前
实践篇:14-构建 Node.js 应用程序镜像
docker·node.js·dockerfile
做一个AC梦15 小时前
Docker安装失败:Docker Desktop installation failed
运维·docker·容器
Shan120515 小时前
浅谈Docker Kicks in的应用
运维·docker·容器
Li&&Tao15 小时前
docker 常用命令
docker·容器·eureka
Jiude17 小时前
MinIO 社区版被故意阉割,Web管理功能全面移除。我来试试国产RustFS
后端·docker·架构
飞询17 小时前
Docker 安装 Elasticsearch 9
elasticsearch·docker