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
Related
I want to dockerize an existing .NET core 5 app and used the container tools to generate a Dockerfile. When I debug with Visual Studio 2022 it works but when I manually run it with the command docker build -t some-name . it generates the following error:
Step 7/21 : COPY ["MechEng.MovableBridges.Api/MechEng.MovableBridges.Api.csproj", "MechEng.MovableBridges.Api/"]
COPY failed: file not found in build context or excluded by .dockerignore: stat MechEng.MovableBridges.Api/MechEng.MovableBridges.Api.csproj: file does not exist
Why doesn't it find the file?
This is my Dockerfile that is inside the MechEng.MovableBridges.Api folder:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["MechEng.MovableBridges.Api/MechEng.MovableBridges.Api.csproj", "MechEng.MovableBridges.Api/"]
COPY ["MechEng.MovableBridges.Infrastructure/MechEng.MovableBridges.Infrastructure.csproj", "MechEng.MovableBridges.Infrastructure/"]
COPY ["MechEng.MovableBridges.MathCad/MechEng.MovableBridges.Infrastructure.MathCad.csproj", "MechEng.MovableBridges.MathCad/"]
COPY ["MechEng.MovableBridges.Application/MechEng.MovableBridges.Application.csproj", "MechEng.MovableBridges.Application/"]
COPY ["MechEng.MovableBridges.Domain/MechEng.MovableBridges.Domain.csproj", "MechEng.MovableBridges.Domain/"]
RUN dotnet restore "MechEng.MovableBridges.Api/MechEng.MovableBridges.Api.csproj"
COPY . .
WORKDIR "/src/MechEng.MovableBridges.Api"
RUN dotnet build "MechEng.MovableBridges.Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MechEng.MovableBridges.Api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MechEng.MovableBridges.Api.dll"]
Your command fails, because the build context is wrong. Visual studio is using the solution root folder as build context, you are (probably) using the project's dockerfile's location. You can read more about the docker build command here.
Your command should look similar to this:
docker build -f "<path-to-your-dockerfile>" -t some-name "<!!!path-to-your-solution-folder!!!>"
You can see the exact command executed by visual studio in the output window, with "Container Tools" selected from the dropdown box.
In visual studio, you can move the docker file to one back folder.
For example, folder1/folder2/dockerfile move to folder1/dockerfile and build the image.
Docker build -t imageName .
Docker run -p 7778:80 imageName
I have asp.net core project developed in my machine with docker support. The docker file resides in my project.
i.e, its placed outside the modules and under the rootfolder.
This image is working perfectly fine when I run using Visual studio.
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
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 ["WebApplication4.csproj", ""]
RUN dotnet restore "./WebApplication4.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WebApplication4.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApplication4.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication4.dll"]
I have created a pipeline in Azure Devops. and pushed the same code the way it is in my system, to azure git repository. but it fails to at workdir itself.
What is that I am missing here?
I created a blazor project using .netcore3.1.
I use PhysicalFileProvider in startup.cs.
I ran it with docker using config in docker.
The build was successful, but an exception occurred during execution.
The docker file is as follows:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1903 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["Web/Web.csproj", "Web/"]
RUN dotnet restore "Web/Web.csproj"
COPY . .
WORKDIR "/src/Web"
RUN dotnet build "Web.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Web.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Web.dll"]
This is a docker file automatically generated by Visual Studio.
Why can't the program find the directory?
Or, is there another way to find the directory?
Following this link I build a .Net Core application and was able to run the application in a docker container. My host machine OS is Windows 10, and I now want to try to run the linked application in a Linux container. I have switched to Linux containers with Docker Desktop.
When I'm building the docker image I get this error:
failed to register layer: error creating overlay mount to /var/lib/docker/overlay2/f3e5279484774002c78a8eb66702c9ee7bca7038b59f4eeca7085b88dcbe25d9/merged: too many levels of symbolic links
The Dockerfile I've Used:
FROM mcr.microsoft.com/dotnet/core/runtime:3.1-nanoserver-1903 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["NetCore.Docker/NetCore.Docker.csproj", "NetCore.Docker/"]
RUN dotnet restore "NetCore.Docker/NetCore.Docker.csproj"
COPY . .
WORKDIR "/src/NetCore.Docker"
RUN dotnet build "NetCore.Docker.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "NetCore.Docker.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "NetCore.Docker.dll"]
Thanks
reason for this is I was using wrong image, had to change the image which support linux containers
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.