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/
Related
I am experimenting with Docker and using arguments to decide what code in the image should start.
I have these two entrypoints which both work if run by themselves.
ENTRYPOINT ["dotnet", "MultiStart.API.dll"]
ENTRYPOINT ["dotnet", "MultiStart.Worker1.dll"]
I would like to decide which one is run by using arguments when i call docker run
I've tried several things along the lines of
CMD ["dotnet", $start]
ENTRYPOINT ./dotnet $start
ENTRYPOINT ["dotnet", $start]
ENTRYPOINT ["dotnet" $start]
and then calling it using something like this.
docker run -d -p 8080:80 -e start=MultiStart.API.dll --name multi multistart
But no matter what I do I end up with this error (or some variant there of):
/bin/sh: 1: ./dotnet: not found
Any suggestions as to how this can be achieved?
Ill include the entire Dockerfile that will start one of the two processes for clarity.
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 ["src", "src"]
COPY ["*.sln", ""]
RUN dotnet restore "src/MultiStart.API/MultiStart.API.csproj"
RUN dotnet restore "src/MultiStart.Worker1/MultiStart.Worker1.csproj"
COPY . .
WORKDIR "/src/src"
RUN dotnet build "MultiStart.API/MultiStart.API.csproj" -c Release -o /app/build
RUN dotnet build "MultiStart.Worker1/MultiStart.Worker1.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MultiStart.API/MultiStart.API.csproj" -c Release -o /app/publish
RUN dotnet publish "MultiStart.Worker1/MultiStart.Worker1.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
# both of these work by themselves.
#ENTRYPOINT ["dotnet", "MultiStart.API.dll"]
ENTRYPOINT ["dotnet", "MultiStart.Worker1.dll"]
docker run cannot pass variable like that, after all, docker file is not bash script.
The simplist way is
dockerfile
ENTRYPOINT ["command"]
docker run
docker run imagename argument list
If you want to declare variables in docker file, you can use ARG or ENV.
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
I have created the Dockerfile using visual studio for my dotnet api project but how can we integrate the client app in the docker file. I have created the docker file which looks like:
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 ["Locomote-e-Client.Service/Locomote-e-Client.Service.csproj", "Locomote-e-Client.Service/"]
COPY ["Locomote-e-Client.Core/Locomote-e-Client.Core.csproj", "Locomote-e-Client.Core/"]
COPY ["Locomote-e-Client.Common/Locomote-e-Client.Common.csproj", "Locomote-e-Client.Common/"]
COPY ["Locomote-e-Client.Data/Locomote-e-Client.Data.csproj", "Locomote-e-Client.Data/"]
RUN dotnet restore "Locomote-e-Client.Service/Locomote-e-Client.Service.csproj"
COPY . .
WORKDIR "/src/Locomote-e-Client.Service"
RUN dotnet build "Locomote-e-Client.Service.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Locomote-e-Client.Service.csproj" -c Release -o /app/publish
FROM node:10.21 as nodebuilder
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
ENV PATH /usr/src/app/node_modules/.bin:$PATH
COPY Locomote-e-ClientApp/package.json /usr/src/app/package.json
RUN npm install
COPY Locomote-e-ClientApp/. /usr/src/app
RUN npm run build
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
RUN mkdir -p /app/ClientApp/dist
COPY --from=nodebuilder /usr/src/app/dist/. /app/ClientApp/dist/
ENTRYPOINT ["dotnet", "Locomote-e-Client.Service.dll"]
Whenever I have created the image by using this docker file then it will create the image successfully. But when I tried to run that image using container then it is not running in container.
Command to build the image
docker build -t ashishrajput194/locomate:v1 .
Command to run as container
docker run -p 80:80 -d ashishrajput194/locomate:v1
It is not showing any error but the container exits automatically. I am not able to run the application.
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.
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"]