diff --git a/nginx-flask-mysql/.docker/docker-compose.yaml b/nginx-flask-mysql/.docker/docker-compose.yaml new file mode 100644 index 0000000..b15dda7 --- /dev/null +++ b/nginx-flask-mysql/.docker/docker-compose.yaml @@ -0,0 +1,61 @@ +services: + db: + image: mariadb:10-focal + command: '--default-authentication-plugin=mysql_native_password' + restart: always + healthcheck: + test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"] + interval: 3s + retries: 5 + start_period: 30s + secrets: + - db-password + volumes: + - db-data:/var/lib/mysql + networks: + - backnet + environment: + - MYSQL_DATABASE=example + - MYSQL_ROOT_PASSWORD_FILE=/run/secrets/db-password + expose: + - 3306 + - 33060 + + backend: + build: + context: backend + target: dev-envs + restart: always + volumes: + - /var/run/docker.sock:/var/run/docker.sock + secrets: + - db-password + ports: + - 8000:8000 + networks: + - backnet + - frontnet + depends_on: + db: + condition: service_healthy + + proxy: + build: proxy + restart: always + ports: + - 80:80 + depends_on: + - backend + networks: + - frontnet + +volumes: + db-data: + +secrets: + db-password: + file: db/password.txt + +networks: + backnet: + frontnet: diff --git a/nginx-flask-mysql/README.md b/nginx-flask-mysql/README.md index ba2cdd3..2d21a39 100644 --- a/nginx-flask-mysql/README.md +++ b/nginx-flask-mysql/README.md @@ -18,13 +18,15 @@ Project structure: ``` services: backend: - build: backend + build: + context: backend + target: builder ... db: # We use a mariadb image which supports both amd64 & arm64 architecture - image: mariadb:10.6.4-focal + image: mariadb:10-focal # If you really want to use MySQL, uncomment the following line - #image: mysql:8.0.27 + #image: mysql:8 ... proxy: build: proxy @@ -37,7 +39,7 @@ Make sure port 80 on the host is not already being in use. > ℹ️ **_INFO_** > For compatibility purpose between `AMD64` and `ARM64` architecture, we use a MariaDB as database instead of MySQL. > You still can use the MySQL image by uncommenting the following line in the Compose file -> `#image: mysql:8.0.27` +> `#image: mysql:8` ## Deploy with docker compose @@ -56,15 +58,13 @@ Creating nginx-flask-mysql_proxy_1 ... done ## Expected result -Listing containers must show three containers running and the port mapping as below: +Listing containers should show three containers running and the port mapping as below: ``` -$ docker ps -CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -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 -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 -0e6a96ea2028 mysql:8.0.19 "docker-entrypoint.s…" 9 minutes ago Up 38 seconds 3306/tcp, 33060/tcp nginx-flask-mysql_db_1 - - +$ docker compose ps +NAME COMMAND SERVICE STATUS PORTS +nginx-flask-mysql-backend-1 "flask run" backend running 0.0.0.0:8000->8000/tcp +nginx-flask-mysql-db-1 "docker-entrypoint.s…" db running (healthy) 3306/tcp, 33060/tcp +nginx-flask-mysql-proxy-1 "nginx -g 'daemon of…" proxy running 0.0.0.0:80->80/tcp ``` After the application starts, navigate to `http://localhost:80` in your web browser or run: @@ -77,3 +77,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/nginx-flask-mysql +``` diff --git a/nginx-flask-mysql/backend/Dockerfile b/nginx-flask-mysql/backend/Dockerfile index bc71f1d..c2ef501 100755 --- a/nginx-flask-mysql/backend/Dockerfile +++ b/nginx-flask-mysql/backend/Dockerfile @@ -1,8 +1,35 @@ -FROM python:3.8-alpine +# 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 -COPY . /code/ +COPY requirements.txt /code +RUN --mount=type=cache,target=/root/.cache/pip \ + pip3 install -r requirements.txt + +COPY . . + ENV FLASK_APP hello.py -CMD flask run --host=0.0.0.0 - \ No newline at end of file +ENV FLASK_ENV development +ENV FLASK_RUN_PORT 8000 +ENV FLASK_RUN_HOST 0.0.0.0 + +EXPOSE 8000 + +CMD ["flask", "run"] + +FROM builder AS dev-envs + +RUN <