在Docker中运行web应用

时间:2023-03-09 19:02:20
在Docker中运行web应用

启动一个简单的web 应用

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

  1. # docker run -d -P training/webapp python app.py
  2. Unable to find image 'training/webapp' locally
  3. Pulling repository training/webapp
  4. 31fa814ba25a: Download complete
  5. 511136ea3c5a: Download complete
  6. f10ebce2c0e1: Download complete
  7. 82cdea7ab5b5: Download complete
  8. 5dbd9cb5a02f: Download complete
  9. 74fe38d11401: Download complete
  10. 64523f641a05: Download complete
  11. 0e2afc9aad6e: Download complete
  12. e8fc7643ceb1: Download complete
  13. 733b0e3dbcee: Download complete
  14. a1feb043c441: Download complete
  15. e12923494f6a: Download complete
  16. a15f98c46748: Download complete
  17. Status: Downloaded newer image for training/webapp:latest
  18. d00f94a31e8767271f68ab72eab15a8e805c416b0636877f22a31572d10b718d

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

-P 映射一个网络端口

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

  1. # docker ps -l
  2. CONTAINER ID        IMAGE                    COMMAND             CREATED             STATUS              PORTS                     NAMES
  3. 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中的5000端口,映射到host的49153端口。5000是Python Flask 的默认端口。

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

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

在Docker中运行web应用

查看web应用信息

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

  1. # docker port hopeful_lalande 5000
  2. 0.0.0.0:49153

查看web应用日志

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

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

查看进程详细信息

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

查看web应用容器详细信息

  1. # docker inspect hopeful_lalande
  2. [{
  3. "AppArmorProfile": "",
  4. "Args": [
  5. "app.py"
  6. ],
  7. "Config": {
  8. "AttachStderr": false,
  9. "AttachStdin": false,
  10. "AttachStdout": false,
  11. "Cmd": [
  12. "python",
  13. "app.py"
  14. ],
  15. "CpuShares": 0,
  16. "Cpuset": "",
  17. "Domainname": "",
  18. "Entrypoint": null,
  19. "Env": [
  20. "HOME=/",
  21. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  22. ],
  23. "ExposedPorts": {
  24. "5000/tcp": {}
  25. },
  26. "Hostname": "d00f94a31e87",
  27. "Image": "training/webapp",
  28. "Memory": 0,
  29. "MemorySwap": 0,
  30. "NetworkDisabled": false,
  31. "OnBuild": null,
  32. "OpenStdin": false,
  33. "PortSpecs": null,
  34. "StdinOnce": false,
  35. "Tty": false,
  36. "User": "",
  37. "Volumes": null,
  38. "WorkingDir": "/opt/webapp"
  39. },
  40. "Created": "2014-12-17T02:24:09.468631143Z",
  41. "Driver": "devicemapper",
  42. "ExecDriver": "native-0.2",
  43. "HostConfig": {
  44. "Binds": null,
  45. "CapAdd": null,
  46. "CapDrop": null,
  47. "ContainerIDFile": "",
  48. "Devices": [],
  49. "Dns": null,
  50. "DnsSearch": null,
  51. "ExtraHosts": null,
  52. "Links": null,
  53. "LxcConf": [],
  54. "NetworkMode": "bridge",
  55. "PortBindings": {},
  56. "Privileged": false,
  57. "PublishAllPorts": true,
  58. "RestartPolicy": {
  59. "MaximumRetryCount": 0,
  60. "Name": ""
  61. },
  62. "SecurityOpt": null,
  63. "VolumesFrom": null
  64. },
  65. "HostnamePath": "/var/lib/docker/containers/d00f94a31e8767271f68ab72eab15a8e805c416b0636877f22a31572d10b718d/hostname",
  66. "HostsPath": "/var/lib/docker/containers/d00f94a31e8767271f68ab72eab15a8e805c416b0636877f22a31572d10b718d/hosts",
  67. "Id": "d00f94a31e8767271f68ab72eab15a8e805c416b0636877f22a31572d10b718d",
  68. "Image": "31fa814ba25ae3426f8710df7a48d567d4022527ef2c14964bb8bc45e653417c",
  69. "MountLabel": "",
  70. "Name": "/hopeful_lalande",
  71. "NetworkSettings": {
  72. "Bridge": "docker0",
  73. "Gateway": "172.17.42.1",
  74. "IPAddress": "172.17.0.8",
  75. "IPPrefixLen": 16,
  76. "MacAddress": "02:42:ac:11:00:08",
  77. "PortMapping": null,
  78. "Ports": {
  79. "5000/tcp": [
  80. {
  81. "HostIp": "0.0.0.0",
  82. "HostPort": "49153"
  83. }
  84. ]
  85. }
  86. },
  87. "Path": "python",
  88. "ProcessLabel": "",
  89. "ResolvConfPath": "/var/lib/docker/containers/d00f94a31e8767271f68ab72eab15a8e805c416b0636877f22a31572d10b718d/resolv.conf",
  90. "State": {
  91. "ExitCode": 0,
  92. "FinishedAt": "0001-01-01T00:00:00Z",
  93. "Paused": false,
  94. "Pid": 15189,
  95. "Restarting": false,
  96. "Running": true,
  97. "StartedAt": "2014-12-17T02:24:11.279426855Z"
  98. },
  99. "Volumes": {},
  100. "VolumesRW": {}
  101. }
  102. ]

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

也可以通过指定名称,获取某项信息值,如下:

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

停止Web应用

  1. # docker stop hopeful_lalande
  2. hopeful_lalande

停止后,可以使用docker ps -a命令,列出之前所有docker容器

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

重启Web应用

  1. # docker start hopeful_lalande
  2. hopeful_lalande

删除Web应用

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

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

停止该container后,重新执行上条命令,成功删除container