Sqlite running inside a docker container - docker

I have a umbraco 10 web app running inside a docker container. I would like to attach the SQLite storage that I use to the container.
The file is located at
~/umbraco/Data/Umbraco.sqlite.db.
This is the docker file for building the image
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 4000
ENV ASPNETCORE_URLS http://+:4000
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["moranmono.web/moranmono.web.csproj", "moranmono.web/"]
RUN dotnet restore "moranmono.web/moranmono.web.csproj"
COPY . .
WORKDIR "/src/moranmono.web"
RUN dotnet build "moranmono.web.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "moranmono.web.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "moranmono.web.dll"]
This is the docker-compose file
version: '3.9'
services:
moranmono-umbraco:
container_name: moranmono-umbraco
build:
context: .
dockerfile: moranmono.web/Dockerfile
ports:
- 4000:4000
environment:
- ASPNETCORE_ENVIRONMENT=Development
volumes:
- ~/.vsdbg:/remote_debugger:rw
I tried running the docker container but it doesn't recognize the SQLite DB file and I am not sure where I am supposed to add the SQLite support, in the docker file or on the docker-compose file
I get the following error after running the container
Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 14: 'unable to open database file'.
2022-12-13T14:56:26.108469888Z at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
2022-12-13T14:56:26.108472054Z at Microsoft.Data.Sqlite.SqliteConnectionInternal..ctor(SqliteConnectionStringBuilder connectionOptions, SqliteConnectionPool pool)
2022-12-13T14:56:26.108474638Z at Microsoft.Data.Sqlite.SqliteConnectionPool.GetConnection()
2022-12-13T14:56:26.108477304Z at Microsoft.Data.Sqlite.SqliteConnectionFactory.GetConnection(SqliteConnection outerConnection)
2022-12-13T14:56:26.108479346Z at Microsoft.Data.Sqlite.SqliteConnection.Open()
2022-12-13T14:56:26.108481096Z at Umbraco.Extensions.DbConnectionExtensions.IsAvailable(IDbConnection connection)

You have to mount the sqlite file inside the docker image using volumes. Something like this will do the trick-
volumes:
- ./path/to/sqlite-folder:/app/db

Related

.Net Core 5 Web API Project not working with Docker Compose

I am trying to use docker-compose with my project.
Having issue with .Net 5 API project.
It works properly with ISS Express and Docker but when I run project with docker-compose api project is not working as expected.
As expeced when running docker-compose it should run API project and launch swagger api page instead it’s displaying different erros when exposed ports are accessed via browser. Please find attached snaps below.
Docker container image
browser run1
browser run2
browser run3
As you can see for each url it's displaying different error message in browser.
Following solutions to resolve issues but none worked.
1. Expose ports using below in docker file
ports:
- 51850:80
Docker expose ports
2. Try with debug environment
ARG BUILD_CONFIGURATION=Debug
ENV ASPNETCORE_ENVIRONMENT=Development
ENV DOTNET_USE_POLLING_FILE_WATCHER=true
ENV ASPNETCORE_URLS=http://+:80
stackoverlow suggestion - set environment to debug
3. Tried using VSCode docker-compose and Visual Studio docker-compose
Regenerated docker-compose file with help of Visual Studio and VS Code.
4. Tried setting up specific ports on Launchsettings.json
Tried settings specif ports with docker in launsettings.json
and exporting from docker, docker compose
Docker
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 44370
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["TAPI/TAPI.csproj", "TAPI/"]
RUN dotnet restore "TAPI/TAPI.csproj"
COPY . .
WORKDIR "/src/TAPI"
RUN dotnet build "TAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "TAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TAPI.dll"]
docker-compose
version: '3.4'
services:
todoworker:
image: tworker
build:
context: .
dockerfile: TWorker/Dockerfile
todoapi:
image: tapi
build:
context: .
dockerfile: TAPI/Dockerfile
ports:
- 80
- 443
- 44370
tangular:
image: tangular
build:
context: .
dockerfile: TAngular/Dockerfile
ports:
- 4200:4200
5. Tried with deleting all containers
Tried deleting all containers, images and recreating all after restarting docker desktop (using docker on windows system)
As suggested in following article Docker for clean build of an image
docker system prune
Docker desktop image
Even deleting images and containers from docker and recreating images some time it displayes image created time from past hours.
Date: 20-May-2021:
I have created another small Sample with two Web APIs. Tested on my local Laptop. Please get that to your local box and execute. Please let me know if you need any further assistance.
GitHub URL: https://github.com/vishipayyallore/mini-projects-2021/tree/master/Projects/CollegeDockerDemo
College.WebAPI Docker File
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["Source/College.WebAPI/College.WebAPI.csproj", "Source/College.WebAPI/"]
RUN dotnet restore "Source/College.WebAPI/College.WebAPI.csproj"
COPY . .
WORKDIR "/src/Source/College.WebAPI"
RUN dotnet build "College.WebAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "College.WebAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "College.WebAPI.dll"]
ToDo.WebAPI Docker File
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["Source/ToDo.WebAPI/ToDo.WebAPI.csproj", "Source/ToDo.WebAPI/"]
RUN dotnet restore "Source/ToDo.WebAPI/ToDo.WebAPI.csproj"
COPY . .
WORKDIR "/src/Source/ToDo.WebAPI"
RUN dotnet build "ToDo.WebAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ToDo.WebAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ToDo.WebAPI.dll"]
docker-compose.yml File
version: '3.4'
services:
college.webapi:
image: ${DOCKER_REGISTRY-}collegewebapi
build:
context: .
dockerfile: Source/College.WebAPI/Dockerfile
todo.webapi:
image: ${DOCKER_REGISTRY-}todowebapi
build:
context: .
dockerfile: Source/ToDo.WebAPI/Dockerfile
docker-compose.override.yml File
version: '3.4'
services:
college.webapi:
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "8000:80"
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
todo.webapi:
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "8001:80"
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro

