手机版
你好,游客 登录 注册
背景:
阅读新闻

为小白准备的重要 Docker 命令说明

[日期:2018-01-13] 来源:Linux中国  作者:Linux [字体: ]

早先的教程中,我们学过了在 RHEL CentOS 7 上安装 Docker 并创建 docker 容器。 在本教程中,我们会学习管理 docker 容器的其他命令。

Docker 命令语法

  1. $ docker [option][command][arguments]

要列出 docker 支持的所有命令,运行

  1. $ docker

我们会看到如下结果,

  1. attach Attach to a running container
  2. build Build an image from a Dockerfile
  3. commit Create a new image from a container's changes
  4. cp Copy files/folders between a container and the local filesystem
  5. create Create a new container
  6. diff Inspect changes on a container's filesystem
  7. events Get real time events from the server
  8. execRun a command in a running container
  9. exportExport a container's filesystem as a tar archive
  10. history Show the history of an image
  11. images List images
  12. import Import the contents from a tarball to create a filesystem image
  13. info Display system-wide information
  14. inspect Return low-level information on a container or image
  15. kill Kill a running container
  16. load Load an image from a tar archive or STDIN
  17. login Log in to a Docker registry
  18. logout Log out from a Docker registry
  19. logs Fetch the logs of a container
  20. network Manage Docker networks
  21. pause Pause all processes within a container
  22. port List port mappings or a specific mapping for the CONTAINER
  23. ps List containers
  24. pull Pull an image or a repository from a registry
  25. push Push an image or a repository to a registry
  26. rename Rename a container
  27. restart Restart a container
  28. rm Remove one or more containers
  29. rmi Remove one or more images
  30. run Run a command in a new container
  31. save Save one or more images to a tar archive
  32. search Search the Docker Hub for images
  33. start Start one or more stopped containers
  34. stats Display a live stream of container(s) resource usage statistics
  35. stop Stop a running container
  36. tag Tag an image into a repository
  37. top Display the running processes of a container
  38. unpause Unpause all processes within a container
  39. update Update configuration of one or more containers
  40. version Show the Docker version information
  41. volume Manage Docker volumes
  42. wait Block until a container stops, then print its exit code

要进一步查看某个命令支持的选项,运行:

  1. $ docker docker-subcommand info

就会列出 docker 子命令所支持的选项了。

 

测试与 Docker Hub 的连接

默认,所有镜像都是从 Docker Hub 中拉取下来的。我们可以从 Docker Hub 上传或下载操作系统镜像。为了检查我们是否能够正常地通过 Docker Hub 上传/下载镜像,运行

  1. $ docker run hello-world

结果应该是:

  1. HellofromDocker.
  2. This message shows that your installation appears to be working correctly.

输出结果表示你可以访问 Docker Hub 而且也能从 Docker Hub 下载 docker 镜像。

 

搜索镜像

搜索容器的镜像,运行

  1. $ docker search Ubuntu

我们应该会得到可用的 Ubuntu 镜像的列表。记住,如果你想要的是官方的镜像,请检查 official 这一列上是否为 [OK]

 

下载镜像

一旦搜索并找到了我们想要的镜像,我们可以运行下面语句来下载它:

  1. $ docker pull Ubuntu

要查看所有已下载的镜像,运行:

  1. $ docker images

 

运行容器

使用已下载镜像来运行容器,使用下面命令:

  1. $ docker run -it Ubuntu

这里,使用 -it 会打开一个 shell 与容器交互。容器启动并运行后,我们就可以像普通机器那样来使用它了,我们可以在容器中执行任何命令。

 

显示所有的 docker 容器

要列出所有 docker 容器,运行:

  1. $ docker ps

会输出一个容器列表,每个容器都有一个容器 id 标识。

 

停止 docker 容器

要停止 docker 容器,运行:

  1. $ docker stop container-id

 

从容器中退出

要从容器中退出,执行:

  1. $ exit

 

保存容器状态

容器运行并更改后(比如安装了 apache 服务器),我们可以保存容器状态。这会在本地系统上保存新创建镜像。

运行下面语句来提交并保存容器状态:

  1. $ docker commit 85475ef774 repository/image_name

这里,commit 命令会保存容器状态,85475ef774,是容器的容器 id,repository,通常为 docker hub 上的用户名 (或者新加的仓库名称)image_name,是新镜像的名称。

我们还可以使用 -m-a 来添加更多信息。通过 -m,我们可以留个信息说 apache 服务器已经安装好了,而 -a 可以添加作者名称。

像这样:

  1. docker commit -m "apache server installed"-a "Dan Daniels"85475ef774 daniels_dan/Cent_container

我们的教程至此就结束了,本教程讲解了一下 Docker 中的那些重要的命令,如有疑问,欢迎留言。

更多Docker相关教程见以下内容: 

Docker安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm 

Ubuntu 16.04 服务器上配置使用 Docker  http://www.linuxidc.com/Linux/2017-06/145176.htm 

Ubuntu 15.04下安装Docker  http://www.linuxidc.com/Linux/2015-07/120444.htm 

Docker 安装实例 http://www.linuxidc.com/Linux/2017-04/142666.htm 

Docker 创建基础镜像  http://www.linuxidc.com/Linux/2017-05/144112.htm 

在 Ubuntu 15.04 上如何安装Docker及基本用法 http://www.linuxidc.com/Linux/2015-09/122885.htm 

Ubuntu 16.04上Docker使用手记 http://www.linuxidc.com/Linux/2016-12/138490.htm 

使用Docker分分钟启动常用应用  http://www.linuxidc.com/Linux/2017-04/142649.htm 

Ubuntu 16.04下Docker修改配置文件不生效解决办法  http://www.linuxidc.com/Linux/2017-05/143862.htm 

Docker 的详细介绍请点这里
Docker 的下载地址请点这里


via: http://linuxtechlab.com/important-docker-commands-beginners/

作者:Shusain 译者:lujun9972 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

本文永久更新链接地址http://www.linuxidc.com/Linux/2018-01/150292.htm

linux
相关资讯       Docker命令 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款