Skip to content

常用命令

帮助命令

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

镜像命令

docker images

shell
# 列出本地主机上的镜像 
[root@notecove ~] docker images 
REPOSITORY   TAG     IMAGE ID      CREATED SIZE 
hello-world latest bf756fb1ae65 4 months ago 13.3kB 
# 可选项 
-a: 列出本地所有镜像 
-q: 只显示镜像id 
--digests: 显示镜像的摘要信息

解释

  • REPOSITORY 镜像的仓库源
  • TAG 镜像的标签
  • IMAGE ID 镜像的ID
  • CREATED 镜像创建时间
  • SIZE 镜像大小

注意

同一个仓库源可以有多个 TAG,代表这个仓库源的不同版本,我们使用REPOSITORY:TAG 定义不同 的镜像,如果你不定义镜像的标签版本,docker将默认使用 lastest 镜像!

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

docker pull

shell
# 下载镜像 
[root@notecove ~] 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
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@notecove ~]# docker pull mysql:5.7

docker rmi

shell
docker rmi -f 镜像id # 删除单个
docker rmi -f 镜像名:tag 镜像名:tag # 删除多个
docker rmi -f $(docker images -qa) # 删除全部

容器命令

有镜像才能创建容器,我们这里使用 centos 的镜像来测试,就是虚拟一个 centos !

docker pull centos

新建容器并启动

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

常用参数说明

shell
--name="Name" # 给容器指定一个名字
-d # 后台方式运行容器,并返回容器的id!
-i # 以交互模式运行容器,通过和 -t 一起使用
-t # 给容器重新分配一个终端,通常和 -i 一起使用
-P # 随机端口映射(大写)
-p # 指定端口映射(小结),一般可以有四种写法
    ip:hostPort:containerPort
    ip::containerPort
    hostPort:containerPort (常用)
    containerPort

列出所有运行的容器

# 命令 
docker ps [OPTIONS] 
-q # 静默模式,只显示容器编号。

常用参数说明

shell
# 常用参数说明 
-a # 列出当前所有正在运行的容器 + 历史运行过的容器 
-l # 显示最近创建的容器 
-n=? # 显示最近n个创建的容器 
-q # 静默模式,只显示容器编号。

退出容器

shell
exit # 容器停止退出 
ctrl+P+Q # 容器不停止退出

启动停止容器

shell
docker start (容器id or 容器名) # 启动容器 
docker restart (容器id or 容器名) # 重启容器 
docker stop (容器id or 容器名) # 停止容器 
docker kill (容器id or 容器名) # 强制停止容器

删除容器

shell
docker rm 容器id # 删除指定容器 
docker rm -f $(docker ps -a -q) # 删除所有容器 
docker ps -a -q|xargs docker rm # 删除所有容器

常用其他命令

后台启动容器

shell
# 命令 
docker run -d 容器名 

# 例子 
docker run -d centos # 启动centos,使用后台方式启动 

# 问题: 使用docker ps 查看,发现容器已经退出了! 
# 解释:Docker容器后台运行,就必须有一个前台进程,容器运行的命令如果不是那些一直挂起的命 令,就会自动退出。 
# 比如,你运行了nginx服务,但是docker前台没有运行应用,这种情况下,容器启动后,会立即自 杀,因为他觉得没有程序了,所以最好的情况是,将你的应用使用前台进程的方式运行启动。

查看日志

shell
# 命令 
docker logs -f -t --tail 容器id 

# 例子:我们启动 centos,并编写一段脚本来测试玩玩!最后查看日志
[root@notecove ~]# docker run -d centos /bin/sh -c "while true;do echo notecove;sleep 1;done" 
[root@notecove ~]# docker ps 
CONTAINER ID      IMAGE 
c8530dbbe3b4      centos 

# -t 显示时间戳 
# -f 打印最新的日志 
# --tail 数字 显示多少条! 
[root@notecove ~]# docker logs -tf --tail 10 c8530dbbe3b4 
2020-05-11T08:46:40.656901941Z notecove 
2020-05-11T08:46:41.658765018Z notecove 
2020-05-11T08:46:42.661015375Z notecove 
2020-05-11T08:46:43.662865628Z notecove 
2020-05-11T08:46:44.664571547Z notecove 
2020-05-11T08:46:45.666718583Z notecove 
2020-05-11T08:46:46.668556725Z notecove 
2020-05-11T08:46:47.670424699Z notecove 
2020-05-11T08:46:48.672324512Z notecove 
2020-05-11T08:46:49.674092766Z notecove

查看容器中运行的进程信息,支持 ps 命令参数。

shell
# 命令 
docker top 容器id 

# 测试 
[root@notecove ~] docker top c8530dbbe3b4 
UID     PID    PPID C STIME TTY TIME    CMD 
root    27437 27421 0 16:43 ? 00:00:00 /bin/sh -c ....

查看容器/镜像的元数据

shell
# 命令 
docker inspect 容器id 

# 测试 
[root@notecove ~]# docker inspect c8530dbbe3b4 
[ 
    { # 完整的id,有意思啊,这里上面的容器id,就是截取的这个id前几位! 
    "Id": "c8530dbbe3b44a0c873f2566442df6543ed653c1319753e34b400efa05f77cf8", 
    "Created": "2020-05-11T08:43:45.096892382Z", 
    "Path": "/bin/sh", 
    "Args": [ 
        "-c", 
        "while true;do echo notecove;sleep 1;done" 
    ],
    # 状态 
    "State": { 
        "Status": "running", 
        "Running": true, 
        "Paused": false, 
        "Restarting": false, 
        "OOMKilled": false, 
        "Dead": false,
        "Pid": 27437, 
        "ExitCode": 0, 
        "Error": "", 
        "StartedAt": "2020-05-11T08:43:45.324474622Z", 
        "FinishedAt": "0001-01-01T00:00:00Z" 
        }, 
]

进入正在运行的容器

shell
docker exec -it 容器id bash
docker attach 容器id

区别

  • exec 是在容器中打开新的终端,并且可以启动新的进程
  • attach 直接进入容器启动命令的终端,不会启动新的进程

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

shell
# 命令 
docker cp 容器id:容器内路径 目的主机路径 

# 测试 
# 容器内执行,创建一个文件测试 
[root@c8530dbbe3b4 /]# cd /home 
[root@c8530dbbe3b4 home]# touch f1 
[root@c8530dbbe3b4 home]# ls
f1
[root@c8530dbbe3b4 home]# exit exit 

# linux复制查看,是否复制成功 
[root@notecove ~]# docker cp c8530dbbe3b4:/home/f1 /home 
[root@notecove ~]# cd /home 
[root@notecove home]# ls 
f1