zoukankan      html  css  js  c++  java
  • 在Docker中执行web应用

    启动一个简单的web 应用

    使用社区提供的模板,启动一个简单的web应用,熟悉下各种Docker命令的使用:

    # docker run -d -P training/webapp python app.py
    Unable to find image 'training/webapp' locally
    Pulling repository training/webapp
    31fa814ba25a: Download complete 
    511136ea3c5a: Download complete 
    f10ebce2c0e1: Download complete 
    82cdea7ab5b5: Download complete 
    5dbd9cb5a02f: Download complete 
    74fe38d11401: Download complete 
    64523f641a05: Download complete 
    0e2afc9aad6e: Download complete 
    e8fc7643ceb1: Download complete 
    733b0e3dbcee: Download complete 
    a1feb043c441: Download complete 
    e12923494f6a: Download complete 
    a15f98c46748: Download complete 
    Status: Downloaded newer image for training/webapp:latest
    d00f94a31e8767271f68ab72eab15a8e805c416b0636877f22a31572d10b718d

    -d 启动一个daemon并在后台执行

    -P 映射一个网络port

    training/webapp docker社区提供的。预先创建好的模板。里面包括一个简单的Python Flask web应用

    # docker ps -l
    CONTAINER ID        IMAGE                    COMMAND             CREATED             STATUS              PORTS                     NAMES
    d00f94a31e87        training/webapp:latest   "python app.py"     59 seconds ago      Up 56 seconds       0.0.0.0:49153->5000/tcp   hopeful_lalande

    -l 查看具体信息

    PORTS显示 0.0.0.0:49153->5000/tcp

    意思是将container中的5000port。映射到host的49153port。5000是Python Flask 的默认port。

    也能够使用 -p container-port:host-port 命令来指定映射端口号

    # docker run -d -p 5000:5000 training/webapp python app.py

    QQ图片20141217103052

    查看web应用信息

    查看名为hopeful_lalande的docker container中,5000port的映射信息

    # docker port hopeful_lalande 5000
    0.0.0.0:49153

    查看web应用日志

    # docker logs -f hopeful_lalande
     * Running on http://0.0.0.0:5000/
    192.168.254.1 - - [17/Dec/2014 02:30:32] "GET / HTTP/1.1" 200 -
    192.168.254.1 - - [17/Dec/2014 02:30:32] "GET /favicon.ico HTTP/1.1" 404 -
    192.168.254.1 - - [17/Dec/2014 02:30:32] "GET /favicon.ico HTTP/1.1" 404 -

    -f 持续查看container日志的标准化输出,类似tail -f

    查看进程具体信息

    # docker top hopeful_lalande
    UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
    root                15189               2447                0                   10:24               ?                   00:00:00            python app.py

    查看web应用容器具体信息

    # docker inspect hopeful_lalande
    [{
        "AppArmorProfile": "",
        "Args": [
            "app.py"
        ],
        "Config": {
            "AttachStderr": false,
            "AttachStdin": false,
            "AttachStdout": false,
            "Cmd": [
                "python",
                "app.py"
            ],
            "CpuShares": 0,
            "Cpuset": "",
            "Domainname": "",
            "Entrypoint": null,
            "Env": [
                "HOME=/",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "ExposedPorts": {
                "5000/tcp": {}
            },
            "Hostname": "d00f94a31e87",
            "Image": "training/webapp",
            "Memory": 0,
            "MemorySwap": 0,
            "NetworkDisabled": false,
            "OnBuild": null,
            "OpenStdin": false,
            "PortSpecs": null,
            "StdinOnce": false,
            "Tty": false,
            "User": "",
            "Volumes": null,
            "WorkingDir": "/opt/webapp"
        },
        "Created": "2014-12-17T02:24:09.468631143Z",
        "Driver": "devicemapper",
        "ExecDriver": "native-0.2",
        "HostConfig": {
            "Binds": null,
            "CapAdd": null,
            "CapDrop": null,
            "ContainerIDFile": "",
            "Devices": [],
            "Dns": null,
            "DnsSearch": null,
            "ExtraHosts": null,
            "Links": null,
            "LxcConf": [],
            "NetworkMode": "bridge",
            "PortBindings": {},
            "Privileged": false,
            "PublishAllPorts": true,
            "RestartPolicy": {
                "MaximumRetryCount": 0,
                "Name": ""
            },
            "SecurityOpt": null,
            "VolumesFrom": null
        },
        "HostnamePath": "/var/lib/docker/containers/d00f94a31e8767271f68ab72eab15a8e805c416b0636877f22a31572d10b718d/hostname",
        "HostsPath": "/var/lib/docker/containers/d00f94a31e8767271f68ab72eab15a8e805c416b0636877f22a31572d10b718d/hosts",
        "Id": "d00f94a31e8767271f68ab72eab15a8e805c416b0636877f22a31572d10b718d",
        "Image": "31fa814ba25ae3426f8710df7a48d567d4022527ef2c14964bb8bc45e653417c",
        "MountLabel": "",
        "Name": "/hopeful_lalande",
        "NetworkSettings": {
            "Bridge": "docker0",
            "Gateway": "172.17.42.1",
            "IPAddress": "172.17.0.8",
            "IPPrefixLen": 16,
            "MacAddress": "02:42:ac:11:00:08",
            "PortMapping": null,
            "Ports": {
                "5000/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "49153"
                    }
                ]
            }
        },
        "Path": "python",
        "ProcessLabel": "",
        "ResolvConfPath": "/var/lib/docker/containers/d00f94a31e8767271f68ab72eab15a8e805c416b0636877f22a31572d10b718d/resolv.conf",
        "State": {
            "ExitCode": 0,
            "FinishedAt": "0001-01-01T00:00:00Z",
            "Paused": false,
            "Pid": 15189,
            "Restarting": false,
            "Running": true,
            "StartedAt": "2014-12-17T02:24:11.279426855Z"
        },
        "Volumes": {},
        "VolumesRW": {}
    }
    ]

    输出一个JSON格式的Docker container配置和状态。

    也能够通过指定名称,获取某项信息值。例如以下:

    # docker inspect -f '{{ .NetworkSettings.IPAddress }}' hopeful_lalande
    172.17.0.8

    停止Web应用

    # docker stop hopeful_lalande
    hopeful_lalande

    停止后,能够使用docker ps -a命令,列出之前全部docker容器

    # docker ps -a
    CONTAINER ID        IMAGE                    COMMAND                CREATED             STATUS                         PORTS               NAMES
    d00f94a31e87        training/webapp:latest   "python app.py"        21 minutes ago      Exited (-1) 43 seconds ago                         hopeful_lalande     
    7f22b335fb2c        fedora:latest            "/bin/sh -c 'while t   49 minutes ago      Exited (-1) 40 minutes ago                         silly_archimedes    
    。

    。。

    重新启动Web应用

    # docker start hopeful_lalande
    hopeful_lalande

    删除Web应用

    # docker rm hopeful_lalande
    Error response from daemon: You cannot remove a running container. Stop the container before attempting removal or use -f
    2014/12/17 10:49:32 Error: failed to remove one or more containers

    提示无法删除一个正在执行的container。

    停止该container后,又一次运行上条命令。成功删除container

  • 相关阅读:
    立体视觉
    KCF中的循环矩阵
    针孔相机成像模型
    lombok-@Accessors注解
    @Value注入map、List,yaml格式
    Error:java: Compilation failed: internal java compiler error 解决办法
    Linux守护进程(init.d和xinetd)
    no server suitable for synchronization found 很简单,可以试试
    yyds什么意思?00后“行话”已经霸占网络平台了
    &>/dev/null表示的意思
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6735422.html
Copyright © 2011-2022 走看看