Cannot assign requested address (localhost:xxxx) - Docker + Linux Containers

We have a WPF application that will call a Web API called API-1 which is running in a docker container. I also have another API called API-2 which is also running in a docker but a different container.
Whenever the API-1 call happened from the application, the API-1 will do some logic and try to do a post request to API-2.
Now, my problem is, the post request to API-2 always returns Cannot assign requested address (localhost:XXXX).
If I try without docker, it works fine.
Also, a separate request to each of the API works fine (using POSTMAN)
This problem occurs only if the API deployed in the docker. I was using docker-compose to create the containers. I have created a docker network bridge and allocated it to the respective APIs in the docker-compose.yml file.
Here is my docker-compose file as well as the docker file for both of the APIs.
Docker-Compose.yml
version: "3.7"
networks :
localdev:
name: localdev
external: true
services:
api-01:
build:
context: .
dockerfile: api-01/Dockerfile
restart: always
container_name: "api-01"
ports:
- "8082:80"
environment:
ASPNETCORE_ENVIRONMENT: "Development"
networks:
- localdev
api-02:
build:
context: .
dockerfile: api-02/Dockerfile
restart: always
container_name: "api-02"
ports:
- "8083:80"
environment:
ASPNETCORE_ENVIRONMENT: "Development"
networks:
- localdev
DockerFile(API-1)
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["api-01/api-01.csproj", "api-01/"]
COPY ["CommonEntities/CommonEntities.csproj", "CommonEntities/"]
RUN dotnet restore "api-01/api-01.csproj"
COPY . .
WORKDIR "/src/api-01"
RUN dotnet build "api-01.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "api-01.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "api-01.dll"]
DockerFile(API-2)
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["api-02/api-02.csproj", "api-02/"]
COPY ["CommonEntities/CommonEntities.csproj", "CommonEntities/"]
RUN dotnet restore "api-02/api-02.csproj"
COPY . .
WORKDIR "/src/api-02"
RUN dotnet build "api-02.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "api-02.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "api-02.dll"]
I'm not sure what I'm missing here!
I have tried most of the solutions from the internet, but nothing works.
It looks like I need to use {DOCKER_IP} instead of "localhost" if I want to have communication between two API in two different containers.
After I changed my post request to hit "http://{My_PC_IP_Address}:8082", the request was successful.
Thanks to David for redirecting me to the right documentation https://docs.docker.com/network/bridge/

Running .Net Core Application with docker container but i have an issue like this `0.0.0.0:7001->7001/tcp`

I try to containerize my .net core application but i have an issue on app container ports. It seems as 0.0.0.0:7001->7001/tcp. I think problem exists because of this. Is there any other problems on there?
My dockerfile and docker-compose file like below
Dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-nanoserver-1903 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:2.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["Blog.Web/Blog.Web.csproj", "Blog.Web/"]
COPY ["Blog.BLL/Blog.BLL.csproj", "Blog.BLL/"]
COPY ["Blog.DAL/Blog.DAL.csproj", "Blog.DAL/"]
COPY ["Blog.Models/Blog.Models.csproj", "Blog.Models/"]
RUN dotnet restore "Blog.Web/Blog.Web.csproj"
COPY . .
WORKDIR "/src/Blog.Web"
RUN dotnet build "Blog.Web.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Blog.Web.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Blog.Web.dll"]
Docker-compose.yaml
version: '3.4'
services:
client:
build:
dockerfile: Blog.Web\Dockerfile
context: .
ports:
- "7001:7001"
Bash Display
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5e3edcbc969 blogwebapplication_client "dotnet Blog.Web.dll" About a minute ago Up About a minute 0.0.0.0:7001->7001/tcp blogwebapplication_client_1
When I try to access my app on localhost:7001 I get an error:
"This site cant be reach".

Dockerize .NET solution with 2 executable projects

