C
C#4d ago
Cydo

✅ Can't update database in docker.

I'm very new to docker, but I've setup 2 Docker files one for my api and one for my client, and i can run them an access both but my database is always empty, and I can't run any ef commands to update or apply migrations and I have no idea why, it keeps saying I don't have the SDK but I'm almost positive in my Dockerfile I'm bringing in the SDK.
$ docker exec -it ripple-api dotnet ef migrations add InitialMigration
The command could not be loaded, possibly because:
* You intended to execute a .NET application:
The application 'ef' does not exist.
* You intended to execute a .NET SDK command:
No .NET SDKs were found.
$ docker exec -it ripple-api dotnet ef migrations add InitialMigration
The command could not be loaded, possibly because:
* You intended to execute a .NET application:
The application 'ef' does not exist.
* You intended to execute a .NET SDK command:
No .NET SDKs were found.
But from my understanding I have the docker file bringing in the SDK, so I don't understand how i cant run the commands...
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base

WORKDIR /app

EXPOSE 3000
EXPOSE 3001

FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Development
WORKDIR /src

COPY . .

RUN dotnet restore "Ripple.csproj"

COPY . .

WORKDIR "/src"

RUN dotnet build "Ripple.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Development

RUN dotnet publish "Ripple.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app

COPY --from=publish /app/publish .

COPY ./app.db ./

ENTRYPOINT ["dotnet", "Ripple.dll"]
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base

WORKDIR /app

EXPOSE 3000
EXPOSE 3001

FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Development
WORKDIR /src

COPY . .

RUN dotnet restore "Ripple.csproj"

COPY . .

WORKDIR "/src"

RUN dotnet build "Ripple.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Development

RUN dotnet publish "Ripple.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app

COPY --from=publish /app/publish .

COPY ./app.db ./

ENTRYPOINT ["dotnet", "Ripple.dll"]
9 Replies
Cydo
CydoOP4d ago
services:
ripple-api:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_HTTP_PORTS=3000
- ASPNETCORE_HTTPS_PORTS=3001
- ASPNETCORE_Kestrel__Certificates__Default__Password=(hidden)
- ASPNETCORE_Kestrel__Certificates__Default__Path=(hidden)
- ConnectionStrings__DefaultConnection="Data Source=/app/app.db"
container_name: ripple-api
image: ripple-api
build:
context: ripple-api/Ripple
dockerfile: Dockerfile
ports:
- "3000:3000"
- "3001:3001"
volumes:
- ~/.aspnet/https:/https:ro
- ./ripple-api/Ripple/app.db:/app/app.db

ripple-client:
build:
context: ripple-client
dockerfile: Dockerfile
image: ripple-client
container_name: ripple-client
ports:
- "5000:5000"
depends_on:
- ripple-api
services:
ripple-api:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_HTTP_PORTS=3000
- ASPNETCORE_HTTPS_PORTS=3001
- ASPNETCORE_Kestrel__Certificates__Default__Password=(hidden)
- ASPNETCORE_Kestrel__Certificates__Default__Path=(hidden)
- ConnectionStrings__DefaultConnection="Data Source=/app/app.db"
container_name: ripple-api
image: ripple-api
build:
context: ripple-api/Ripple
dockerfile: Dockerfile
ports:
- "3000:3000"
- "3001:3001"
volumes:
- ~/.aspnet/https:/https:ro
- ./ripple-api/Ripple/app.db:/app/app.db

ripple-client:
build:
context: ripple-client
dockerfile: Dockerfile
image: ripple-client
container_name: ripple-client
ports:
- "5000:5000"
depends_on:
- ripple-api
Jimmacle
Jimmacle4d ago
you're only bringing the sdk into the build step, you don't have it in the final image and there's no reason to creating migrations is done at dev time and they're compiled into the application you can apply them through code when your app starts as well with context.Database.MigrateAsync()
Jimmacle
Jimmacle4d ago
Applying Migrations - EF Core
Strategies for applying schema migrations to production and development databases using Entity Framework Core
Jimmacle
Jimmacle4d ago
tl;dr you shouldn't be trying to run these commands in the container in the first place
Cydo
CydoOP4d ago
Maybe I’m thinking about how docker is used wrong then? I’m running this in development right now so I figured I had to run the commands in the container in order for the SQLite database to get updated while I’m developing. Do people only dockerize everything once they are going to deploy to their server?
Jimmacle
Jimmacle4d ago
i personally don't develop in containers, but you can but that's not how you've structured your image yours is just set up to build an image that runs the application
Cydo
CydoOP4d ago
Is there a reason why? Normally i just do everything locally no docker. When i want to deploy I ssh into my server and manually drag the files where they need to go, install everything setup nginx etc, but i though Docker would make everything in my life easier but I have been on the struggle bus lol
Jimmacle
Jimmacle4d ago
same reason you deploy applications with docker, to have a separate and consistent environment for a particular purpose
Cydo
CydoOP4d ago
gotcha, well ill look into seeing how to setup a docker container for development, if not ill go back to my old way and just use docker when im ready to deploy. Thanks for the help 😄

Did you find this page helpful?