Deployment: the Dockerfile, not built here
The project has two halves, and a container is one way to ship them together: the C# core published as a folder, the Python service next to it, and a base image that has both runtimes. The C# side needs a publish step and the Python side needs nothing but the interpreter and the folder. A first version of this file was built on the host of this container and did not work; the file below is the fixed one, and the section on the three defects says what was wrong.
The publish
dotnet publish of the CLI project writes a folder that runs on a machine with the .NET runtime. Its size here, with the core dll next to it, is 144 KiB:
dotnet publish src/Cli/Cli.csproj -c Release -o publish -nologo -v:q
du -sh publish
144K publish
Cli
Cli.deps.json
Cli.dll
Cli.pdb
Cli.runtimeconfig.json
Core.dll
Core.pdb
The Dockerfile
Two stages: an SDK image that restores and publishes the core, and a Python image that copies the published folder and the Python files. The service starts the core with dotnet, so the Python stage needs the runtime and not only the interpreter:
# The C# core and the Python service in one image.
FROM mcr.microsoft.com/dotnet/sdk:10.0-alpine AS build
WORKDIR /src
COPY src/Core/Core.csproj src/Core/
COPY src/Cli/Cli.csproj src/Cli/
RUN dotnet restore src/Cli/Cli.csproj
COPY src/ src/
RUN dotnet publish src/Cli/Cli.csproj -c Release -o /app/core
FROM mcr.microsoft.com/dotnet/runtime:10.0-alpine
RUN apk add --no-cache python3 icu-libs
WORKDIR /app
COPY --from=build /app/core ./core
COPY python/ ./python/
ENV CORE_CLI=/app/core/Cli.dll
ENV BIND_HOST=0.0.0.0
ENV BIND_PORT=8080
EXPOSE 8080
CMD ["python3", "python/service.py"]
- The restore of the first stage copies only the project files, so a change to a source file does not invalidate the restore layer. That is the one trick of a .NET Dockerfile worth knowing.
- The run stage is mcr.microsoft.com/dotnet/runtime:10.0-alpine with python3 added, and not the Python image: the publish is framework-dependent, so the image must already have the .NET runtime the folder needs, and adding one interpreter is smaller than adding a whole runtime to a Python image. The other choice, a self-contained publish on the Python image, carries the runtime in the folder and costs tens of megabytes more.
- icu-libs is in the run stage because the runtime needs ICU for cultures; the core formats with the invariant culture, but the service and the runtime still load it.
- CORE_CLI points at the copied program and BIND_HOST=0.0.0.0 is what makes docker run -p reach the service: a server bound to 127.0.0.1 inside the container is not published.
- The Python client and the tests are not in the image: a container runs the service, and the tests run where the code is built.
What the first version got wrong
The first Dockerfile of this page ended with FROM python:3.14-alpine and copied the same framework-dependent folder. It was built on the host of this container, and it built successfully, and the image did not work. Three defects, in the order a reader would meet them:
- The Python image has no .NET runtime. command -v dotnet inside it prints nothing, so the service cannot start the CLI at all, and the container exits when the first request arrives.
- The service bound 127.0.0.1, because that was the default of the page. The log said listening on http://127.0.0.1:8080 and docker run -p 8080:8080 could not reach it: a request from the host failed with curl exit status 56, which is a connection reset. The server was healthy and unreachable, which is the worst kind of failure to read.
- The service looked for the CLI at src/Cli/bin/Release/net10.0/Cli.dll, the path of the checkout, while the image has it at /app/core/Cli.dll. That is why the path is an environment variable now, with the checkout path as its default.
None of the three was a Dockerfile syntax error, which is the useful lesson: a build that succeeds says nothing about an image that works.
Not built here
This image was not built in this container. There is no Docker daemon inside the container, so docker build has nowhere to run; the Dockerfile is shown as the source it is, and the page states no output of a build that did not happen here. The first version of the file was built on the host of this container, and the section above is what that build found. The fixed image is rebuilt on the host with:
docker build -t csharp-core-python .
docker run --rm -p 8080:8080 csharp-core-python
The publish of the previous section did happen here, and its size is the number on the page; the image size would be the base image plus that folder plus the Python interpreter, which is a number this page does not invent. The Docker section of this site covers the image itself.
This is the last page of the group. The project it describes is small, and the three callers of the timing page are the three shapes a modern project chooses between: a process, a protocol and an HTTP API. The core did not change between them, which is the reason to write it first.
Exercises
- Write the run stage on the .NET runtime image and add Python to it with apk add.
- Make the service take the port and the core path from environment variables so the image can configure it.
- Explain why the restore is a separate layer from the publish.
- List what a test container would need that the service image does not.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How large was the published folder of the CLI?
Answer
144 KiB
The measured du output is on the page.
-
Why are there two stages in the Dockerfile?
Answer
The SDK image builds and the smaller runtime and Python image runs
The build tools do not belong in the run image.
-
What does this program print?
Console.WriteLine(144);Answer
144
The published folder is 144 KiB, which the page quotes.
-
Why is only the project file copied before the restore?
Answer
So a source change does not invalidate the restore layer
The page names the caching trick.
-
What does this program print?
Console.WriteLine(144 * 1024 < 1024 * 1024);Answer
True
A 144 KiB folder is smaller than a megabyte.
-
Why is the Dockerfile not built here?
Answer
There is no Docker daemon inside the container
The page says so and shows no invented output.
-
Why is icu-libs in the run stage?
Answer
The core formats decimals and the runtime needs ICU for cultures
The smaller alternative is the invariant globalization mode.
-
What does this program print?
Console.WriteLine(8080);Answer
8080
The service of the image listens on 8080.
-
Which parts run in the image?
Answer
The Python service and the published core
The page says tests and clients stay out.
-
What is the reason to write the core first?
Answer
It does not change between the three callers
The closing paragraph of the page says so.
The C# code on this page was built with the .NET SDK 10.0.303 and the Python code was run with Python 3.14.10, on Alpine Linux 3.24.3 (x86_64, four processors) in a Docker container.
Article author: Arthur Isaev