0
点赞
收藏
分享

微信扫一扫

Docker 运行Web程序

 一、运行一个 Web 应用

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

[root@VM_0_8_centos test]# docker pull training/webapp  # 载入镜
[root@VM_0_8_centos test]# docker run -d -P training/webapp python app.py

        -d参数:让容器在后台运行。

        -P参数:将容器内部使用的网络端口随机映射到我们使用的主机上。

二、查看 Web 应用容器

[root@VM_0_8_centos test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e672688580a training/webapp "python app.py" 42 hours ago Up 24 hours 0.0.0.0:32770->5000/tcp sweet_hodgkin

        Docker 开放了5000端口(这是Flask的默认端口),映射到主机端口32770上。

        浏览器访问如下:

        

Docker 运行Web程序_应用程序

        通过 -p 命令修改端口:

[root@VM_0_8_centos test]# docker run -d -p 5000:5000 training/webapp python app.py
c2cdfb692a40da7f2130b39b636708a9c5c1d63c211d15cdae75b1e310a93c2c
[root@VM_0_8_centos test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2cdfb692a40 training/webapp "python app.py" 21 seconds ago Up 20 seconds 0.0.0.0:5000->5000/tcp condescending_spence

        容器内部的 5000 端口映射到我们本地主机的 5000 端口上。

三、查看网络端口的快捷方式

        使用 docker ps 可以查看网络端口,也可以用 docker port 快捷方式查看端口:

[root@VM_0_8_centos test]# docker port c2cdfb692a40
5000/tcp -> 0.0.0.0:5000
[root@VM_0_8_centos test]# docker port condescending_spence
5000/tcp -> 0.0.0.0:5000

四、查看 Web 应用程序日志

[root@VM_0_8_centos test]# docker logs -f c2cdfb692a40
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

        -f参数: 让 docker logs 像使用 tail -f 一样来输出容器内部的标准输出。

        从上面,我们可以看到应用程序使用的是 5000 端口并且能够查看到应用程序的访问日志。

五、检测 Web 应用程序

        使用 docker inspect 来查看 Docker 的底层信息。它会返回一个 JSON 文件记录着 Docker 容器的配置和状态信息。

[root@VM_0_8_centos test]# docker inspect condescending_spence

六、停止 Web 应用程序

[root@VM_0_8_centos test]# docker stop condescending_spence
condescending_spence

七、重启 Web 应用程序

        已经停止的容器,我们可以使用命令 docker start 来启动:

[root@VM_0_8_centos test]# docker start c2cdfb692a40
c2cdfb692a40

        docker ps -l 查询最后一次创建的容器:

[root@VM_0_8_centos test]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2cdfb692a40 training/webapp "python app.py" 3 hours ago Up 2 minutes 0.0.0.0:5000->5000/tcp condescending_spence

docker restart 命令来重启:

[root@VM_0_8_centos test]# docker restart c2cdfb692a40
c2cdfb692a40

八、移除 Web 应用程序

        可以使用 docker rm 命令来删除不需要的容器:

[root@VM_0_8_centos test]# docker stop c2cdfb692a40
c2cdfb692a40
[root@VM_0_8_centos test]# docker rm c2cdfb692a40
c2cdfb692a40

        删除容器时,容器必须是停止状态,否则会报错。

        结束!



举报

相关推荐

0 条评论