rabejens
rabejens

Reputation: 8122

How to containerize a .net core app that has dependencies in the same solution?

I am trying to containerize a simple .net core console application that depends on a .net Standard library.

My solution layout is like this:

+
|- Solution.sln
|- Library
|  +- Files in Library project
+- App
   |- Files in App
   +- Dockerfile

App depends on Library.

So far I have something like this:

FROM microsoft/dotnet:2.1-sdk
WORKDIR /app

# copy fsproj and restore as distinct layers
COPY *.fsproj ./
RUN dotnet restore

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

But this, obviously, only containerizes App.

How do I achieve putting Library in, too, without having to put the Dockerfile into the root besides the solution file?

Upvotes: 1

Views: 956

Answers (1)

maxm
maxm

Reputation: 3667

You can change the docker build context to the parent directory.

Change your dockerfile to this:

FROM microsoft/dotnet:2.1-sdk
WORKDIR /app

# copy fsproj and restore as distinct layers
COPY App/*.fsproj ./
RUN dotnet restore

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

And then run (from the folder containing Add and Library)

docker build -f App/Dockerfile .

But yes, it's usually more common to have your dockerfile located where you want your build context to be. So at the location that has all the files you'll need.

On docker for Mac/Windows the docker daemon will copy every file from the build context into the VM. So there is a performance hit here. Something to consider when you're structuring your projects.

Upvotes: 2

Related Questions