美文网首页
docker构建微服务 启动web容器

docker构建微服务 启动web容器

作者: maxbin | 来源:发表于2018-03-07 14:58 被阅读66次

    一、查看docker客户端命令

    docker.png
    1. 通过docker COMMAND --help 更深入的了解指定的docker命令的用法
      例如:
    [root@localhost ~]# docker stats --help
    
    Usage: docker stats [OPTIONS] CONTAINER [CONTAINER...]
    
    Display a live stream of one or more containers' resource usage statistics
    
      --help=false         Print usage
      --no-stream=false    Disable streaming stats and only pull the first result
    

    二、运行一个web应用

    在docker容器中运行一个Python Flask应用

    1. 载入镜像
    [root@localhost ~]# docker pull training/webapp
    latest: Pulling from training/webapp
    e9e06b06e14c: Pull complete 
    a82efea989f9: Pull complete 
    37bea4ee0c81: Pull complete 
    07f8e8c5e660: Pull complete 
    23f0158a1fbe: Pull complete 
    0a4852b23749: Pull complete 
    7d0ff9745632: Pull complete 
    99b0d955e85d: Pull complete 
    33e109f2ff13: Pull complete 
    cc06fd877d54: Pull complete 
    b1ae241d644a: Pull complete 
    b37deb56df95: Pull complete 
    02a8815912ca: Already exists 
    Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
    Status: Downloaded newer image for training/webapp:latest
    
    1. 启动应用程序,并查看正在运行的容器
    [root@localhost ~]# docker run -d -P training/webapp python app.py
    2b939c4ffe3a2f6508c2fca9f92e2ef78300028f2643673535d377a8d6c6c851
    [root@localhost ~]# docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
    2b939c4ffe3a        training/webapp     "python app.py"     6 seconds ago       Up 5 seconds        0.0.0.0:32769->5000/tcp   happy_poitras  
    

    多了PORTS列,显示端口映射:0.0.0.0:32769->5000/tcp,此时访问IP+端口(32769)可以看到web程序已经正常运行起来了


    web.png
    1. 查看web容器日志:docker logs -f ID/NAME
    [root@localhost ~]# docker logs -f 2b939c4ffe3a
     * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
    192.168.72.173 - - [07/Mar/2018 06:37:23] "GET / HTTP/1.1" 200 -
    192.168.72.173 - - [07/Mar/2018 06:37:24] "GET /favicon.ico HTTP/1.1" 404 -
    192.168.72.173 - - [07/Mar/2018 06:37:36] "GET / HTTP/1.1" 200 -
    192.168.72.173 - - [07/Mar/2018 06:46:24] "GET / HTTP/1.1" 200 -
    

    三、停止web容器

    命令:docker start ID/NAME

    docker stop 2b939c4ffe3a
    

    四、移除web容器

    命令:docker rm ID/NAME

    docker rm 2b939c4ffe3a
    

    移除容器时必须先停止容器,否则会报错

    [root@localhost ~]# docker rm 2b939c4ffe3a
    Error response from daemon: Cannot destroy container 2b939c4ffe3a: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f
    Error: failed to remove containers: [2b939c4ffe3a]
    

    移除容器后,使用docker info查看,可以发现Containers 数量减少

    [root@localhost ~]# docker info
    Containers: 8  #数量减少了一个
    Images: 16
    

    相关文章

      网友评论

          本文标题:docker构建微服务 启动web容器

          本文链接:https://www.haomeiwen.com/subject/kovjfftx.html