Skip to content

Commit

Permalink
flask-redis: dev envs support & misc improvements (docker#265)
Browse files Browse the repository at this point in the history
(Most of this is almost identical to docker#263.)

* Docker Desktop Development Environments config
* Use cache volumes for pip
* Downgrade from Python 3.11 _alpha_ -> Python 3.10
* Use port `8000` to avoid conflicts with Airplay on
    macOS for default Flask port `5000`
* Use `SIGINT` to gracefully stop Flask
* Formatting fixes in `compose.yaml`

Signed-off-by: Milas Bowman <[email protected]>
  • Loading branch information
milas authored Jul 8, 2022
1 parent ec55256 commit b53e454
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 20 deletions.
16 changes: 16 additions & 0 deletions flask-redis/.docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
services:
redis:
image: redislabs/redismod
ports:
- '6379:6379'
web:
build:
context: .
target: dev-envs
stop_signal: SIGINT
ports:
- '8000:8000'
volumes:
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
- redis
27 changes: 24 additions & 3 deletions flask-redis/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
FROM python:3.11.0a6-alpine3.15
# syntax=docker/dockerfile:1.4
FROM --platform=$BUILDPLATFORM python:3.10-alpine AS builder

WORKDIR /code

COPY requirements.txt /code
RUN pip install -r requirements.txt --no-cache-dir
RUN --mount=type=cache,target=/root/.cache/pip \
pip3 install -r requirements.txt

COPY . /code
CMD python app.py

ENTRYPOINT ["python3"]
CMD ["app.py"]

FROM builder as dev-envs

RUN <<EOF
apk update
apk add git bash
EOF

RUN <<EOF
addgroup -S docker
adduser -S --shell /bin/bash --ingroup docker vscode
EOF
# install Docker tools (cli, buildx, compose)
COPY --from=gloursdocker/docker / /
19 changes: 15 additions & 4 deletions flask-redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ services:
web:
build: .
ports:
- "5000:5000"
- "8000:8000"
volumes:
- .:/code
depends_on:
Expand Down Expand Up @@ -55,12 +55,12 @@ 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
flask-redis-web-1 "/bin/sh -c 'python …" web running 0.0.0.0:8000->8000/tcp
```

After the application starts, navigate to `http://localhost:5000` in your web browser or run:
After the application starts, navigate to `http://localhost:8000` in your web browser or run:
```
$ curl localhost:5000
$ curl localhost:8000
This webpage has been viewed 2 time(s)
```

Expand All @@ -80,3 +80,14 @@ Stop and remove the containers
```
$ docker compose down
```

## 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
```
2 changes: 1 addition & 1 deletion flask-redis/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def hello():
return "This webpage has been viewed "+counter+" time(s)"

if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
app.run(host="0.0.0.0", port=8000, debug=True)
29 changes: 17 additions & 12 deletions flask-redis/compose.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
services:
redis:
image: redislabs/redismod
ports:
- '6379:6379'
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
depends_on:
- redis
redis:
image: redislabs/redismod
ports:
- '6379:6379'
web:
build:
context: .
target: builder
# flask requires SIGINT to stop gracefully
# (default stop signal from Compose is SIGTERM)
stop_signal: SIGINT
ports:
- '8000:8000'
volumes:
- .:/code
depends_on:
- redis

0 comments on commit b53e454

Please sign in to comment.