docker_practice/compose/usage.md

75 lines
1.4 KiB
Go
Raw Permalink Normal View History

# 使
2015-05-08 20:10:01 +08:00
##
2017-12-10 11:52:09 +08:00
2015-05-08 20:10:01 +08:00
2017-11-26 09:54:04 +08:00
* (`service`)
2017-11-23 15:48:52 +08:00
2017-11-26 09:54:04 +08:00
* (`project`)
2015-05-11 14:46:51 +08:00
2017-11-26 09:54:04 +08:00
`Compose`
2015-05-11 14:46:51 +08:00
##
2015-05-11 14:46:51 +08:00
2017-12-10 11:52:09 +08:00
web web
2015-05-11 14:46:51 +08:00
2017-12-10 11:52:09 +08:00
`Python` 访 web
2015-05-11 14:46:51 +08:00
### web
2015-05-11 14:46:51 +08:00
2017-12-10 11:52:09 +08:00
`app.py`
2015-05-11 14:46:51 +08:00
2017-11-13 18:54:38 +08:00
```python
2017-12-10 11:52:09 +08:00
from flask import Flask
from redis import Redis
2015-05-11 14:46:51 +08:00
2017-12-10 11:52:09 +08:00
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)
2015-05-11 14:46:51 +08:00
```
### Dockerfile
2017-12-10 11:52:09 +08:00
2017-11-01 00:20:30 +08:00
`Dockerfile`
2017-12-10 11:52:09 +08:00
```docker
2017-12-10 11:52:09 +08:00
FROM python:3.6-alpine
2015-05-11 14:46:51 +08:00
ADD . /code
2017-12-10 11:52:09 +08:00
WORKDIR /code
RUN pip install redis flask
CMD ["python", "app.py"]
2015-05-11 14:46:51 +08:00
```
### docker-compose.yml
2017-12-10 11:52:09 +08:00
`docker-compose.yml` Compose 使
2015-05-11 14:46:51 +08:00
2017-11-01 00:20:30 +08:00
```yaml
2017-12-10 11:52:09 +08:00
version: '3'
2017-11-01 00:20:30 +08:00
services:
2017-11-23 15:48:52 +08:00
2017-12-10 11:52:09 +08:00
web:
build: .
2015-05-11 14:46:51 +08:00
ports:
2017-12-10 11:52:09 +08:00
- "5000:5000"
2018-12-31 14:50:01 +08:00
2017-12-10 11:52:09 +08:00
redis:
image: "redis:alpine"
2015-05-11 14:46:51 +08:00
```
### compose
2017-12-10 11:52:09 +08:00
2017-11-13 18:54:38 +08:00
```bash
$ docker-compose up
2015-05-11 14:46:51 +08:00
```
2017-12-10 11:52:09 +08:00
访 `5000` 1