博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
docker学习3:Docker容器使用
阅读量:2453 次
发布时间:2019-05-10

本文共 8560 字,大约阅读时间需要 28 分钟。

Docker 容器使用
Docker 客户端
docker 客户端非常简单 ,我们可以直接输入 docker 命令来查看到 Docker 客户端的所有命令选项。
[root@huixuan ~]# docker
Usage:  docker COMMAND
A self-sufficient runtime for containers
Options:
      --config string      Location of client config files (default "/root/.docker")
  -D, --debug              Enable debug mode
      --help               Print usage
  -H, --host list          Daemon socket(s) to connect to (default [])
  -l, --log-level string   Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit
Management Commands:
  container   Manage containers
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  volume      Manage volumes
Commands:
  attach      Attach to a running container
  build       Build an image from a 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 on a container's filesystem
  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
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  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
Run 'docker COMMAND --help' for more information on a command.
[root@huixuan ~]# 
可以通过命令 docker command --help 更深入的了解指定的 Docker 命令使用方法。
例如我们要查看 docker stats 指令的具体使用方法:
[root@huixuan ~]# docker stats --help
Usage:  docker stats [OPTIONS] [CONTAINER...]
Display a live stream of container(s) resource usage statistics
Options:
  -a, --all             Show all containers (default shows just running)
      --format string   Pretty-print images using a Go template
      --help            Print usage
      --no-stream       Disable streaming stats and only pull the first result
