Skip to content

Commit 9f4f9d8

Browse files
authored
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 <[email protected]>
1 parent 00c7d85 commit 9f4f9d8

File tree

4 files changed

+101
-24
lines changed

4 files changed

+101
-24
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
services:
2+
backend:
3+
build:
4+
context: backend
5+
target: dev-envs
6+
restart: always
7+
secrets:
8+
- db-password
9+
depends_on: ['db']
10+
environment:
11+
- ASPNETCORE_URLS=http://+:8000
12+
volumes:
13+
- /var/run/docker.sock:/var/run/docker.sock
14+
15+
db:
16+
image: mariadb:10-focal
17+
command: '--default-authentication-plugin=mysql_native_password'
18+
restart: always
19+
secrets:
20+
- db-password
21+
volumes:
22+
- db-data:/var/lib/mysql
23+
environment:
24+
- MYSQL_DATABASE=example
25+
- MYSQL_ROOT_PASSWORD_FILE=/run/secrets/db-password
26+
27+
proxy:
28+
build: proxy
29+
ports:
30+
- 80:80
31+
depends_on:
32+
- backend
33+
34+
volumes:
35+
db-data:
36+
secrets:
37+
db-password:
38+
file: db/password.txt

nginx-aspnet-mysql/README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ Project structure:
2121
```
2222
services:
2323
backend:
24-
build: backend
24+
build:
25+
context: backend
2526
...
2627
db:
2728
# We use a mariadb image which supports both amd64 & arm64 architecture
28-
image: mariadb:10.6.4-focal
29+
image: mariadb:10-focal
2930
# If you really want to use MySQL, uncomment the following line
30-
#image: mysql:8.0.27
31+
#image: mysql:8
3132
...
3233
proxy:
3334
build: proxy
@@ -74,3 +75,14 @@ Stop and remove the containers
7475
```
7576
$ docker compose down
7677
```
78+
79+
## Use with Docker Development Environments
80+
81+
You can use this sample with the Dev Environments feature of Docker Desktop.
82+
83+
![Screenshot of creating a Dev Environment in Docker Desktop](../dev-envs.png)
84+
85+
To develop directly on the services inside containers, use the HTTPS Git url of the sample:
86+
```
87+
https://github.com/docker/awesome-compose/tree/master/nginx-aspnet-mysql
88+
```
Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
1+
# syntax=docker/dockerfile:1.4
12

2-
FROM mcr.microsoft.com/dotnet/aspnet:6.0 as base
3-
WORKDIR /app
3+
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:6.0 AS base
44

5-
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
6-
COPY . /src
75
WORKDIR /src
8-
RUN ls
9-
RUN dotnet build "aspnetapp.csproj" -c Release -o /app/build
106

11-
FROM build AS publish
12-
RUN dotnet publish "aspnetapp.csproj" -c Release -o /app/publish
7+
COPY aspnetapp.csproj ./
8+
RUN ["dotnet", "restore"]
9+
10+
FROM base as builder
11+
12+
COPY . .
13+
14+
CMD ["dotnet", "build", "-c", "-o", "/build"]
15+
16+
FROM builder as dev-envs
17+
18+
RUN <<EOF
19+
apt-get update
20+
apt-get install -y git
21+
EOF
22+
23+
RUN <<EOF
24+
useradd -s /bin/bash -m vscode
25+
groupadd docker
26+
usermod -aG docker vscode
27+
EOF
28+
# install Docker tools (cli, buildx, compose)
29+
COPY --from=gloursdocker/docker / /
30+
31+
CMD ["dotnet", "run"]
32+
33+
FROM builder AS publisher
34+
35+
RUN ["dotnet", "publish", "-c", "Release", "-o", "/build"]
36+
37+
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/aspnet:6.0
1338

14-
FROM base AS final
1539
WORKDIR /app
16-
COPY --from=publish /app/publish .
17-
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
40+
COPY --from=publisher /build .
41+
42+
CMD ["dotnet", "aspnetapp.dll"]

nginx-aspnet-mysql/compose.yaml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
services:
22
backend:
3-
build: backend
3+
build:
4+
context: backend
45
restart: always
56
secrets:
67
- db-password
7-
depends_on:
8-
- db
8+
depends_on:
9+
db:
10+
condition: service_healthy
911
environment:
1012
- ASPNETCORE_URLS=http://+:8000
11-
# depends_on:
12-
# db:
13-
# condition: service_healthy
13+
1414
db:
1515
# We use a mariadb image which supports both amd64 & arm64 architecture
16-
image: mariadb:10.6.4-focal
16+
image: mariadb:10-focal
1717
# If you really want to use MySQL, uncomment the following line
18-
#image: mysql:8.0.27
18+
#image: mysql:8
1919
command: '--default-authentication-plugin=mysql_native_password'
2020
restart: always
2121
healthcheck:
22-
test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"]
22+
test: ['CMD-SHELL', 'mysqladmin ping -h 127.0.0.1 --password="$$(cat /run/secrets/db-password)" --silent']
2323
interval: 3s
2424
retries: 5
2525
start_period: 30s
@@ -37,8 +37,10 @@ services:
3737
- 80:80
3838
depends_on:
3939
- backend
40+
4041
volumes:
4142
db-data:
43+
4244
secrets:
4345
db-password:
44-
file: db/password.txt
46+
file: db/password.txt

0 commit comments

Comments
 (0)