Skip to content

Commit

Permalink
Merge pull request #69 from ksummarized/00067_docker_code_hot_reload
Browse files Browse the repository at this point in the history
Closes #67 Hot-reload the code in the containers
  • Loading branch information
Sojusan authored Sep 23, 2024
2 parents 18da5bc + 38bf34c commit a728d40
Show file tree
Hide file tree
Showing 7 changed files with 12,360 additions and 12,304 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,36 @@ dotnet dev-certs https -ep .aspnet/https/aspnetapp.pfx -p devcertpasswd --trust
```

Next go to the `scripts` directory and run `apply_migrations.ps1`
Next You should go back to the main directory and run `docker compose up --build`
Next You should go back to the main directory and run `docker compose --profile hot-reload up --build --watch`
This can be done with the following snippet.

```powershell
cd scripts
.\apply_migrations.ps1
cd ..\
docker compose up --build
docker compose --profile hot-reload up --build --watch
```

You can now visit the site at: <http://localhost:8888/>

## Scripts

Directory `scripts` contains some helpful scripts which automate some parts of working with this directory.

## Development

The application is started using the following command:

```bash
docker compose --profile hot-reload up --build --watch
```

or without the hot-reload feature:

```bash
docker compose --profile without-hot-reload up --build --watch
```

The `--watch` parameter starts containers with the `hot-reload` feature. This enables the auto-reload functionality, meaning that the container will be automatically reloaded when the code for either the frontend or backend changes.

> The `hot-reload` feature for backend applications uses `dotnet watch`, which only detects changes to existing files. It will not restart the container if new files are added (dotnet watch [issue](https://github.com/dotnet/aspnetcore/issues/8321)).
5 changes: 5 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS development
COPY . /src
WORKDIR /src
CMD ["dotnet", "watch", "--verbose", "--non-interactive", "--no-launch-profile", "--project", "src/api/"]

#Todo: clean it up for multiproject build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
Expand Down
42 changes: 38 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
version: "3.9"
services:
frontend:
frontend: &frontend-base
container_name: ks-frontend
profiles:
- without-hot-reload
build:
context: ./frontend
ports:
- "8888:8888"

backend:
frontend-hot-reload:
<<: *frontend-base
container_name: ks-frontend-hot-reload
profiles:
- hot-reload
command: ["npm", "run", "dev"]
develop:
watch:
- action: sync
path: ./frontend
target: /src
ignore:
- node_modules
- action: rebuild
path: package.json

backend: &backend-base
container_name: ks-backend
build:
context: ./backend
profiles:
- without-hot-reload
ports:
- "5001:80"
- "5000:443"
Expand All @@ -20,12 +39,27 @@ services:
ASPNETCORE_Kestrel__Certificates__Default__Path: "/https/aspnetapp.pfx"
ASPNETCORE_Kestrel__Certificates__Default__Password: "devcertpasswd"
ASPNETCORE_URLS: "https://+:443;http://+:80"
DOTNET_USE_POLLING_FILE_WATCHER: true # dotnet watch uses a a polling file watcher. Required for running it in docker.
volumes:
- .aspnet/https:/https/
- .aspnet/https:/https/:ro # read-only on the container
depends_on:
db:
condition: service_healthy

backend-hot-reload:
<<: *backend-base
container_name: ks-backend-hot-reload
build:
context: ./backend
target: development
profiles:
- hot-reload
develop:
watch:
- action: sync
path: ./backend
target: /src

db:
image: postgres:14.0-alpine
container_name: ks-database
Expand Down
2 changes: 1 addition & 1 deletion frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM node:current-alpine
WORKDIR /src
COPY package.json .
COPY package*.json .
RUN npm install
COPY . .
RUN npm run build
Expand Down
Loading

0 comments on commit a728d40

Please sign in to comment.