Docker-compose部署简单的web

  • 1 新建一个文件夹,编写一个app文件
[root@doc dockertest]# vi app.py 
from flask import Flask
from redis import Redis
app = Flask(__name__)
redis = Redis(host='redis', port=6379)
@app.route('/')
def hello():
  count = redis.incr('hits')
  return 'Hello World! 该页面已被访问 {} 次。\n'.format(count)
if __name__ == "__main__":
  app.run(host="0.0.0.0", debug=True)
  • 2 编写Dockerfile
[root@doc dockertest]# vi Dockerfile 
FROM python:3.6-alpine
ADD . /code
WORKDIR /code
RUN pip install redis flask
CMD ["python", "app.py"]
  • 3 编写yml文件
[root@doc dockertest]# vi docker-compose.yml 
version: '3'
services:
  web:
    build: .
    ports:
    - "5000:5000"

  redis:
    image: "redis:alpine"
  • 4 运行compose
[root@doc dockertest]# docker-compose up
Building web
Step 1/5 : FROM python:3.6-alpine
 ---> 35bb01a3d284
Step 2/5 : ADD . /code
 ---> 5e3c17c61c39
Step 3/5 : WORKDIR /code
 ---> Running in 919874ca1cd5
Removing intermediate container 919874ca1cd5
 ---> d71d1bef2f09
Step 4/5 : RUN pip install redis flask
 ---> Running in da6b4c057124
Collecting redis
  Downloading https://files.pythonhosted.org/packages/ac/a7/cff10cc5f1180834a3ed564d148fb4329c989cbb1f2e196fc9a10fa07072/redis-3.2.1-py2.py3-none-any.whl (65kB)
Collecting flask
  Downloading https://files.pythonhosted.org/packages/9a/74/670ae9737d14114753b8c8fdf2e8bd212a05d3b361ab15b44937dfd40985/Flask-1.0.3-py2.py3-none-any.whl (92kB)
Collecting Jinja2>=2.10 (from flask)
  Downloading https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl (124kB)
Collecting Werkzeug>=0.14 (from flask)
  Downloading https://files.pythonhosted.org/packages/9f/57/92a497e38161ce40606c27a86759c6b92dd34fcdb33f64171ec559257c02/Werkzeug-0.15.4-py2.py3-none-any.whl (327kB)
Collecting itsdangerous>=0.24 (from flask)
  Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting click>=5.1 (from flask)
  Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->flask)
  Downloading https://files.pythonhosted.org/packages/b9/2e/64db92e53b86efccfaea71321f597fa2e1b2bd3853d8ce658568f7a13094/MarkupSafe-1.1.1.tar.gz
Building wheels for collected packages: MarkupSafe
  Building wheel for MarkupSafe (setup.py): started
  Building wheel for MarkupSafe (setup.py): finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/f2/aa/04/0edf07a1b8a5f5f1aed7580fffb69ce8972edc16a505916a77
Successfully built MarkupSafe
Installing collected packages: redis, MarkupSafe, Jinja2, Werkzeug, itsdangerous, click, flask
Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.4 click-7.0 flask-1.0.3 itsdangerous-1.1.0 redis-3.2.1
Removing intermediate container da6b4c057124
 ---> 211946ad3f73
Step 5/5 : CMD ["python", "app.py"]
 ---> Running in 8a99efcfcb92
Removing intermediate container 8a99efcfcb92
 ---> d61d5441217c
Successfully built d61d5441217c
Successfully tagged dockertest_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Pulling redis (redis:alpine)...
alpine: Pulling from library/redis
921b31ab772b: Pull complete
da982f901912: Pull complete
b0a650576779: Pull complete
600c8b9b6cf4: Pull complete
9addbabe0fbf: Pull complete
71dc983a976c: Pull complete
Digest: sha256:7d2a7f4444bf4728f79360cc7b36b9537ab73782284e90d3e07f6098ffe92b71
Status: Downloaded newer image for redis:alpine
Creating dockertest_web_1   ... done
Creating dockertest_redis_1 ... done
Attaching to dockertest_redis_1, dockertest_web_1
redis_1  | 1:C 26 Jun 2019 03:37:07.397 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1  | 1:C 26 Jun 2019 03:37:07.397 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1  | 1:C 26 Jun 2019 03:37:07.397 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1  | 1:M 26 Jun 2019 03:37:07.398 * Running mode=standalone, port=6379.
redis_1  | 1:M 26 Jun 2019 03:37:07.398 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1  | 1:M 26 Jun 2019 03:37:07.398 # Server initialized
redis_1  | 1:M 26 Jun 2019 03:37:07.398 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1  | 1:M 26 Jun 2019 03:37:07.398 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1  | 1:M 26 Jun 2019 03:37:07.398 * Ready to accept connections
web_1    |  * Serving Flask app "app" (lazy loading)
web_1    |  * Environment: production
web_1    |    WARNING: This is a development server. Do not use it in a production deployment.
web_1    |    Use a production WSGI server instead.
web_1    |  * Debug mode: on
web_1    |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web_1    |  * Restarting with stat
web_1    |  * Debugger is active!
web_1    |  * Debugger PIN: 171-941-593
  • 5 访问页面


    Docker-compose部署简单的web_第1张图片
  • 6 查看镜像及容器
[root@doc dockertest]# docker-compose top
dockertest_redis_1
UID   PID    PPID   C   STIME   TTY     TIME         CMD     
-------------------------------------------------------------
100   2083   2042   0   11:37   ?     00:00:00   redis-server

dockertest_web_1
UID    PID    PPID   C   STIME   TTY     TIME                    CMD                
------------------------------------------------------------------------------------
root   2195   2172   0   11:37   ?     00:00:00   python app.py                     
root   2350   2195   0   11:37   ?     00:00:05   /usr/local/bin/python /code/app.py
  • 7 注意

yaml配置文件格式
大小写敏感
使用缩进表示层级关系
缩进时不允许使用 TAB 键,只允许使用空格
缩进的空格数目不重要,只要相同层级的元素左侧对齐即可

你可能感兴趣的:(Docker-compose部署简单的web)