Skip to content

Commit 111c55d

Browse files
milasglours
andauthored
nginx-flask-mysql: add dev envs support (#272)
* Add Docker Desktop Development Environments config * Change port `5000` -> `8000` for Flask to avoid conflicts on recent macOS versions * Improve DB health check (for non-dev envs case) to avoid producing a bunch of log spam Co-authored-by: Guillaume Lours <[email protected]> Signed-off-by: Milas Bowman <[email protected]>
1 parent 20089c7 commit 111c55d

File tree

5 files changed

+130
-24
lines changed

5 files changed

+130
-24
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
services:
2+
db:
3+
image: mariadb:10-focal
4+
command: '--default-authentication-plugin=mysql_native_password'
5+
restart: always
6+
healthcheck:
7+
test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"]
8+
interval: 3s
9+
retries: 5
10+
start_period: 30s
11+
secrets:
12+
- db-password
13+
volumes:
14+
- db-data:/var/lib/mysql
15+
networks:
16+
- backnet
17+
environment:
18+
- MYSQL_DATABASE=example
19+
- MYSQL_ROOT_PASSWORD_FILE=/run/secrets/db-password
20+
expose:
21+
- 3306
22+
- 33060
23+
24+
backend:
25+
build:
26+
context: backend
27+
target: dev-envs
28+
restart: always
29+
volumes:
30+
- /var/run/docker.sock:/var/run/docker.sock
31+
secrets:
32+
- db-password
33+
ports:
34+
- 8000:8000
35+
networks:
36+
- backnet
37+
- frontnet
38+
depends_on:
39+
db:
40+
condition: service_healthy
41+
42+
proxy:
43+
build: proxy
44+
restart: always
45+
ports:
46+
- 80:80
47+
depends_on:
48+
- backend
49+
networks:
50+
- frontnet
51+
52+
volumes:
53+
db-data:
54+
55+
secrets:
56+
db-password:
57+
file: db/password.txt
58+
59+
networks:
60+
backnet:
61+
frontnet:

nginx-flask-mysql/README.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ Project structure:
1818
```
1919
services:
2020
backend:
21-
build: backend
21+
build:
22+
context: backend
23+
target: builder
2224
...
2325
db:
2426
# We use a mariadb image which supports both amd64 & arm64 architecture
25-
image: mariadb:10.6.4-focal
27+
image: mariadb:10-focal
2628
# If you really want to use MySQL, uncomment the following line
27-
#image: mysql:8.0.27
29+
#image: mysql:8
2830
...
2931
proxy:
3032
build: proxy
@@ -37,7 +39,7 @@ Make sure port 80 on the host is not already being in use.
3739
> ℹ️ **_INFO_**
3840
> For compatibility purpose between `AMD64` and `ARM64` architecture, we use a MariaDB as database instead of MySQL.
3941
> You still can use the MySQL image by uncommenting the following line in the Compose file
40-
> `#image: mysql:8.0.27`
42+
> `#image: mysql:8`
4143
4244
## Deploy with docker compose
4345

@@ -56,15 +58,13 @@ Creating nginx-flask-mysql_proxy_1 ... done
5658

5759
## Expected result
5860

59-
Listing containers must show three containers running and the port mapping as below:
61+
Listing containers should show three containers running and the port mapping as below:
6062
```
61-
$ docker ps
62-
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
63-
c2c703b66b19 nginx-flask-mysql_proxy "nginx -g 'daemon of…" 39 seconds ago Up 38 seconds 0.0.0.0:80->80/tcp nginx-flask-mysql_proxy_1
64-
2b8a21508c3c nginx-flask-mysql_backend "/bin/sh -c 'flask r…" 9 minutes ago Up 38 seconds 0.0.0.0:5000->5000/tcp nginx-flask-mysql_backend_1
65-
0e6a96ea2028 mysql:8.0.19 "docker-entrypoint.s…" 9 minutes ago Up 38 seconds 3306/tcp, 33060/tcp nginx-flask-mysql_db_1
66-
67-
63+
$ docker compose ps
64+
NAME COMMAND SERVICE STATUS PORTS
65+
nginx-flask-mysql-backend-1 "flask run" backend running 0.0.0.0:8000->8000/tcp
66+
nginx-flask-mysql-db-1 "docker-entrypoint.s…" db running (healthy) 3306/tcp, 33060/tcp
67+
nginx-flask-mysql-proxy-1 "nginx -g 'daemon of…" proxy running 0.0.0.0:80->80/tcp
6868
```
6969

7070
After the application starts, navigate to `http://localhost:80` in your web browser or run:
@@ -77,3 +77,14 @@ Stop and remove the containers
7777
```
7878
$ docker compose down
7979
```
80+
81+
## Use with Docker Development Environments
82+
83+
You can use this sample with the Dev Environments feature of Docker Desktop.
84+
85+
![Screenshot of creating a Dev Environment in Docker Desktop](../dev-envs.png)
86+
87+
To develop directly on the services inside containers, use the HTTPS Git url of the sample:
88+
```
89+
https://github.com/docker/awesome-compose/tree/master/nginx-flask-mysql
90+
```
Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
1-
FROM python:3.8-alpine
1+
# syntax=docker/dockerfile:1.4
2+
FROM --platform=$BUILDPLATFORM python:3.10-alpine AS builder
3+
24
WORKDIR /code
3-
COPY requirements.txt /code/
4-
RUN pip install -r requirements.txt --no-cache-dir
5-
COPY . /code/
5+
COPY requirements.txt /code
6+
RUN --mount=type=cache,target=/root/.cache/pip \
7+
pip3 install -r requirements.txt
8+
9+
COPY . .
10+
611
ENV FLASK_APP hello.py
7-
CMD flask run --host=0.0.0.0
8-
12+
ENV FLASK_ENV development
13+
ENV FLASK_RUN_PORT 8000
14+
ENV FLASK_RUN_HOST 0.0.0.0
15+
16+
EXPOSE 8000
17+
18+
CMD ["flask", "run"]
19+
20+
FROM builder AS dev-envs
21+
22+
RUN <<EOF
23+
apk update
24+
apk add git
25+
EOF
26+
27+
RUN <<EOF
28+
addgroup -S docker
29+
adduser -S --shell /bin/bash --ingroup docker vscode
30+
EOF
31+
32+
# install Docker tools (cli, buildx, compose)
33+
COPY --from=gloursdocker/docker / /
34+
35+
CMD ["flask", "run"]

nginx-flask-mysql/compose.yaml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
services:
22
db:
33
# We use a mariadb image which supports both amd64 & arm64 architecture
4-
image: mariadb:10.6.4-focal
4+
image: mariadb:10-focal
55
# If you really want to use MySQL, uncomment the following line
6-
#image: mysql:8.0.27
6+
#image: mysql:8
77
command: '--default-authentication-plugin=mysql_native_password'
88
restart: always
99
healthcheck:
10-
test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"]
10+
test: ['CMD-SHELL', 'mysqladmin ping -h 127.0.0.1 --password="$$(cat /run/secrets/db-password)" --silent']
1111
interval: 3s
1212
retries: 5
1313
start_period: 30s
@@ -23,19 +23,23 @@ services:
2323
expose:
2424
- 3306
2525
- 33060
26+
2627
backend:
27-
build: backend
28+
build:
29+
context: backend
30+
target: builder
2831
restart: always
2932
secrets:
3033
- db-password
3134
ports:
32-
- 5000:5000
35+
- 8000:8000
3336
networks:
3437
- backnet
3538
- frontnet
3639
depends_on:
3740
db:
3841
condition: service_healthy
42+
3943
proxy:
4044
build: proxy
4145
restart: always
@@ -45,11 +49,14 @@ services:
4549
- backend
4650
networks:
4751
- frontnet
52+
4853
volumes:
4954
db-data:
55+
5056
secrets:
5157
db-password:
5258
file: db/password.txt
59+
5360
networks:
5461
backnet:
5562
frontnet:

nginx-flask-mysql/proxy/conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ server {
22
listen 80;
33
server_name localhost;
44
location / {
5-
proxy_pass http://backend:5000;
5+
proxy_pass http://backend:8000;
66
}
77

88
}

0 commit comments

Comments
 (0)