【编程向导】Docker-常用命令

常用命令

管理命令

管理命令 说明
builder 管理构建
config 管理配置
container 管理容器
context 管理上下文
engine 管理引擎
image 管理镜像
network 管理网络
node 管理 Swarm 节点
plugin 管理插件
secret 管理 Docker secrets
service 管理服务
stack 管理 Docker stacks
swarm 管理 Swarm 集群
system 查看系统信息
trust 管理 Docker trust
volume 管理卷

普通命令

容器生命周期管理

命令 说明
run 创建一个新的容器并运行一个命令
start 启动容器
stop 停止容器
restart 重启容器
kill kill 运行中的容器
rm 删除容器
pause 暂停一个或多个容器中的所有进程
unpause 恢复容器中所有的进程
create 创建一个新容器
exec 在正在运行的容器中运行命令

容器操作

命令 说明
ps 查看容器列表
inspect 返回 Docker 对象的低级信息
top 展示一个容器中运行的进程
attach 进入一个运行的容器
events 从服务器获取实时事件
logs 获取一个容器的日志
wait 阻塞直到容器停止,然后打印退出代码
export 将容器的文件系统导出为 tar 存档
port 查看端口映射或容器的特定映射列表
stats 实时显示容器资源使用情况的统计信息
update 更新容器配置

容器 rootfs 命令

命令 说明
commit 从容器变更记录中创建一个镜像
cp 在容器和宿主机文件系统之间拷贝文件
diff 检查对容器文件系统上的文件或目录的更改

镜像仓库

命令 说明
login 登陆 Docker 镜像仓库
logout 登出 Docker 镜像仓库
pull 从镜像仓库拉取镜像
push 将本地的镜像上传到镜像仓库,要先登陆到镜像仓库
search 从 Docker Hub 搜索镜像

本地镜像管理

命令 说明
images 查看镜像列表
rmi 删除镜像
tag 标记本地镜像,将其归入某一仓库
build 根据 DockerFile 构建镜像
history 显示镜像的构建历史记录
save 将指定镜像保存成 tar 归档文件
load 从存档或者 STDIN 加载镜像
import 从归档文件中创建镜像
rename 重命名容器

相关信息

命令 说明
wait
info 显示系统范围的信息
version 显示 Docker 的版本信息

宿主机操作

端口和磁盘目录的映射

bash 复制代码
# 将宿主机的 81 端口映射到容器的 80 端口
# 将宿主机的 /develop/data 卷 映射到容器的 /data 卷
$ docker run -i -t -p 81:80 -v /develop/data:/data centos /bin/bash
  • -p:映射端口号
  • -v:磁盘目录映射

Docker 更改端口号映射:

运行中的容器无法映射新的端口号,也无法更改端口号映射,但可以通过两种方法解决。

  1. iptable 转发端口
bash 复制代码
# 查看容器 IP
$ docker insepct 36afde543eb5 | grep IPAddress
> "IPAddress": "172.17.0.2"

# 将主机的 8081 端口映射到宿主机的 8080 端口
$ iptables -t nat -A DOCKER -p tcp --dport 8081 -j DNAT --to-destination 172.17.0.2:8080
  1. 先提交容器为镜像,再运行这个容器,同时指定新的端口映射
bash 复制代码
# 提交容器为镜像
$ docker commit 9995ffa15f46  mycentos:0.1

# 停止旧的容器
$ docker stop 9995ffa15f46

# 重新从旧的镜像启动容器
$ docker run -i -t  -p 8081:8080  mycentos:0.1

宿主机关于 Docker 的操作

bash 复制代码
# 开启 docker
service docker start

# 重启
service docker restart

# 暂停
service docker stop

Docker Hub

创建和使用私有仓库

bash 复制代码
#
docker run -eSEARCH_BACKEND=sqlalchemy-eSQLALCHEMY_INDEX_DATABASE=sqlite:tmp/docker-registry.db-d -name registry -p 5000:5000 registry

原版指令指引

按首字母大小写排列

  • 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
相关推荐
这可就有点麻烦了9 分钟前
强化学习笔记之【TD3算法】
linux·笔记·算法·机器学习
百锦再21 分钟前
自动驾驶的技术实现及原理
人工智能·机器学习·自动驾驶
HyperAI超神经2 小时前
Meta 首个多模态大模型一键启动!首个多针刺绣数据集上线,含超 30k 张图片
大数据·人工智能·深度学习·机器学习·语言模型·大模型·数据集
漫无目的行走的月亮2 小时前
在Docker中运行微服务注册中心Eureka
docker
sp_fyf_20242 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-03
人工智能·算法·机器学习·计算机视觉·语言模型·自然语言处理
学步_技术2 小时前
自动驾驶系列—线控悬架技术:自动驾驶背后的动力学掌控者
人工智能·机器学习·自动驾驶·线控系统·悬挂系统
牛哥带你学代码3 小时前
交叠型双重差分法
人工智能·深度学习·机器学习
学步_技术3 小时前
自动驾驶系列—线控系统:驱动自动驾驶的核心技术解读与应用指南
人工智能·机器学习·自动驾驶·线控系统·转向系统
quaer4 小时前
Open-Sora全面开源?
开发语言·算法·机器学习·matlab·矩阵
墨@#≯4 小时前
机器学习系列篇章0 --- 人工智能&机器学习相关概念梳理
人工智能·经验分享·机器学习