DSA
DSA

Reputation: 780

Build Docker image for .net core project through command

I want to create docker image for my .net core project. I already have .net core project created with docker enable feature. If I run project from VS, it automatically creates docker image for me. But I want to create image through docker commands (I don't want to create/run container on my machine, just want to create docker image from source code). I am using docker for windows.

Any help?

Upvotes: 0

Views: 294

Answers (2)

Jayoti Parkash
Jayoti Parkash

Reputation: 878

You can build the docker image from .net core project in the following way:

  1. In your project, you have a docker file which contains information like: (if not then create).

    FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
    WORKDIR /app
    EXPOSE 80
    EXPOSE 443
    
    FROM microsoft/dotnet:2.2-sdk AS build
    WORKDIR /src
    COPY TestProject.API/TestProject.API.csproj TestProject.API/
    RUN dotnet restore TestProject.API/TestProject.API.csproj        
    COPY . .        
    WORKDIR /src/TestProject.API        
    RUN dotnet build TestProject.API.csproj -c Release -o /app
    
    FROM build as unittest        
    WORKDIR /src/TestProject.UnitTest
    
    FROM build AS publish        
    RUN dotnet publish TestProject.API.csproj -c Release -o /app
    
    FROM base AS final        
    WORKDIR /app        
    COPY --from=publish /app .        
    ENTRYPOINT ["dotnet", "TestProject.API.dll"]
    
  2. Build the project in the solution or using command via opening the same in command prompt and then use the command:

    E:\TestProject\TestProject.API>docker build .

It will create the image. You can check the same the via command:

E:\TestProject\TestProject.API>docker images

Upvotes: 1

Nick
Nick

Reputation: 143

You will need to create a Dockerfile and check that into your source repository if you don't have it already. This tells docker how to create an image.

Running docker build from the directory with the Dockerfile will build you the image.

You can then execute docker push to push that image to a docker repository, or docker run to create a container with that new image.

You'll probably also want to be familiar with docker images to list the images on your machine, and docker rmi to remove old images. They build up fast during development so it helps to be on top of that.

Upvotes: 0

Related Questions