mirror of
https://github.com/docker/awesome-compose.git
synced 2025-04-19 07:18:06 +02:00
Nginx/Node/Redis and Flask/Redis Compose example (#222)
* Added Node, Nginx and Redis Project * Added Flask/Redis
This commit is contained in:
parent
05095bbe0d
commit
04f8c9ca12
15 changed files with 727 additions and 2 deletions
6
flask-redis/Dockerfile
Normal file
6
flask-redis/Dockerfile
Normal file
|
@ -0,0 +1,6 @@
|
|||
FROM python:3.11.0a6-alpine3.15
|
||||
WORKDIR /code
|
||||
COPY requirements.txt /code
|
||||
RUN pip install -r requirements.txt --no-cache-dir
|
||||
COPY . /code
|
||||
CMD python app.py
|
82
flask-redis/README.md
Normal file
82
flask-redis/README.md
Normal file
|
@ -0,0 +1,82 @@
|
|||
## Compose sample application
|
||||
|
||||
### Python/Flask application using a Redis database
|
||||
|
||||
Project structure:
|
||||
|
||||
```
|
||||
.
|
||||
├── Dockerfile
|
||||
├── README.md
|
||||
├── app.py
|
||||
├── docker-compose.yml
|
||||
└── requirements.txt
|
||||
```
|
||||
|
||||
[_docker-compose.yml_](docker-compose.yml)
|
||||
|
||||
```
|
||||
services:
|
||||
redis:
|
||||
image: redislabs/redismod
|
||||
ports:
|
||||
- '6379:6379'
|
||||
web:
|
||||
build: .
|
||||
ports:
|
||||
- "5000:5000"
|
||||
volumes:
|
||||
- .:/code
|
||||
depends_on:
|
||||
- redis
|
||||
```
|
||||
|
||||
## Deploy with docker-compose
|
||||
|
||||
```
|
||||
$ docker-compose up -d
|
||||
[+] Running 24/24
|
||||
⠿ redis Pulled
|
||||
...
|
||||
⠿ 565225d89260 Pull complete
|
||||
[+] Building 12.7s (10/10) FINISHED
|
||||
=> [internal] load build definition from Dockerfile ...
|
||||
[+] Running 3/3
|
||||
⠿ Network flask-redis_default Created
|
||||
⠿ Container flask-redis-redis-1 Started
|
||||
⠿ Container flask-redis-web-1 Started
|
||||
```
|
||||
|
||||
## Expected result
|
||||
|
||||
Listing containers must show one container running and the port mapping as below:
|
||||
```
|
||||
|
||||
$ docker-compose ps
|
||||
NAME COMMAND SERVICE STATUS PORTS
|
||||
flask-redis-redis-1 "redis-server --load…" redis running 0.0.0.0:6379->6379/tcp
|
||||
flask-redis-web-1 "/bin/sh -c 'python …" web running 0.0.0.0:5000->5000/tcp
|
||||
```
|
||||
|
||||
After the application starts, navigate to `http://localhost:5000` in your web browser or run:
|
||||
```
|
||||
$ curl localhost:5000
|
||||
This webpage has been viewed 2 time(s)
|
||||
```
|
||||
|
||||
## Monitoring Redis keys
|
||||
|
||||
Connect to redis database by using ```redis-cli``` command and monitor the keys.
|
||||
```
|
||||
redis-cli -p 6379
|
||||
127.0.0.1:6379> monitor
|
||||
OK
|
||||
1646634062.732496 [0 172.21.0.3:33106] "INCRBY" "hits" "1"
|
||||
1646634062.735669 [0 172.21.0.3:33106] "GET" "hits"
|
||||
```
|
||||
|
||||
|
||||
Stop and remove the containers
|
||||
```
|
||||
$ docker-compose down
|
||||
```
|
14
flask-redis/app.py
Normal file
14
flask-redis/app.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
from flask import Flask
|
||||
from redis import Redis
|
||||
|
||||
app = Flask(__name__)
|
||||
redis = Redis(host='redis', port=6379)
|
||||
|
||||
@app.route('/')
|
||||
def hello():
|
||||
redis.incr('hits')
|
||||
counter = str(redis.get('hits'),'utf-8')
|
||||
return "This webpage has been viewed "+counter+" time(s)"
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", debug=True)
|
13
flask-redis/docker-compose.yml
Normal file
13
flask-redis/docker-compose.yml
Normal file
|
@ -0,0 +1,13 @@
|
|||
services:
|
||||
redis:
|
||||
image: redislabs/redismod
|
||||
ports:
|
||||
- '6379:6379'
|
||||
web:
|
||||
build: .
|
||||
ports:
|
||||
- "5000:5000"
|
||||
volumes:
|
||||
- .:/code
|
||||
depends_on:
|
||||
- redis
|
2
flask-redis/requirements.txt
Normal file
2
flask-redis/requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
flask
|
||||
redis
|
Loading…
Add table
Add a link
Reference in a new issue