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"]

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:

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

  1. Write the run stage on the .NET runtime image and add Python to it with apk add.
  2. Make the service take the port and the core path from environment variables so the image can configure it.
  3. Explain why the restore is a separate layer from the publish.
  4. 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.

  1. How large was the published folder of the CLI?

    Answer

    144 KiB

    The measured du output is on the page.

  2. 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.

  3. What does this program print? Console.WriteLine(144);

    Answer

    144

    The published folder is 144 KiB, which the page quotes.

  4. 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.

  5. What does this program print? Console.WriteLine(144 * 1024 < 1024 * 1024);

    Answer

    True

    A 144 KiB folder is smaller than a megabyte.

  6. 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.

  7. 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.

  8. What does this program print? Console.WriteLine(8080);

    Answer

    8080

    The service of the image listens on 8080.

  9. Which parts run in the image?

    Answer

    The Python service and the published core

    The page says tests and clients stay out.

  10. 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

Banner image