소스 검색

nginx-aspnet-mysql: add dev envs configuration (#267)

* nginx-aspnet-mysql: add dev envs configuration
* Fix/enable MySQL/MariaDB health check
* Refactor `Dockerfile` for use as Compose app as well as with
  dev envs
* nginx-aspnet-mysql: remove DB healthcheck from dev envs config

Signed-off-by: Milas Bowman <milas.bowman@docker.com>
Milas Bowman 3 년 전
부모
커밋
9f4f9d8fb8
4개의 변경된 파일101개의 추가작업 그리고 24개의 파일을 삭제
  1. 38 0
      nginx-aspnet-mysql/.docker/docker-compose.yaml
  2. 15 3
      nginx-aspnet-mysql/README.md
  3. 36 11
      nginx-aspnet-mysql/backend/Dockerfile
  4. 12 10
      nginx-aspnet-mysql/compose.yaml

+ 38 - 0
nginx-aspnet-mysql/.docker/docker-compose.yaml

@@ -0,0 +1,38 @@
+services:
+  backend:
+    build:
+      context: backend
+      target: dev-envs
+    restart: always
+    secrets:
+      - db-password
+    depends_on: ['db']
+    environment:
+      - ASPNETCORE_URLS=http://+:8000
+    volumes:
+      - /var/run/docker.sock:/var/run/docker.sock
+
+  db:
+    image: mariadb:10-focal
+    command: '--default-authentication-plugin=mysql_native_password'
+    restart: always
+    secrets:
+      - db-password
+    volumes:
+      - db-data:/var/lib/mysql
+    environment:
+      - MYSQL_DATABASE=example
+      - MYSQL_ROOT_PASSWORD_FILE=/run/secrets/db-password
+
+  proxy:
+    build: proxy
+    ports:
+      - 80:80
+    depends_on: 
+      - backend
+
+volumes:
+  db-data:
+secrets:
+  db-password:
+    file: db/password.txt

+ 15 - 3
nginx-aspnet-mysql/README.md

@@ -21,13 +21,14 @@ Project structure:
 ```
 services:
   backend:
-    build: backend
+    build:
+      context: backend
     ...
   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
@@ -74,3 +75,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-aspnet-mysql
+```

+ 36 - 11
nginx-aspnet-mysql/backend/Dockerfile

@@ -1,17 +1,42 @@
+# syntax=docker/dockerfile:1.4
 
-FROM mcr.microsoft.com/dotnet/aspnet:6.0 as base
-WORKDIR /app
+FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:6.0 AS base
 
-FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
-COPY . /src
 WORKDIR /src
-RUN ls
-RUN dotnet build "aspnetapp.csproj" -c Release -o /app/build
 
-FROM build AS publish
-RUN dotnet publish "aspnetapp.csproj" -c Release -o /app/publish
+COPY aspnetapp.csproj ./
+RUN ["dotnet", "restore"]
+
+FROM base as builder
+
+COPY . .
+
+CMD ["dotnet", "build", "-c", "-o", "/build"]
+
+FROM builder as dev-envs
+
+RUN <<EOF
+apt-get update
+apt-get install -y git
+EOF
+
+RUN <<EOF
+useradd -s /bin/bash -m vscode
+groupadd docker
+usermod -aG docker vscode
+EOF
+# install Docker tools (cli, buildx, compose)
+COPY --from=gloursdocker/docker / /
+
+CMD ["dotnet", "run"]
+
+FROM builder AS publisher
+
+RUN ["dotnet", "publish", "-c", "Release", "-o", "/build"]
+
+FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/aspnet:6.0
 
-FROM base AS final
 WORKDIR /app
-COPY --from=publish /app/publish .
-ENTRYPOINT ["dotnet", "aspnetapp.dll"]
+COPY --from=publisher /build .
+
+CMD ["dotnet", "aspnetapp.dll"]

+ 12 - 10
nginx-aspnet-mysql/compose.yaml

@@ -1,25 +1,25 @@
 services:
   backend:
-    build: backend
+    build:
+      context: backend
     restart: always
     secrets:
       - db-password
-    depends_on: 
-      - db
+    depends_on:
+      db:
+        condition: service_healthy
     environment:
       - ASPNETCORE_URLS=http://+:8000
-#    depends_on:
-#      db:
-#        condition: service_healthy
+
   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
     command: '--default-authentication-plugin=mysql_native_password'
     restart: always
     healthcheck:
-      test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"]
+      test: ['CMD-SHELL', 'mysqladmin ping -h 127.0.0.1 --password="$$(cat /run/secrets/db-password)" --silent']
       interval: 3s
       retries: 5
       start_period: 30s
@@ -37,8 +37,10 @@ services:
       - 80:80
     depends_on: 
       - backend
+
 volumes:
   db-data:
+
 secrets:
   db-password:
-    file: db/password.txt
+    file: db/password.txt