I was asked to create a .net solution with a web api and a console program, then have this available with a docker-compose command that includes the full environment. The web api is using mongodb and easynetq. The console program references the web api project and also uses easynetq. I know Visual Studio can provide me a dockerfile for each project. How do I proceed after this? Do I combine the 2 dockerfiles into 1 at the solution level? Or can I continue with the 2 dockerfiles?
Dockerfile for console program
FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app
COPY . ./
RUN dotnet restore "./ConsoleProgram.csproj"
RUN dotnet publish "ConsoleProgram.csproj" -c Release -o out
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "ConsoleProgram.dll"]
Dockerfile for webapi is identical as above, only names are changed
Docker compose at solution level
version: '3'
services:
mongo:
image: mongo
restart: always
ports:
- 27017:27017
webapi:
build: ./webapi
restart: always
ports:
- 5000:80
environment:
MongoDB__Host: mongo
depends_on:
- mongo
console:
build: ./console
restart: always
depends_on:
- webapi
After doing a docker compose on this I get the following errors:
Skipping project "C:\WebApi\WebApi.csproj" because it was not found.
The referenced project '..\WebApi\WebApi.csproj' does not exist.
Service 'console' failed to build: The command 'cmd /S /C dotnet
publish "Console.csproj" -c Release -o out' returned a non-zero code:
1
You are on the right way. You should have Dockerfile for each projects (in root of the project folder), then one file docker-compose.yaml and many docker-compose.override.yaml if you need.
There are some gaps in Dockerfile.
1.Don't copy COPY . ./ all files in ConsoleProgram image, first just copy csproj
COPY console/ConsoleProgram.csproj console/
2.After copy csproj file run dotnet restore to improve image building time
RUN dotnet restore console/ConsoleProgram.csproj
3.Then copy all other files and run dotnet publish (or build) with flag --no-restore
COPY console/ console/
RUN dotnet publish console/ConsoleProgram.csproj --no-restore -c Release -o /out
Here are some example how Dockerfile should look
FROM microsoft/dotnet:2.2-sdk AS publish
WORKDIR /src
COPY console/ConsoleProgram.csproj console/
RUN dotnet restore console/ConsoleProgram.csproj
COPY console/ console/
RUN dotnet publish console/ConsoleProgram.csproj --no-restore -c Release -o /app
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS runtime
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "ConsoleProgram.dll"]
Something similar should be for WebAPI. Now look at your docker-compose.yaml. You should specify build context and image name
webapi:
image: 'webapi'
build:
context: .
dockerfile: ./webapi/Dockerfile
restart: always
ports:
- 5000:80
environment:
MongoDB__Host: mongo
depends_on:
- mongo
console:
image: 'console'
build:
context: .
dockerfile: ./console/Dockerfile
restart: always
depends_on:
- webapi
As I doesn't know your project structure you need to be careful with all paths to project in this answer

Use a DockerFile to create an empty MSSQL Express database in a container

I am a little confused as to how all of this works as this is my first time playing with Docker.
I've downloaded the following docker image of Microsoft SQL Server Express (https://hub.docker.com/r/microsoft/mssql-server-windows-express/).
I am trying to docker-compose together some existing containers and every time I run docker-compose up, what I want to have happen is that a new database is created and a target container is run.
Here is the existing Dockerfile for the container that I want to have execute. "Execute this" is the container that I want to have dotnet run run on and any "HelperService" are services that should be up before the target container is run:
FROM microsoft/dotnet:2.0-runtime-nanoserver-1709 AS base
WORKDIR /app
FROM microsoft/dotnet:2.0-sdk-nanoserver-1709 AS build
WORKDIR /src
COPY ExecuteThis/ExecuteThis.csproj ExecuteThis/
COPY ../HelperService/HelperService.csproj ../HelperService/
COPY ../HelperService2/HelperService2.csproj ../HelperService2/
COPY ../HelperService3/HelperService3.csproj ../HelperService3/
COPY ../HelperService4/HelperService4.csproj ../HelperService4/
COPY ../HelperService5/HelperService5.csproj ../HelperService5/
COPY ../HelperService6/HelperService6.csproj ../HelperService6/
RUN dotnet restore ExecuteThis/ExecuteThis.csproj
COPY . .
WORKDIR /src/ExecuteThis
RUN dotnet build ExecuteThis.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish ExecutThis.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "ExecuteThis.dll"]
Here is a docker-compose.yml file:
version: '3.4'
services:
sqlserver:
image: microsoft/mssql-server-windows-express
helperservice:
image: helperservice
helperservice2:
image: helperservice2
helperservice3:
image: helperservice3
helperservice4:
image: helperservice4
helperservice5:
image: helperservice5
helperservice6:
image: helperservice6
executethis:
image: ${DOCKER_REGISTRY}executethis
depends_on:
- sqlserver
- helperservice
- helperservice2
- helperservice3
- helperservice4
- helperservice5
- helperservice6
build:
context: .
dockerfile: ExecuteThis\Dockerfile
My question is: how do I generate a database and where should I write those instructions? How do I dotnet run my "ExecuteThis" container?

Resources