Skip to content

Commit 3743b46

Browse files
committed
dockerfile
1 parent 7de628f commit 3743b46

File tree

5 files changed

+60
-17
lines changed

5 files changed

+60
-17
lines changed

.env

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
PROJECT_NAME=entity-framework-example
22

3+
# DotNet
4+
DOTNET_TAG=7.0
5+
DOTNET_PORT=5001
6+
37
# Postgres
48
POSTGRES_TAG=15.1
59
DB_NAME=$PROJECT_NAME
@@ -16,3 +20,6 @@ ADMINER_PORT=8080
1620
# SQLite Browser
1721
SQLITE_BROWSER_TAG=3.12.2
1822
SQLITE_BROWSER_PORT=3000
23+
24+
# Database
25+
DB=postgres

EntityFrameworkExample/Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
ARG DOTNET_TAG
2+
ARG DB
3+
4+
FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_TAG} AS build-env
5+
WORKDIR /App
6+
7+
# Copy everything
8+
COPY . ./
9+
# Restore as distinct layers
10+
RUN dotnet restore
11+
# Build and publish a release
12+
RUN dotnet publish -c Release -o out
13+
14+
# Build runtime image
15+
FROM mcr.microsoft.com/dotnet/aspnet:${DOTNET_TAG}
16+
WORKDIR /App
17+
COPY --from=build-env /App/out .
18+
19+
#EXPOSE 5001
20+
EXPOSE 80
21+
ENTRYPOINT ["dotnet", "EntityFrameworkExample.dll"]

EntityFrameworkExample/Program.cs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,22 @@
2525
);
2626
}
2727

28-
2928
var app = builder.Build();
3029

31-
if(app.Environment.IsDevelopment())
30+
app.UseSwagger();
31+
32+
app.UseSwaggerUI(options =>
33+
{
34+
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
35+
options.RoutePrefix = string.Empty;
36+
});
37+
38+
using(var scope = app.Services.CreateScope())
3239
{
33-
app.UseSwagger();
34-
35-
app.UseSwaggerUI(options =>
36-
{
37-
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
38-
options.RoutePrefix = string.Empty;
39-
});
40-
41-
using(var scope = app.Services.CreateScope())
42-
{
43-
var entityContext = scope.ServiceProvider.GetRequiredService<EntityContext>();
44-
entityContext.Database.EnsureDeleted();
45-
entityContext.Database.EnsureCreated();
46-
entityContext.Seed();
47-
}
40+
var entityContext = scope.ServiceProvider.GetRequiredService<EntityContext>();
41+
entityContext.Database.EnsureDeleted();
42+
entityContext.Database.EnsureCreated();
43+
entityContext.Seed();
4844
}
4945

5046
// Configure the HTTP request pipeline.

EntityFrameworkExample/appsettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@
55
"Microsoft.AspNetCore": "Warning"
66
}
77
},
8+
"ConnectionStrings": {
9+
"postgres": "Server=postgres;Database=entity-framework-example;User Id=user;Password=pass",
10+
"sqlite": "Data Source=entity.db"
11+
},
812
"AllowedHosts": "*"
913
}

docker-compose.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@ version: "3.7"
22

33
services:
44

5+
# app:
6+
# image: mcr.microsoft.com/dotnet/sdk:${DOTNET_TAG}
7+
# hostname: app
8+
# stop_grace_period: 60s
9+
# restart: always
10+
# environment:
11+
# DB: $DB
12+
# build:
13+
# context: ./EntityFrameworkExample
14+
# args:
15+
# DB: $DB
16+
# DOTNET_TAG: $DOTNET_TAG
17+
# ports:
18+
# - ${DOTNET_PORT}:80
19+
520
postgres:
621
image: "postgres:${POSTGRES_TAG}"
722
container_name: "${PROJECT_NAME}-postgres"

0 commit comments

Comments
 (0)