0

I need to publish self-contained .NET application in docker, and save it on my PC. I tried to save it using -v parameter like docker run --rm -v myHostFolderPath:/app/artifact myimage:latest, but it doesn't work. I run container via /bin/sh. My published project exists! In artifact folder. But no files were saved on my PC after running container.

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["folder1/folder2/MyProject.csproj", "MyProject/"]
COPY ["folder3/folder4/MyLib.csproj", "MyLib/"]
WORKDIR "MyProject/MyProject.Api/"
CMD dotnet build "MyProject.csproj" -c Release -o /app/build


FROM build AS publish
CMD dotnet publish "MyProject.csproj" -o artifacts
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445

1 Answers1

1

You can try using the Custom build outputs for docker build:

docker build -o artifacts_path .

You will want to limit the output (as written in the docs -o will copy everything from the final stage). Add specific stage which will copy only needed files. Something along this lines:

# ...
# everything up to publish including it

FROM scratch AS export-stage
COPY --from=publish /artifacts / # copy from publish artifacts to root

# rest of the file if any

If you have something after publish then use --target parameter for docker build:

docker build --target=export-stage -o artifacts_path .
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • artifacts_path - it is path on my pc? – Murrchalkina Feb 15 '23 at 12:21
  • @Murrchalkina yes, this is path where you want the results. The whole output of the final stage in multistage build will be put there. – Guru Stron Feb 15 '23 at 12:22
  • error from receiver: failed to create dir \c\users\Мику\desktop\artifacts Do you know what should i do? – Murrchalkina Feb 15 '23 at 12:23
  • @Murrchalkina I suggest to use some other folder not in the users directory - it usually requires specific permissions which docker should not have. – Guru Stron Feb 15 '23 at 12:26
  • I tried to save it to c:/test, but 'directory name is invalid' – Murrchalkina Feb 15 '23 at 12:30
  • @Murrchalkina Cause it is invalid. Should not it be `C:\test`? – Guru Stron Feb 15 '23 at 12:32
  • Just tried to do it this way. Still invalid... `docker build -o C:\test -t img -f Service/Dockerfile .` - i use it like that – Murrchalkina Feb 15 '23 at 12:37
  • Error: `error from receiver: failed to create dir C:\test\root\.local\share\NuGet....... The directory is invalid` – Murrchalkina Feb 15 '23 at 12:38
  • @Murrchalkina I think you might want to limit what you a trying to copy out. For my project I just created a specific stage which contained only needed data. – Guru Stron Feb 15 '23 at 12:41
  • And... in that folder was created many "linux" folders, like home and other. But many of that - empty. And there is no "src" folder, which should be... – Murrchalkina Feb 15 '23 at 12:41
  • You wanna say, that I should use `--target` parameter in `docker run`? Or what? – Murrchalkina Feb 15 '23 at 12:42
  • @Murrchalkina you should not use `docker run` in the first place. Please see the update – Guru Stron Feb 15 '23 at 12:47
  • Oh sorry, i want to say in `docker build` – Murrchalkina Feb 15 '23 at 12:49
  • `mount3437207600/artifacts: no such file or directory` – Murrchalkina Feb 15 '23 at 13:07
  • @Murrchalkina this one you need to figure out yourself =) Possibly it should be `/app/build/artifacts` or `/src/artifacts` - in short you need to locate the artifacts in your container and use correct path. – Guru Stron Feb 15 '23 at 13:08
  • Sorry. My fault. It's working. But it gave me `invalid directory name`. Ha-ha :) But i got files :D Is there any way to export just artifacts folder, without other `bin, boot, dev...` ? – Murrchalkina Feb 15 '23 at 13:16
  • @Murrchalkina was glad to help! If answer works for you - feel free to mark it as accepted one ;) As for exporting the artifact folder - yes, as I wrote previously you need to create a stage which will contain only it (as described in the previous update). – Guru Stron Feb 15 '23 at 13:30
  • I've created it, but it exports me other folders too. And published app is corrupted :( – Murrchalkina Feb 15 '23 at 13:40
  • @Murrchalkina TBH for me only needed stuff was exported when correct target was used. As for app being corrupted - self-contained apps AFAIK are bound to the target platform and architecture - check out that you have specified a correct one. – Guru Stron Feb 15 '23 at 13:46
  • 1
    I had a little mistake. Sorry =( Now its working correctly. Thanks a lot!!! – Murrchalkina Feb 15 '23 at 14:20