COPY . . command in Dockerfile for ASP.NET

后端 未结 2 735
时光取名叫无心
时光取名叫无心 2020-12-31 21:58

The Visual Studio tooling for Docker creates a Dockerfile for ASP.NET projects containing a COPY . . command as below:

WORKDIR /src
COPY *.sln .         


        
2条回答
  •  清酒与你
    2020-12-31 22:21

    The COPY . . copies the entire project, recursively into the container for the build.

    The reason for the separation of the first 2 COPY commands with dotnet restore and then the complete COPY with dotnet build is a Docker caching trick to speed up builds. It is done this way so the project dependencies don't need to be installed every time a code change is made.

    Docker images are built in layers. Docker compares the contents and instructions that would make up the each new layer to previous builds. If they match the SHA256 checksum for the existing layer, the build step for that layer can be skipped.

    Code changes a lot more than dependencies, and dependencies are usually fetched from a slow(ish) network now. If you copy the code after the dependencies are completed then you don't bust the cached dependency layer for every other change.

    The Node.js equivalent does the package.json before the app contents:

    WORKDIR /app
    COPY package.json /app/
    RUN npm install
    COPY . /app/
    CMD ["node", "app/index.js"]
    

提交回复
热议问题