Docker常用命令(自己亲自总结)

本文介绍了Docker的基础命令,包括查看Docker版本和系统信息、管理镜像(列出、搜索、下载和删除)、操作容器(新建、运行、列出、退出、删除、启动/停止/重启/进入容器)以及其他常用命令。内容详实,适合初学者掌握Docker基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、帮助命令

docker version # 显示 Docker 版本信息。
docker info # 显示 Docker 系统信息,包括镜像和容器数。。
docker --help # 帮助

二、镜像命令

1、docker images

# 列出本地主机上的镜像
[root@kuangshen ~]# docker images
REPOSITORY TAG IMAGE ID CREATED
SIZE
hello-world latest bf756fb1ae65 4 months ago
13.3kB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像创建时间
SIZE 镜像大小
# 同一个仓库源可以有多个 TAG,代表这个仓库源的不同版本,我们使用REPOSITORY:TAG 定义不同
的镜像,如果你不定义镜像的标签版本,docker将默认使用 lastest 镜像!
# 可选项
-a: 列出本地所有镜像
-q: 只显示镜像id
--digests: 显示镜像的摘要信息

2、docker search(注意搜索镜像时可以先去官网查看,官网信息非常详细)

http://hub.docker.com 

# 搜索镜像
[root@kuangshen ~]# docker search mysql
NAME DESCRIPTION STARS
OFFICIAL
mysql MySQL is a widely used, open-source relation… 9484
[OK]
# docker search 某个镜像的名称 对应DockerHub仓库中的镜像
# 可选项
--filter=stars=50 : 列出收藏数不小于指定值的镜像。

3、docker pull

# 下载镜像
[root@kuangshen ~]# docker pull mysql
Using default tag: latest # 不写tag,默认是latest
latest: Pulling from library/mysql
54fec2fa59d0: Already exists # 分层下载
bcc6c6145912: Already exists
951c3d959c9d: Already exists
05de4d0e206e: Already exists
319f0394ef42: Already exists
d9185034607b: Already exists
013a9c64dadc: Already exists
42f3f7d10903: Pull complete
c4a3851d9207: Pull complete
1 2 3 4 5 6 7 8 9
10
11
12
13docker rmi
容器命令
说明:有镜像才能创建容器,我们这里使用 centos 的镜像来测试,就是虚拟一个 centos !
新建容器并启动
82a1cc65c182: Pull complete
a0a6b01efa55: Pull complete
bca5ce71f9ea: Pull complete
Digest:
sha256:61a2a33f4b8b4bc93b7b6b9e65e64044aaec594809f818aeffbff69a893d1944 #
签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实位置
# 指定版本下载
[root@kuangshen ~]# docker pull mysql:5.7
....

4、docker rmi

# 删除镜像
docker rmi -f 镜像id # 删除单个
docker rmi -f 镜像名:tag 镜像名:tag # 删除多个
docker rmi -f $(docker images -qa) # 删除全部
$(docker images -qa)这个可以列出系统的镜像

  

三、容器命令

1、新建容器并运行

docker run [可选参数] image

# 参数说明
  --name="name"    容器名称, 区分容器
  -d               后台方式运行
  -it              使用交付模式,进入容器查看内容
  -p               指定容器的端口
    -p ip:主机端口:容器端口
    -p 主机端口:容器端口
    -p 容器端口
  -P               随机指定端口
  -rm              用完就删除
  -v 主机路径:容器路径  挂载数据卷

#测试创建并进入容器
[root@localhost ~]# docker run --name="my-nginx" -it -p 8080:80 -v /data:/data

#从容器退出到主机
[root@localhost ~]# eixt
  

2、列出所有运行的容器

# docker ps 
        # 列出所有正在运行的容器
  -a    # 列出所有的容器,包括正在运行和曾经运行的容器
  -n=?  # 显示最近创建的容器
  -q    # 显示容器id
  