[root@huixuan ~]# 
运行一个web应用
前面我们运行的容器并没有一些什么特别的用处。
接下来让我们尝试使用 docker 构建一个 web 应用程序。
我们将在docker容器中运行一个 Python Flask 应用来运行一个web应用。
[root@huixuan ~]# docker pull training/webapp  # 载入镜像
Using default tag: latest
Trying to pull repository docker.io/training/webapp ... 
latest: Pulling from docker.io/training/webapp
e190868d63f8: Pull complete 
909cd34c6fd7: Pull complete 
0b9bfabab7c1: Pull complete 
a3ed95caeb02: Pull complete 
10bbbc0fc0ff: Pull complete 
fca59b508e9f: Pull complete 
e7ae2541b15b: Pull complete 
9dd97ef58ce9: Pull complete 
a4c1b0cb7af7: Pull complete 
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for docker.io/training/webapp:latest
[root@huixuan ~]# docker run -d -P training/webapp python app.py
e4fbb06d2dda11414252575dd77b2561b23e09c0237fac764076ba61fffa3461
[root@huixuan ~]# 
参数说明:
-d:让容器在后台运行。
-P:将容器内部使用的网络端口映射到我们使用的主机上。
查看 WEB 应用容器
使用 docker ps 来查看我们正在运行的容器
[root@huixuan ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
e4fbb06d2dda        training/webapp     "python app.py"     37 seconds ago      Up 35 seconds       0.0.0.0:1024->5000/tcp   kickass_wright
[root@huixuan ~]# 
这里多了端口信息。
PORTS  
0.0.0.0:1024->5000/tcp 
Docker 开放了 5000 端口(默认 Python Flask 端口)映射到主机端口 32769 上。
这时我们可以通过浏览器访问WEB应用
我们也可以指定 -p 标识来绑定指定端口。
[root@huixuan ~]# docker run -d -p 5000:5000 training/webapp python app.py
0e044f323370c157539fa9e235a37c7b2907e5920e846429562f1f18f3f6ff55
[root@huixuan ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
0e044f323370        training/webapp     "python app.py"     4 seconds ago       Up 3 seconds        0.0.0.0:5000->5000/tcp   admiring_goldwasser
e4fbb06d2dda        training/webapp     "python app.py"     13 minutes ago      Up 13 minutes       0.0.0.0:1024->5000/tcp   kickass_wright
[root@huixuan ~]# 
容器内部的 5000 端口映射到我们本地主机的 5000 端口上。
网络端口的快捷方式
通过docker ps 命令可以查看到容器的端口映射,docker还提供了另一个快捷方式:docker port,使用 docker port 可以查看指定 (ID或者名字)容器的某个确定端口映射到宿主机的端口号。
上面我们创建的web应用容器ID为:e4fbb06d2dda kickass_wright
我可以使用docker port e4fbb06d2dda 或docker port kickass_wright来查看容器端口的映射情况
[root@huixuan ~]# docker port e4fbb06d2dda
5000/tcp -> 0.0.0.0:1024
[root@huixuan ~]# docker port kickass_wright
5000/tcp -> 0.0.0.0:1024
[root@huixuan ~]#
查看WEB应用程序日志
docker logs [ID或者名字] 可以查看容器内部的标准输出。
[root@huixuan ~]# docker logs -f e4fbb06d2dda
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
172.17.8.178 - - [01/May/2018 01:35:36] "GET / HTTP/1.1" 200 -
172.17.8.178 - - [01/May/2018 01:35:36] "GET /favicon.ico HTTP/1.1" 404 -
-f:让 dokcer logs 像使用 tail -f 一样来输出容器内部的标准输出。
从上面,我们可以看到应用程序使用的是 5000 端口并且能够查看到应用程序的访问日志。
查看WEB应用程序容器的进程
我们还可以使用 docker top 来查看容器内部运行的进程
[root@huixuan ~]# docker top e4fbb06d2dda
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                3034                3017                0                   09:32               ?                   00:00:00            python app.py
[root@huixuan ~]# 
检查WEB应用程序
使用 docker inspect 来查看Docker的底层信息。它会返回一个 JSON 文件记录着 Docker 容器的配置和状态信息。
[root@huixuan ~]# docker inspect e4fbb06d2dda
[
    {
        "Id": "e4fbb06d2dda11414252575dd77b2561b23e09c0237fac764076ba61fffa3461",
        "Created": "2018-05-01T01:32:33.151210372Z",
        "Path": "python",
        "Args": [
            "app.py"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 3034,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-05-01T01:32:35.091034063Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        ....
        
停止WEB应用容器
[root@huixuan ~]# docker stop e4fbb06d2dda
e4fbb06d2dda
[root@huixuan ~]# 
重启WEB应用容器
已经停止的容器,我们可以使用命令 docker start 来启动。
[root@huixuan ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
0e044f323370        training/webapp     "python app.py"     11 minutes ago      Up 11 minutes       0.0.0.0:5000->5000/tcp   admiring_goldwasser
[root@huixuan ~]# docker start e4fbb06d2dda
e4fbb06d2dda
[root@huixuan ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
0e044f323370        training/webapp     "python app.py"     11 minutes ago      Up 11 minutes       0.0.0.0:5000->5000/tcp   admiring_goldwasser
e4fbb06d2dda        training/webapp     "python app.py"     24 minutes ago      Up 2 seconds        0.0.0.0:1025->5000/tcp   kickass_wright
[root@huixuan ~]# 
docker ps -l 查询最后一次创建的容器:
[root@huixuan ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
0e044f323370        training/webapp     "python app.py"     11 minutes ago      Up 11 minutes       0.0.0.0:5000->5000/tcp   admiring_goldwasser
[root@huixuan ~]# 
正在运行的容器,我们可以使用 docker restart 命令来重启
移除WEB应用容器
我们可以使用 docker rm 命令来删除不需要的容器
删除容器时,容器必须是停止状态,否则会报如下错误
[root@huixuan ~]# docker rm e4fbb06d2dda
Error response from daemon: You cannot remove a running container e4fbb06d2dda11414252575dd77b2561b23e09c0237fac764076ba61fffa3461. Stop the container before attempting removal or use -f
[root@huixuan ~]# 
[root@huixuan ~]# docker stop e4fbb06d2dda
e4fbb06d2dda
[root@huixuan ~]# docker rm e4fbb06d2dda
e4fbb06d2dda
[root@huixuan ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
0e044f323370        training/webapp     "python app.py"     13 minutes ago      Up 13 minutes       0.0.0.0:5000->5000/tcp   admiring_goldwasser
[root@huixuan ~]# 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/312079/viewspace-2153586/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/312079/viewspace-2153586/

你可能感兴趣的文章
数据预处理--噪声_为什么数据对您的业务很重要-以及如何处理数据
查看>>
计算机视觉技术 图像分类_如何训练图像分类器并教您的计算机日语
查看>>
桌面应用程序 azure_如何开始使用Microsoft Azure-功能应用程序,HTTP触发器和事件队列...
查看>>
矩阵奇异值分解特征值分解_推荐系统中的奇异值分解与矩阵分解
查看>>
异步JavaScript的演变:从回调到承诺,再到异步/等待
查看>>
自己写一个微型数据库_“最国际化的微型机构:”两名伦敦训练营的毕业生如何建造了一个远程…...
查看>>
构建静态服务器_为静态网站构建无服务器联系表
查看>>
塞尔达传说顺序_编码《塞尔达传说》克隆图例
查看>>
spring vertx_如何在Spring设置Vertx
查看>>
在Unity中创建3D直升机游戏
查看>>
编码和编码格式一样吗?_学习如何像专业人士一样编码
查看>>
CSS-in-JS的权衡
查看>>
ssh框架实现数据库_自顶向下介绍SSH及其如何实现安全的数据共享
查看>>
测试驱动开发 测试前移_测试驱动开发简介
查看>>
css 网格布局_我从CSS网格布局中学到的东西
查看>>
快速了解Kubernetes微服务中的通信
查看>>
如何使您的Kotlin Android动画可访问
查看>>
github pages_使用GitHub Pages和Lighthouse增强您的开发人员产品组合
查看>>
JavaScript命名约定:注意事项
查看>>
html:漂亮的原生表格_HTML表格:关于它们的所有知识
查看>>