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.
Related
I have an empty .NET 6 web api solution with swagger installed. I have generated a docker file from Visual Studio which looks like this:
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 ["Api.Permissions/Api.Permissions.csproj", "Api.Permissions/"]
COPY ["Api.Permissions.Models/Api.Permissions.Models.csproj", "Api.Permissions.Models/"]
COPY ["Api.Permissions.Services/Api.Permissions.Services.csproj", "Api.Permissions.Services/"]
RUN dotnet restore "Api.Permissions/Api.Permissions.csproj"
COPY . .
WORKDIR "/src/Api.Permissions"
RUN dotnet build "Api.Permissions.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Api.Permissions.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.Permissions.dll"]
I have created a docker image and pushed to my docker hub account.
I have then run this command to pull and run the image locally:
docker container run -d --name mynewapi -p 8000:443 myusername/myreponame:mycontainername
However when I browse to http://localhost:433/swagger/index.html I get:
This site can’t be reached localhost refused to connect.
I have also tried to make a GET request using postman to the sample endpoint but i just get the same error.
What am i doing wrong here?
When you run the container, you map port 443 in the container to port 8000 on the host. So you need to access it using port 8000.
Since you map port 443 which is the https port, you should be using https. So your URL should be https://localhost:8000/swagger/index.html
But that still isn't enough. By default Swagger is only available when your solution is running in Development mode. You control that using the environment variable ASPNETCORE_ENVIRONMENT which needs to be set to 'Development'. You can do that in your docker command when you run the container like this
docker container run -d --name mynewapi -p 8000:443 -e ASPNETCORE_ENVIRONMENT=Development myusername/myreponame:mycontainername
or you can add it to the Dockerfile like this
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
ENV ASPNETCORE_ENVIRONMENT=Development
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Api.Permissions/Api.Permissions.csproj", "Api.Permissions/"]
COPY ["Api.Permissions.Models/Api.Permissions.Models.csproj", "Api.Permissions.Models/"]
COPY ["Api.Permissions.Services/Api.Permissions.Services.csproj", "Api.Permissions.Services/"]
RUN dotnet restore "Api.Permissions/Api.Permissions.csproj"
COPY . .
WORKDIR "/src/Api.Permissions"
RUN dotnet build "Api.Permissions.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Api.Permissions.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.Permissions.dll"]
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/
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'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"]
I am currently developing a NET Application and I have to run bash scripts that:
1.Build the project
2.dotnet publish -c Release the project
3.copy the publish folder to a path for Dockerfile to take on.
Can this be done directly from the dockerfile?
What I am running now:
Bash-script
dotnet build ../${serverSrcFolder} &&
dotnet publish -c Release ../${serverSrcFolder} --output $(pwd)/${serverPublishDestFolder}
Dockerfile
FROM microsoft/dotnet:2.1-aspnetcore-runtime
WORKDIR /app
COPY . /app
ENTRYPOINT [ "dotnet" , "./publish/Server.dll" ]
EXPOSE 9400
EXPOSE 6379
The publish folder stays near the dockerfile.
ParentFolder
Dockerfile
publish
The serverPublishDestFolder from the bash script above is this publish folder.
Can I somehow always map the publish folder whenever it changes from within the dockerfile?
You can do three steps above inside the Dockerfile
e.g.
ROM microsoft/dotnet:2.1.5-aspnetcore-runtime AS base
WORKDIR /app
ENV ASPNETCORE_URLS http://+:5000
EXPOSE 5000
FROM microsoft/dotnet:2.1.403-sdk AS build
WORKDIR /src
COPY src/WebApi/WebApi.csproj src/WebApi/
RUN dotnet restore src/WebApi/WebApi.csproj
COPY . .
WORKDIR /src/src/WebApi
RUN dotnet build WebApi.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish WebApi.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApi.dll"]
This Dockerfile can be easily generated by Visual Studio 2017