原创

Docker初学3:Docker的常用命令


Docker的常用命令

帮助命令

帮助命令可以在我们遇到不懂的命令时帮助我们去了解它们怎么使用。

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

镜像命令

docker images

docker images
# 列出本地主机上的镜像 
[root@iZh40ti53pk77iZ blog]# 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:显示镜像的摘要信息
# 搜索镜像 
[root@iZh40ti53pk77iZ blog]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11156     [OK]

# docker search 某个镜像的名称 对应DockerHub仓库中的镜像 

# 可选项 
--filter=stars=50 : 列出收藏数不小于指定值的镜像。

docker pull

[root@iZh40ti53pk77iZ blog]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
33847f680f63: Pull complete  #采用分层下载的策略
5cb67864e624: Pull complete 
1a2b594783f5: Pull complete 
b30e406dd925: Pull complete 
48901e306e4c: Pull complete 
603d2b7147fd: Pull complete 
802aa684c1c4: Pull complete 
715d3c143a06: Pull complete 
6978e1b7a511: Pull complete 
f0d78b0ac1be: Pull complete 
35a94d251ed1: Pull complete 
36f75719b1a9: Pull complete 
Digest: sha256:8b928a5117cf5c2238c7a09cd28c2e801ac98f91c3f8203a8938ae51f14700fd
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

# 签名
Status: Downloaded newer image for mysql:latest 
docker.io/library/mysql:latest # 真实位置 
# 指定版本下载 
[root@kuangshen ~]# docker pull mysql:5.7 ....

docker rmi

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

容器命令

说明:有镜像才能创建容器,我们这里下载一个CentOS镜像来进行测试

docker pull centos

新建容器并启动

# 命令 
docker run [OPTIONS] IMAGE [COMMAND][ARG...] 
# 常用参数说明 
--name="Name" # 给容器指定一个名字 
-d # 后台方式运行容器,并返回容器的id! 
-i # 以交互模式运行容器,通过和 -t 一起使用 
-t # 给容器重新分配一个终端,通常和 -i 一起使用 
-P # 随机端口映射(大写) 
-p # 指定端口映射(小结),一般可以有四种写法 
	ip:hostPort:containerPort (主机ip+主机端口号+容器端口号) 
	ip::containerPort         (主机ip+容器端口号)
	hostPort:containerPort    (主机端口号+容器端口号)(常用) 
	containerPort             (容器端口号)
# 测试 
[root@iZh40ti53pk77iZ blog]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
mysql        latest    c60d96bd2b77   17 hours ago   514MB
centos       latest    300e315adb2f   7 months ago   209MB
# 使用centos进行用交互模式( -it )启动容器,在容器内执行/bin/bash命令! 
[root@iZh40ti53pk77iZ blog]# docker run -it centos /bin/bash 
[root@7c4a18af243e /]# ls        # 注意地址,已经切换到容器内部了!
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr 
[root@7c4a18af243e /]# exit      # 使用 exit 退出容器
exit
[root@iZh40ti53pk77iZ blog]# 

列出所有运行的容器

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

退出容器

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

启动、停止容器

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

删除容器

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

常用其他命令

后台启动容器

# 命令 
docker run -d 容器名

# 例子
[root@iZh40ti53pk77iZ blog]# docker run -d centos # 启动centos,使用后台方式启动
371085cc988807036498eaa84109528537f353b75f675fb30714a63bac767969
[root@iZh40ti53pk77iZ blog]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

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

查看日志

# 命令 
docker logs -f -t --tail 容器id 
# 例子:我们启动 centos,并编写一段脚本来测试玩玩!最后查看日志

[root@iZh40ti53pk77iZ blog]# docker run -d centos /bin/bash -c "while true;do echo hello;sleep 1;done"

[root@iZh40ti53pk77iZ blog]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
5038eabb3d86   centos    "/bin/bash -c 'while…"   17 seconds ago   Up 16 seconds             nice_tu

# -t 显示时间戳 
# -f 打印最新的日志 
# --tail 数字 显示多少条!

[root@iZh40ti53pk77iZ blog]# docker logs -tf --tail 10 5038eabb3d86  # 输出容器中的10条日志
2021-07-23T02:52:09.030127690Z hello
2021-07-23T02:52:10.031872640Z hello
2021-07-23T02:52:11.033675362Z hello
2021-07-23T02:52:12.035598214Z hello
2021-07-23T02:52:13.037466342Z hello
2021-07-23T02:52:14.039246693Z hello
2021-07-23T02:52:15.041016744Z hello
2021-07-23T02:52:16.042876605Z hello
2021-07-23T02:52:17.044731568Z hello

查看容器中运行的进程信息

# 命令 
docker top 容器id 

# 测试 
[root@iZh40ti53pk77iZ blog]# docker top 04c7cf5f4794
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                4773                4752                0                   10:56               pts/0               00:00:00            /bin/bash

查看容器/镜像的元数据

# 命令 
docker inspect 容器id 

# 测试 
[root@iZh40ti53pk77iZ blog]# docker inspect 04c7cf5f4794
[
    {
        # 完整的id,有意思啊,这里上面的容器id,就是截取的这个id前几位!
        "Id": "04c7cf5f47946e9ae08cd527747550a54e266ce58a3b1c703d221c5e5d3df0c8",
        "Created": "2021-07-23T02:56:55.317061124Z",
        "Path": "/bin/bash",
        # 传递的参数
        "Args": [],
        # 状态
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 4773,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-07-23T02:56:55.608672352Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        # 使用的镜像
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        ...

进入正在运行的容器

# 命令1 
docker exec -it 容器id bashShell

# 测试1
[root@iZh40ti53pk77iZ blog]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
04c7cf5f4794   centos    "/bin/bash"   7 minutes ago   Up 7 minutes             confident_jackson
[root@iZh40ti53pk77iZ blog]# docker exec -it 04c7cf5f4794 /bin/bash
[root@04c7cf5f4794 /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr

# 命令2 
docker attach 容器id

# 测试2
[root@iZh40ti53pk77iZ blog]# docker attach 04c7cf5f4794
[root@04c7cf5f4794 /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@04c7cf5f4794 /]# 

# 区别 
# exec 是在容器中打开新的终端,并且可以启动新的进程
# 以exec的方式进入容器后使用exit,不会使容器停止,如下:
[root@iZh40ti53pk77iZ blog]# docker exec -it b2fc2f281760 /bin/bash
[root@b2fc2f281760 /]# exit
exit
[root@iZh40ti53pk77iZ blog]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
b2fc2f281760   centos    "/bin/bash"   58 seconds ago   Up 58 seconds             dreamy_ellis

# attach 进入一个已经运行的容器的终端,然后进行命令执行的动作,不会启动新的进程
# 以attach的方式进入容器后使用exit会导致整个容器停止,如下图:
[root@iZh40ti53pk77iZ blog]# docker attach b2fc2f281760
[root@b2fc2f281760 /]# exit
exit
[root@iZh40ti53pk77iZ blog]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

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

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

# 测试 
# 容器内执行,创建一个文件测试
[root@iZh40ti53pk77iZ blog]# docker attach 990e2dee4f48
[root@990e2dee4f48 /]# cd /home
[root@990e2dee4f48 home]# ls
# 在容器内创建一个hello.java文件
[root@990e2dee4f48 home]# touch hello.java
[root@990e2dee4f48 home]# exit
exit
[root@iZh40ti53pk77iZ blog]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                        PORTS     NAMES
990e2dee4f48   centos         "/bin/bash"              3 minutes ago    Exited (0) 12 seconds ago practical_proskuriakova
# 开始复制
[root@iZh40ti53pk77iZ blog]# docker cp 990e2dee4f48:/home/hello.java /www/wwwroot/blog
# 查看是否复制成功
[root@iZh40ti53pk77iZ blog]# ls
demo2-0.0.1-SNAPSHOT.jar  hello.java  log  nohup.out

小结

在这里插入图片描述

常用命令总结

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 # 在已存在的容器上运行命令
export  Stream the contents of a container as a tar archive # 导出容器的内容流作为一个 tar 归档文件[对应 import ] 
history  Show the history of an image # 展示一个镜像形成历史 
images  List images # 列出系统当前镜像
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 容器
load  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 # 查看容器中运行的进程信息
unpause  Unpause a paused container # 取消暂停容器 
version  Show the docker version information # 查看 docker 版本号 
wait  Block until a container stops, then print its exit code # 截取容器停止时的退出状态值
Docker
End
  • 作者:小关同学(联系作者)
  • 发表时间:2021-07-23 11:48
  • 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)
  • 公众号转载:请在文末添加作者博客链接
  • 问题交流(QQ群)