Jurgy
Jurgy

Reputation: 2360

Sqlite database in .NET ASP Core Docker container empty

I'm trying to wrap an .NET ASP Core web app in a Docker container, but the Sqlite database appears to be empty in the container.

In the Controller I execute the following query:

using (var connection = new SQLiteConnection(_connectionString))
{
    connection.Open();
    using (var cmd = new SQLiteCommand("SELECT COUNT(*) FROM sqlite_master WHERE type='table';", connection))
    {
        result = (Int64)cmd.ExecuteScalar();
    }
}

Where _connectionString is defined as follows:

private static readonly string _database = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "LocalDb\\v1\\sqlitedb.db");

The Dockerfile:

FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app

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

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "MyApp.dll"]

I build with docker build -t MyApp:dev . and run with docker run -p 8080:80 -e "ASPNETCORE_ENVIRONMENT=Development" MyApp:dev

When I run the app in Visual Studio with IIS I get the expected number of tables however in docker it returns 0. Also, when I query a table I get an SQLiteException: SQL logic error no such table error.

When I SSH into the container I can see that the database file is where I expect it: in app/LocalDb/v1/

Any clue what the problem might be?

Upvotes: 3

Views: 5089

Answers (1)

Jurgy
Jurgy

Reputation: 2360

Found the problem. The container was Linux based and didn't recognize the file path of the .db file with back slashes. For some reason it created a new file called LocalDb\v1\sqlitedb.db (Not directories, an actual file with that name). Changing the path to forward slashes solved it.

Upvotes: 3

Related Questions