8

I'm building an Azure DevOps pipeline for a dotnet core project. After the dotnet publish command executes the artifacts get saved in $(artifactStagingDirectory). How do I copy these artifacts from the staging directory to the docker image?

I am not building project in dockerfile. I am executing dotnet publish in pipeline.

I have tried to save artifacts to another folder by using:

dotnet publish --configuration $(BuildConfiguration) --output out
PedroC88
  • 3,708
  • 7
  • 43
  • 77
Vaibhav Khalane
  • 101
  • 1
  • 6
  • First, do you have any experience with creating a windows docker image? If so, then you need to issue the exact same commands as you usually would and have your files staged in the appropriate directories prior to executing the dockerfile recipe. Visual Studio 2017/2019 have support for the docker project files, so they can create docker images through the msbuild/dotnet framework. You might want to explorer that. But `dotnet publish` just merely packs the binary output of your app ready for deployment... nothing else. – Antebios Aug 21 '19 at 14:57
  • I want to copy the binary output of publish command into the docker image. That docker image contains dotnetcore runtime. I want to do this because when I want to get the app running in the docker. How do I copy that? – Vaibhav Khalane Aug 22 '19 at 14:22

2 Answers2

14

Publishing to $(Build.ArtifactStagingDirectory) is fine. There are several ways to do it, so here's one:

  1. Add a Docker task after the publish, configured to "Build"
  2. Set the "Build Context" in the task to $(Build.ArtifactStagingDirectory). That's the root path Docker will use for commands like COPY in a Dockerfile.
  3. Commit a Dockerfile to your repo, and set the Dockerfile path in the task to match its location

Set up the Dockerfile like this (I'm assuming .NET Core 2.2 here):

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "myAppNameHere.dll"]

Because you've set the Docker Build Context to $(Build.ArtifactStagingDirectory), where your app has been published, the COPY command will use that as a "current working directory." The translation of the COPY is "copy everything in $(Build.ArtifactStagingDirectory) to the /app folder inside the container."

That'll get you a basic Docker container that simply contains your pre-built and published app files.

Thomas F. Abraham
  • 2,082
  • 1
  • 19
  • 24
  • 2
    For yaml pipeline, config that worked for me was (for `task: Docker@2`): `buildContext: $(Build.ArtifactStagingDirectory)` – James Reategui Jan 04 '20 at 19:37
0

When downloading a secureFile (via the DownloadSecureFile task), the buildContext needs to be $(Agent.TempDirectory). Be aware that with this certain buildcontext, you only need to pass the filename of the securefile, not the entire path.

Example:

- task: Docker@2
  displayName: 'Build image that relies on a SecureFile'
  inputs:
    command: 'build'
    # Explicit path reference due to the buildContext
    Dockerfile: '$(Build.Repository.LocalPath)/Docker/Dockerfile'
            tags: |
              <your tag here>
            arguments: |
              --build-arg DOCKERFILE_ARG=<secure_file_name> 
            buildContext: $(Agent.TempDirectory)
cigien
  • 57,834
  • 11
  • 73
  • 112
OddKMS
  • 1