Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #67 Hot-reload the code in the containers #69

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading