How to build dockerfile created by visual studio 2019 from command line? - docker

I have dockerfile created by visual studio 2019.
How can i run it manually?
bellow is the dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-nanoserver-1903 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:2.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["Client/Client.csproj", "Client/"]
RUN dotnet restore "Client/Client.csproj"
COPY . .
WORKDIR "/src/Client"
RUN dotnet build "Client.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Client.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Client.dll"]

Navigate exactly to the project folder (not solution) where the dockerfile is located.
Run the command below in terminal:
docker build -f Dockerfile ..
docker-build

Open a terminal in the folder where dockerfile is located and execute:
docker build -t image_name .
On successful build, execute:
docker run -d image_name
I advise you to read the official documentation for docker build and docker run commands. You can find additional flags which may be useful for your case.

Related

cannot run docker build when using docker setup from Visual Studio

I created a web-project named ProjectService and added docker-support for it using Visual Studio 2022. I can build and debug the image pretty well from within VS.
Now I try to build and run the image from the command-line in order to have it within my build-pipeline. So I execute this from the root-directory of my repo:
docker build -t myrep/demo:latest ./ProjectService
However when I do that I get the following error:
#11 ERROR: "/ProjectService/ProjectService.csproj" not found: not found
#12 [build 4/8] COPY [DatabaseManager/DatabaseManager.csproj, DatabaseManager/]
#12 sha256:b881c00e01ebb7ea687c2f8c5d5f585e237bf6151b63cd21110ed1a7bdf74af6
#12 ERROR: "/DatabaseManager/DatabaseManager.csproj" not found: not found
I think this is because within my docker-file paths are relative:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["ProjectService/ProjectService.csproj", "ProjectService/"]
COPY ["DatabaseManager/DatabaseManager.csproj", "DatabaseManager/"]
RUN dotnet restore "ProjectService/ProjectService.csproj"
COPY . .
WORKDIR "/src/ProjectService"
RUN dotnet build "ProjectService.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ProjectService.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProjectService.dll"]
when I execute docker from within the ProjectService-drectory itself, I get the same error, though.
My folder-structure is this:
root
ProjectService
ProjectService.csproj
DockerFile
DatabaseManager
DatabaseManager.csproj
where ProjectService depends on DatabaseManager.
Your Dockerfile looks like it assumes that the build context is the root directory.
So to build it you can either do it from the root directory with
docker build -t myrep/demo:latest -f ProjectService/Dockerfile .
or from the ProjectService directory with
docker build -t myrep/demo:latest ..
A 3rd option is to move the Dockerfile to the root directory. Then your Dockerfile will be in the directory that's assumed to be the build context. That's how most projects are organized. Then you can build with
docker build -t myrep/demo:latest .

Create an image containing .net core sdk and install custom templates inside the image

I am creating a docker image using visual studio console application and the dockerfile looke like:
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS base
RUN dotnet new --install MyCustomTemplate::1.0.0
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["DockerTemplates/DockerTemplates.csproj", "DockerTemplates/"]
RUN dotnet restore "DockerTemplates/DockerTemplates.csproj"
COPY . .
WORKDIR "/src/DockerTemplates"
RUN dotnet build "DockerTemplates.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DockerTemplates.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerTemplates.dll"]
After creating the image from sdk, I have called the command to install the custom template using the command:
RUN dotnet new --install MyCustomTemplate::1.0.0
But when the image is created, I don't find my custom template in my docker image.
How can I install custom templates inside a image during creation of the image? Anybody can help?
Thanks
I try to replicate but can't.
Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:5.0
RUN dotnet new --install Amazon.Lambda.Templates::5.5.0
Commands
docker build -t test .
docker run --rm test dotnet new --list
Lists the standard templates + the new ones

Docker image refuse to run dotnet test command

I'm trying to embed tests building in docker build process and abort it in case of either of tests fail. My docker file looks like below:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
COPY ["WebApplication1.Tests/WebApplication1.Tests.csproj", "WebApplication1.Tests/"]
RUN dotnet restore "WebApplication1/WebApplication1.csproj"
COPY . .
WORKDIR /src/WebApplication1
RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build
FROM build AS test
WORKDIR /src/WebApplication1.Tests
RUN dotnet test --logger:trx
FROM build AS publish
WORKDIR /src/WebApplication1
RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]
Does anybody have an idea how to deal with it?
Thanks you
You have a multistage docker file, https://docs.docker.com/develop/develop-images/multistage-build/
For your case this will work
docker build --target test -t test-tag .
docker build -t webapp-image .
docker run -ti test-tag # will run tests and give you results
If you want to see output of other stages you need to do the same
Docker build only generates the docker image from your docker file. It does not run it.
You need to use "docker run" after building your image to actually start the image as a container (which will then perform your entry point command)
https://docs.docker.com/engine/reference/run/

Unable to build docker file c# - Visual Studio 2019

After creating and developing WEB Api in c#, I wanted to add docker file and run API in docker container.
My docker file is named Dockerfile, with content like this:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["WebAPI/WebAPI.csproj", "WebAPI/"]
RUN dotnet restore "WebAPI/WebAPI.csproj"
COPY . .
WORKDIR "/src/WebAPI"
RUN dotnet build "WebAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebAPI.dll"]
After building docker file, the Visual Studio code is promted this message in console:
Severity Code Description Project File Line Suppression State
Error CTC1014 Docker command failed with exit code 1.
The command 'cmd /S /C dotnet build "WebAPI.csproj" -c Release -o /app/build' returned a non-zero code: 1 WebAPI c:\devops projekti\web api gir\webapi\dockerfile 1

Docker image doesnt work - probably wrong paths on build

I'm trying to move ASP.NET Core project to docker linux container, however I cannot build up a proper image. When I create default dockerfile (through VisualStudio Add -> DockerSupport) it's setting up paths for COPY to all of the projects. However when I run docker build -t . it pop's out error "no such file or directory". I've tried to stick with official docker documentation with moving asp.net core to docker containers and here is the code I ended with:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ./ ./
RUN dotnet restore "myproject.csproj"
COPY . .
WORKDIR "/src/myproject"
RUN dotnet build "myproject.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "myproject.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "myproject.dll"]
This is the command im using to build up an image:
docker build -t myapp -f myproject/Dockerfile .
I'm building it from the parent directory, because when I was using current working directory it wasn't finding all the .csproj files. When I run this image it doesn't work - I know there are wrong paths, however I have no idea where is the error in logic. Current error is "Program does not contain a static 'Main' method suitable for an entry point" and "A fatal error was encountered. The library 'libhostpolicy.so' required to execute the application was not found in '/app/'. Failed to run as a self-contained app. If this should be a framework-dependent app, specify the appropriate framework in /app/myproject.runtimeconfig.json"
Try the following based off this documentation replace myproject with project name
FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", “myproject.dll"]
Then build and run with the following commands:
docker build -t myproject .
docker run -d -p 8080:80 --name myapp myproject
You should not specify RUN dotnet build "myproject.csproj" -c Release -o /app with -o /app which will output in the same folder with build layer.
Try
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["myproject/*.csproj", "myproject/"]
RUN dotnet restore "myproject/myproject.csproj"
COPY . .
WORKDIR /src/myproject
RUN dotnet build "myproject.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "myproject.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "myproject.dll"]

Resources