Home > Software design >  Default worker process does not start in docker without debug mode
Default worker process does not start in docker without debug mode

Time:12-11

Maybe it is something obvious, but cannot figure it out.

I have just out-of-the-box C# Worker service solution in Visual Studio (2022, .NET 6). Docker support is on. When I press 'Debug' - it starts docker container and runs app. Everything looks fine on the console (by default it just write messages each 1 sec.)

Now if I leave VS and just open up CMD and type:

docker ps

I would see the image created during Visual Studio runtime. If I try to start it by:

docker run workerservicedocker:dev

I get quick output like:

enter image description here

So no code execution actually happens. Looks like app is getting opened/closed immediately. I'm just trying Docker approach, so maybe I'm missing something obvious. As I mentioned it is just default template solution, no modifications from my side. Thanks in advance for taking a look.

Dockerfile is:

FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["WorkerServiceDocker/WorkerServiceDocker.csproj", "WorkerServiceDocker/"]
RUN dotnet restore "WorkerServiceDocker/WorkerServiceDocker.csproj"
COPY . .
WORKDIR "/src/WorkerServiceDocker"
RUN dotnet build "WorkerServiceDocker.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WorkerServiceDocker.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WorkerServiceDocker.dll"]

This is default Dockerfile created by VS.

UPDATE:

If I do

docker run -it  workerservicedocker:dev

like it was suggested in the reply below - it just shows app> commandline, but no actual execution happens.

enter image description here

CodePudding user response:

docker run -it workerservicedocker:dev attaches stdin and a psuedo-tty

basically, the way you run it, it's in the "background". If you docker ps you'll see your container running. Grab the container id, docker logs <id> and you'll see your output.

With -it it effectively attaches the container to your console. When you run from VS it does this for you.


additional: The actual problem is that VS is being "clever" The ":dev" tag is just the base stage (basically nothing). VS then squirts in the code and builds. It does this for edit-continue, debugging etc. (check the output/container-tools window. See the "--target base" arg)

Right click the Dockerfile and select "Build Docker Image". This actually runs the entire dockerfile and gives you a ":latest" tag which will has what you expected.

docker run -it workerservicedocker:latest

  • Related