ASP.NET Core Container SQLLite database is empty after DockerBuild

I am having issues debugging why my docker build is giving me a sqlite database file that is empty.

When I Docker exec -it appName bash into the container and ls -la the contents of the container at run time the appName.db file is 0 bytes in size.

Let me start with some context: I am doing this in Jenkins and my Jenkinsfile looks like this

pipeline {
    agent any
    stages {
        stage ('Build') {
            steps { //build section
                echo 'entering build stage'
                sh 'docker build -t bakeryapp .'
                echo 'finished build stage'
            }
        }

        stage ('Test') {
            steps { //test section
                echo 'no tests at this moment'
            }
        }
        stage ('Run') {
            steps { //run section 
                echo "starting run stage"
                sh 'docker run -d -p 8080:80 --name runningbakeryApp bakeryapp'
                echo 'end run app'
                echo 'be sure to use "docker ps  -a to kill the running app"'
            }
        }
        stage ('Tear Down'){
            steps {
                input message: 'Finished with the website?  (Click "Proceed" to continue)'
                sh 'docker container stop runningbakeryApp'
                sh 'docker container rm runningbakeryApp'
            }
        }
    }
}

The meat and potatoes of the build is in the : Dockerfile

# pull in the SDK to do the dirty work
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 as build-env
WORKDIR /Bakery

# Copy csproj and restore as distinct layers
COPY /Bakery/*.csproj ./
RUN dotnet restore

# Copy everything Else and build
COPY ./Bakery ./
RUN export PATH="$PATH:/root/.dotnet/tools"; dotnet tool install -g dotnet-ef --version 3.0.0; dotnet ef migrations add CreateDatabase --output-dir Data/Migrations; dotnet ef database update; dotnet publish -c D -o out
CMD /bin/bash  dotnet ef database update

# Buid runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0
WORKDIR /Bakery
COPY --from=build-env /Bakery/out .
ENTRYPOINT ["dotnet", "Bakery.dll"]

So when I run the app in VScode localhost. The app works fine and I can see the items in my Database.

When I run the container after the build I get this in the stack dump:

SqliteException: SQLite Error 1: 'no such table: Products'.
Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(int rc, sqlite3 db)
Microsoft.Data.Sqlite.SqliteCommand.PrepareAndEnumerateStatements(Stopwatch timer)+MoveNext()
Microsoft.Data.Sqlite.SqliteCommand.GetStatements(Stopwatch timer)+MoveNext()
Microsoft.Data.Sqlite.SqliteDataReader.NextResult()
Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
Microsoft.Data.Sqlite.SqliteCommand.ExecuteReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable<T>+AsyncEnumerator.InitializeReaderAsync(DbContext _, bool result, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable<T>+AsyncEnumerator.MoveNextAsync()
Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync<TSource>(IQueryable<TSource> source, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync<TSource>(IQueryable<TSource> source, CancellationToken cancellationToken)
Bakery.Pages.IndexModel.OnGetAsync() in Index.cshtml.cs
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory+NonGenericTaskHandlerMethod.Execute(object receiver, object[] arguments)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeHandlerMethodAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeNextPageFilterAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

I know its a lot of text

But I believe the problem stems from something in the container layers with these commands:

dotnet ef migrations add CreateDatabase --output-dir Data/Migrations
dotnet ef database update  <-- This is the command that looks like is not persisting between layers.
dotnet publish -c D -o out

Lastly I will dump the build output from Jenkins:

+ docker build -t bakeryapp .

Sending build context to Docker daemon  9.905MB


Step 1/11 : FROM mcr.microsoft.com/dotnet/core/sdk:3.0 as build-env

 ---> a345b68a725e

Step 2/11 : WORKDIR /Bakery

 ---> Using cache

 ---> a85f6b5a634f

Step 3/11 : COPY /Bakery/*.csproj ./

 ---> Using cache

 ---> f2c1253db9be

Step 4/11 : RUN dotnet restore

 ---> Using cache

 ---> 33c754c4aceb

Step 5/11 : COPY ./Bakery ./

 ---> Using cache

 ---> 25b1baf25016

Step 6/11 : RUN export PATH="$PATH:/root/.dotnet/tools"; dotnet tool install -g dotnet-ef --version 3.0.0; dotnet ef migrations add CreateDatabase --output-dir Data/Migrations; dotnet ef database update; dotnet publish -c D -o out

 ---> Using cache

 ---> 8108b6b5d2d2

Step 7/11 : CMD /bin/bash  dotnet ef database update

 ---> Running in b1c8a3d15482

Removing intermediate container b1c8a3d15482

 ---> 30cc8da711ac

Step 8/11 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.0

 ---> 880d85db3775

Step 9/11 : WORKDIR /Bakery

 ---> Using cache

 ---> 7d4af6e159e8

Step 10/11 : COPY --from=build-env /Bakery/out .

 ---> Using cache

 ---> fdb298f42c52

Step 11/11 : ENTRYPOINT ["dotnet", "Bakery.dll"]

 ---> Using cache

 ---> 388e5cfa37f1

Successfully built 388e5cfa37f1

Successfully tagged bakeryapp:latest

Thanks for the help!

Source: StackOverflow