[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
2bdb808adcda        f00                 "nginx -g 'daemon of…"   47 hours ago        Exited (0) 47 hours ago                       my-nginx
[root@localhost ~]# docker ps -q
[root@localhost ~]# docker ps -aq
2bdb808adcda

3、退出容器

exit          # 退出容器并停止运行
Ctrl + P + Q  # 退出容器,不停止运行

4、删除容器

docker rm 容器id                # 删除指定容器, 如果删除正在运行的容器, 加 -f 强制删除
docker rm -f $(docker ps -aq)  # 删除所有容器

5、启动停止容器

docker strat 容器id    # 启动
docker stop 容器id     # 停止容器
docker restart 容器id  # 重启容器
docker kill 容器id     # 强制停止容器

四、常用其他命令

1、后台启动容器

# docker run -d 镜像名

[root@localhost ~]# docker run -d centos

# docker ps 后发现容器没有运行

# 常见的坑, docker启动容器后台运行, 就必须有一个前台前台进程 docker发现没有应用, 就会自动停止

# 列如centos 容器启动后, 发现自己没有提供服务, 就会立即停止, 就没有运行呢, 解决这个问题的话, 就-it 再Ctrl + P + Q 再退出

2、查看容器日志

# docker logs 可选参数 容器id
  -tf            # 显示日志
  --tail number  # 显示日志条数

3、查看容器内的进程

# docker top 容器id

4、查看容器内的源数据

# docker inspect 容器id

5、进入正在运行的容器

# 我们通常容器使用都是后台运行模式, 需要进入容器,修改一些配置

# 命令
docker exec 容器id    # 进入容器后, 打开一个新的终端
docker attach 容器id  # 进入容器正在运行的终端, 不会启动新进程

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
8f04eac497f9        2f7                 "nginx -g 'daemon of…"   6 minutes ago       Up 5 seconds        0.0.0.0:80->80/tcp   my-nginx
[root@localhost ~]# docker exec -it 8f sh
# read escape sequence
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
8f04eac497f9        2f7                 "nginx -g 'daemon of…"   7 minutes ago       Up About a minute   0.0.0.0:80->80/tcp   my-nginx

6、从容器内拷贝文件到主机

# docker cp 容器id:容器内部路径 主机路径

[root@localhost ~]# docker cp 2bdb808adcda:/data /data

7、commit提交自定义镜像

# docker commit -m="提交描述信息" -a="作者" 容器id 目标镜像名:[TAG版本]

 

 五、常用命令

attach Attach to a running container # 当前 shell 下
attach 连接指定运行镜像
build Build an image from a Dockerfile # 通过 Dockerfile 定
制镜像
commit Create a new image from a container changes # 提交当前容器为新的镜像
cp Copy files/folders from the containers filesystem to the host path
#从容器中拷贝指定文件或者目录到宿主机中
create Create a new container # 创建一个新的容器,同
run,但不启动容器
diff Inspect changes on a container's filesystem # 查看 docker 容器变化
events Get real time events from the server # 从 docker 服务获取容
器实时事件
exec Run a command in an existing container # 在已存在的容器上运行命
令 ex
port Stream the contents of a container as a tar archive # 导出容器的内
容流作为一个 tar 归档文件[对应 import ]
history Show the history of an image # 展示一个镜像形成历史
images List images # 列出系统当前镜像
1 2 3 4 5 6 7 8 9
10
11作业练习
使用Docker 安装 Nginx
import Create a new filesystem image from the contents of a tarball # 从
tar包中的内容创建一个新的文件系统映像[对应export]
info Display system-wide information # 显示系统相关信息
inspect Return low-level information on a container # 查看容器详细信息
kill Kill a running container # kill 指定 docker 容
器 lo
ad Load an image from a tar archive # 从一个 tar 包中加载一
个镜像[对应 save]
login Register or Login to the docker registry server # 注册或者登陆一个
docker 源服务器
logout Log out from a Docker registry server # 从当前 Docker
registry 退出
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 # 列出容器列表
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 #
推送指定镜像或者库镜像至docker源服务器
restart Restart a running container # 重启运行的容器
rm Remove one or more containers # 移除一个或者多个容器
rmi Remove one or more images # 移除一个或多个镜像[无容器使用该
镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
run Run a command in a new container # 创建一个新的容器并运行
一个命令
save Save an image to a tar archive # 保存一个镜像为一个
tar 包[对应 load]
search Search for an image on the Docker Hub # 在 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 # 查看容器中运行的进程信
息 un
pause Unpause a paused container # 取消暂停容器
version Show the docker version information # 查看 docker 版本号
wait Block until a container stops, then print its exit code # 截取容
器停止时的退出状态值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值