2022-03-17 16:33:36 +00:00
|
|
|
## Compose sample application
|
|
|
|
|
|
|
|
### Python/Flask application using a Redis database
|
|
|
|
|
|
|
|
Project structure:
|
|
|
|
|
|
|
|
```
|
|
|
|
.
|
|
|
|
├── Dockerfile
|
|
|
|
├── README.md
|
|
|
|
├── app.py
|
2022-05-10 09:59:25 +00:00
|
|
|
├── compose.yaml
|
2022-03-17 16:33:36 +00:00
|
|
|
└── requirements.txt
|
|
|
|
```
|
|
|
|
|
2022-05-10 09:59:25 +00:00
|
|
|
[_compose.yaml_](compose.yaml)
|
2022-03-17 16:33:36 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
services:
|
|
|
|
redis:
|
|
|
|
image: redislabs/redismod
|
|
|
|
ports:
|
|
|
|
- '6379:6379'
|
|
|
|
web:
|
|
|
|
build: .
|
|
|
|
ports:
|
2022-07-08 14:48:08 +00:00
|
|
|
- "8000:8000"
|
2022-03-17 16:33:36 +00:00
|
|
|
volumes:
|
|
|
|
- .:/code
|
|
|
|
depends_on:
|
|
|
|
- redis
|
|
|
|
```
|
|
|
|
|
2022-05-10 09:59:25 +00:00
|
|
|
## Deploy with docker compose
|
2022-03-17 16:33:36 +00:00
|
|
|
|
|
|
|
```
|
2022-05-10 09:59:25 +00:00
|
|
|
$ docker compose up -d
|
2022-03-17 16:33:36 +00:00
|
|
|
[+] 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:
|
|
|
|
```
|
|
|
|
|
2022-05-10 09:59:25 +00:00
|
|
|
$ docker compose ps
|
2022-03-17 16:33:36 +00:00
|
|
|
NAME COMMAND SERVICE STATUS PORTS
|
|
|
|
flask-redis-redis-1 "redis-server --load…" redis running 0.0.0.0:6379->6379/tcp
|
2022-07-08 14:48:08 +00:00
|
|
|
flask-redis-web-1 "/bin/sh -c 'python …" web running 0.0.0.0:8000->8000/tcp
|
2022-03-17 16:33:36 +00:00
|
|
|
```
|
|
|
|
|
2022-07-08 14:48:08 +00:00
|
|
|
After the application starts, navigate to `http://localhost:8000` in your web browser or run:
|
2022-03-17 16:33:36 +00:00
|
|
|
```
|
2022-07-08 14:48:08 +00:00
|
|
|
$ curl localhost:8000
|
2022-03-17 16:33:36 +00:00
|
|
|
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
|
|
|
|
```
|
2022-05-10 09:59:25 +00:00
|
|
|
$ docker compose down
|
2022-03-17 16:33:36 +00:00
|
|
|
```
|
2022-07-08 14:48:08 +00:00
|
|
|
|
|
|
|
## Use with Docker Development Environments
|
|
|
|
|
|
|
|
You can use this sample with the Dev Environments feature of Docker Desktop.
|
|
|
|
|
|
|
|
![Screenshot of creating a Dev Environment in Docker Desktop](../dev-envs.png)
|
|
|
|
|
|
|
|
To develop directly on the services inside containers, use the HTTPS Git url of the sample:
|
|
|
|
```
|
|
|
|
https://github.com/docker/awesome-compose/tree/master/flask-redis
|
|
|
|
```
|