Skip to content

Commit dc65024

Browse files
committed
Added something about Docker
1 parent 5119d37 commit dc65024

File tree

7 files changed

+121
-4
lines changed

7 files changed

+121
-4
lines changed

BCMS_Backend/Dockerfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
FROM mcr.microsoft.com/dotnet/aspnet:8.0-nanoserver-1809 AS base
77
WORKDIR /app
8-
EXPOSE 8080
9-
EXPOSE 8081
8+
EXPOSE 5195
9+
1010

1111
FROM mcr.microsoft.com/dotnet/sdk:8.0-nanoserver-1809 AS build
1212
ARG BUILD_CONFIGURATION=Release
1313
WORKDIR /src
1414
COPY ["BCMS_Backend/BCMS_Backend.csproj", "BCMS_Backend/"]
15-
RUN dotnet restore "./BCMS_Backend/./BCMS_Backend.csproj"
15+
RUN dotnet restore "./BCMS_Backend/BCMS_Backend.csproj"
1616
COPY . .
1717
WORKDIR "/src/BCMS_Backend"
1818
RUN dotnet build "./BCMS_Backend.csproj" -c %BUILD_CONFIGURATION% -o /app/build

BCMS_FrontendBlazor/Dockerfile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
2+
WORKDIR /src
3+
ENV DOTNET_NUGET_SIGNATURE_VERIFICATION=false
4+
COPY ["BCMS_FrontendBlazor/BCMS_FrontendBlazor.csproj", "BCMS_FrontendBlazor/"]
5+
RUN dotnet restore "BCMS_FrontendBlazor/BCMS_FrontendBlazor.csproj"
6+
COPY . .
7+
WORKDIR "/src/BCMS_FrontendBlazor"
8+
RUN dotnet build "BCMS_FrontendBlazor.csproj" -c Release -o /app/build
9+
FROM build AS publish
10+
RUN dotnet publish "BCMS_FrontendBlazor.csproj" -c Release -o /app/publish /p:UseAppHost=false
11+
FROM nginx:alpine AS final
12+
WORKDIR /usr/share/nginx/html
13+
RUN apk add jq
14+
RUN apk add nano
15+
COPY ["BCMS_FrontendBlazor/script.sh", "/docker-entrypoint.d/35-script.sh"]
16+
RUN chmod +x /docker-entrypoint.d/35-script.sh
17+
COPY --from=publish /app/publish/wwwroot .
18+
EXPOSE 7164
19+
CMD /docker-entrypoint.d/35-script.sh && nginx -g "daemon off;"

BCMS_FrontendBlazor/script.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
# vim:sw=2:ts=2:sts=2:et
3+
jq --arg aVar "$(printenv API_BASE_URL)" '.ApiBaseUrl = $aVar' /usr/share/nginx/html/appsettings.json > /usr/share/nginx/html/appsettings.json.tmp && mv /usr/share/nginx/html/appsettings.json.tmp /usr/share/nginx/html/appsettings.json

BookCatalogManagementSystem.sln

+2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BCMS_FrontendBlazor", "BCMS
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{32BF5940-7596-4E55-A791-520584D78FD4}"
1111
ProjectSection(SolutionItems) = preProject
12+
docker-compose.yml = docker-compose.yml
1213
README.MD = README.MD
1314
screenshot.PNG = screenshot.PNG
1415
screenshot2.PNG = screenshot2.PNG
1516
screenshot3.PNG = screenshot3.PNG
17+
TREATISE ON DOCKER.MD = TREATISE ON DOCKER.MD
1618
EndProjectSection
1719
EndProject
1820
Global

README.MD

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The solution consists of backend and frontend projects. The backend exposes API
2424
- Backend: C#, ASP.NET Core Minimal API
2525
- Frontend: Blazor WebAssembly
2626
- Database: In-memory storage
27-
- ~~Containerization: Docker, Docker Compose~~ Something went wrong with Docker, but I add those builds
27+
- ~~Containerization: Docker, Docker Compose~~ ~~Something went wrong with Docker~~ I add those builds to Release section
2828

2929
### Run API
3030

@@ -65,6 +65,12 @@ Here `path/to/release` is where you unpacked BCMS_FrontendBlazor
6565

6666
Anyway, a project should be available at http://localhost:7164 .
6767

68+
## Experimental - run Docker
69+
70+
A friend (and ChatGPT) helped me to come with Docker implementation, it is just there
71+
Run docker-compose up command
72+
application may be available at http://localhost:7164/
73+
6874
## Pictures
6975

7076
![screenshot](screenshot.PNG?raw=true "screenshot")

TREATISE ON DOCKER.MD

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
To run your .NET Core project in Docker and make it accessible on port 5195, you need to create a Dockerfile for your application, build the Docker image, and run it while mapping the desired port. Here's how to do it step by step:
2+
3+
1. Create a Dockerfile
4+
Create a Dockerfile in the root of your project:
5+
6+
```
7+
# Use the official .NET runtime image as the base image
8+
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
9+
WORKDIR /app
10+
11+
# Copy the build output from your local machine
12+
COPY ./bin/Release/net8.0/publish ./
13+
14+
# Expose port 5195
15+
EXPOSE 5195
16+
17+
# Define the entry point
18+
ENTRYPOINT ["dotnet", "BCMS_backend.dll"]
19+
```
20+
2. Build the Project for Release
21+
Make sure to build your project in release mode and publish it:
22+
23+
```
24+
dotnet publish -c Release -o ./bin/Release/net8.0/publish
25+
```
26+
3. Build the Docker Image
27+
Build a Docker image using the Dockerfile:
28+
29+
```
30+
docker build -t bcms-backend .
31+
```
32+
4. Run the Docker Container
33+
Run the container while mapping port 5195 on the host to 5195 in the container:
34+
35+
```
36+
docker run -d -p 5195:5195 bcms-backend --urls=http://+:5195
37+
```
38+
Here’s what happens:
39+
40+
-d: Runs the container in detached mode.
41+
-p 5195:5195: Maps port 5195 on your local machine to port 5195 in the container.
42+
--urls=http://+:5195: Configures the application to listen on all network interfaces within the container on port 5195.
43+
5. Verify the Running Container
44+
Check if the container is running:
45+
46+
```
47+
docker ps
48+
```
49+
You should see your container running and exposing port 5195.
50+
51+
6. Access Your Application
52+
Open a browser or use a tool like curl to access the application:
53+
54+
```
55+
http://localhost:5195
56+
```
57+
If everything is set up correctly, your .NET Core project will now be running inside Docker and accessible via the specified port.

docker-compose.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
version: '3.4'
2+
3+
name: book-management-system
4+
services:
5+
apiserver:
6+
build:
7+
context: .
8+
dockerfile: ./BCMS_Backend/Dockerfile
9+
ports:
10+
- "5195:5195"
11+
networks:
12+
- app_network
13+
14+
blazorclient:
15+
build:
16+
context: .
17+
dockerfile: ./BCMS_FrontendBlazor/Dockerfile
18+
ports:
19+
- "7164:7164"
20+
networks:
21+
- app_network
22+
depends_on:
23+
- apiserver
24+
environment:
25+
- API_BASE_URL=http://localhost:7164
26+
27+
28+
networks:
29+
app_network:
30+
driver: bridge

0 commit comments

Comments
 (0)