【Docker】常用命令 - Go语言中文社区

【Docker】常用命令


帮助命令

官方文档命令查询

打印当前docker的版本信息

docker version                 #docker版本信息
docker info                    #docker系统信息,包括镜像和容器数量
docker --help            #万能命令

镜像命令

查询镜像

查看所有本地主机上的镜像

docker images

可选项:

  • 列出所有镜像:-a, —all
  • 只显示镜像的id:-q, —quiet

搜索镜像

查找仓库源里面的镜像

docker search

例如我要搜索mysql:
在这里插入图片描述
可选项:

  • 过滤:—filter=
    docker search mysql --filter=stars=3000 # 收藏数大于3000
    

下载镜像

下载/拉取镜像:

docker pull [OPTIONS] NAME[:TAG]

默认版本下载mysql:

[root@vvvv ~]# docker pull mysql
Using default tag: latest #如果不写版本,默认下载最新的
latest: Pulling from library/mysql
a076a628af6f: Pull complete  #分层下载,docker images的核心 联合文件系统
f6c208f3f991: Pull complete 
88a9455a9165: Pull complete 
406c9b8427c6: Pull complete 
7c88599c0b25: Pull complete 
25b5c6debdaf: Pull complete 
43a5816f1617: Pull complete 
1a8c919e89bf: Pull complete 
9f3cf4bd1a07: Pull complete 
80539cea118d: Pull complete 
201b3cad54ce: Pull complete 
944ba37e1c06: Pull complete 
Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址

下载mysql的指定版本:(具体有哪些版本的镜像我们可以去docker hub中查看)

[root@vvvv ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Already exists # 联合文件,因为latest里面有这些所以无需下载
f6c208f3f991: Already exists 
88a9455a9165: Already exists 
406c9b8427c6: Already exists 
7c88599c0b25: Already exists 
25b5c6debdaf: Already exists 
43a5816f1617: Already exists 
1831ac1245f4: Pull complete # 只需要下载5.7特有的即可
37677b8c1f79: Pull complete 
27e4ac3b0f6e: Pull complete 
7227baa8c445: Pull complete 
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

删除镜像

删除指定镜像:(可以通过镜像id或镜像名称来删除某个镜像)

docker rmi -f 镜像id/镜像名称   

删除多个镜像:

docker rmi -f 镜像id 镜像id 镜像id

删除所有镜像:

docker rmi -f $(docker images -aq)

容器命令

说明:有了镜像才可以创建容器

新建容器并启动

docker run  [可选参数] images

参数说明:

--name="Name"                    #容器名字
-d                                #后台运行
-it                                #使用交互方式运行,进入容器查看内容
-p                                #指定容器的端口 -p 8080:8080
    -p ip:主机端口:容器端口
    -p 主机端口:容器端口(常用)
    -p 容器端口
    容器端口
-P                                #随机指定端口
-e                                #配置环境遍历
-v                                #挂载卷
  • 交互方式启动
    我们使用交互的方式启动并进入centos
    # 启动并进入容器
    docker run -it centos /bin/bash
    # 从容器中退回主机
    exit
    
    在这里插入图片描述
  • 后台方式启动
    docker容器使用后台运行,就必须要有一个前台进程
    通过后台方式启动一个容器,这个容器要自己本身有前台应用,否则就会停止
    docker run -d centos  #  通过这种方式启动centos,当我们docker ps的时候发现centos停止了。因为它本身没有前台应用
    docker run -d tomcat  #   tomcat本身有前台进程,所以用这种方式不会停止
    

查看正在运行的容器

docker ps [可选参数]
#参数说明
-a                            #列出正在运行+历史运行过的容器
-n=?                          #显示最近创建的?条容器
-q                            #只显示容器的编号

在这里插入图片描述

退出容器

exit                #直接停止容器并退出
Ctrl+p+q            #容器不停止退出

删除容器

用容器id 删除指定容器
注意不能删除正在运行的容器,如果要强制删除就docker rm -f

docker rm 容器id 

删除所有所有的容器

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

启动和停止容器的操作

docker start 容器id      # 启动容器
docker restart 容器id    # 重启容器
docker stop 容器id       # 停止当前正在运行的容器
docker kill 容器id 		# 强制停止当前容器

常用的其他命令

查看容器日志

docker logs
docker logs -tf --tail 10 容器id # 看最近的10条日志

参数说明:

  • -t 是加入时间戳
  • -f 跟随最新的日志打印
  • –tail number 显示日志条数

例子:
为了能显示日志,我们以后台的方式启动centos 并2秒中打印zxf一次
其中/bin/sh -c 可以让 bash 将一个字串作为完整的命令来执行

docker run -d centos /bin/sh -c “while true;do echo hello zxf;sleep 2;done” # 自己编写一段shell脚本
docker ps # 查看id
docker logs -tf --tail 10 容器id # 看最近的10条日志

在这里插入图片描述

查看容器中进程信息

docker top 容器id

在这里插入图片描述

查看容器元数据

docker inspet 容器id

进入当前正在运行的容器

由于我们通常都是用后台方式运行容器的,有时我们需要进入容器修改一些配置

# 方式1
docker exec -it 容器id /bin/bash        #交互模式进入容器后,开启新的终端,可以在里面操作
# 方式2
docker attach 容器id                    #进入容器正在执行的终端,也就是当前打开的终端

方式一exec:开启新的终端,可以在里面操作
在这里插入图片描述
方式二attach:进入容器正在执行的终端
在这里插入图片描述

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

docker cp 容器id:容器内文件路径 目的主机路径

例子:
我们以在centos容器中写了个test.java,然后退出容器 通过以下命令把容器内的test.java 移到了服务器的/home下

docker cp bf99251e7f7f:/home/test.java /home

在这里插入图片描述

命令总结

在这里插入图片描述

attach      Attach local standard input, output, and error streams to a running container
  #当前shell下 attach连接指定运行的镜像
  build       Build an image from a Dockerfile # 通过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 #查看docker容器的变化
  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 #导出容器文件系统作为一个tar归档文件[对应import]
  history     Show the history of an image # 展示一个镜像形成历史
  images      List images #列出系统当前的镜像
  import      Import the contents from a tarball to create a filesystem image #从tar包中导入内容创建一个文件系统镜像
  info        Display system-wide information # 显示全系统信息
  inspect     Return low-level information on Docker objects #查看容器详细信息
  kill        Kill one or more running containers # kill指定docker容器
  load        Load an image from a tar archive or STDIN #从一个tar包或标准输入中加载一个镜像[对应save]
  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
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/zxfhahaha/article/details/115186714
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