After refactoring from docker-compose into separate Docker file for each project, I'm Unable to run the container, it simply exits. I'm using both docker and docker-compose since I'll have more projects down the road.
My docker files are as follows.
docker-compose.yml
version: '3'
services:
customer:
image: customer
container_name: customer
build:
context: ./Customer
dockerfile: Dockerfile
Customer/Dockerfile
FROM mcr.microsoft.com/dotnet/core/runtime:2.2
WORKDIR /Customer
EXPOSE 80
COPY ./bin/Release/netcoreapp2.2/ service/
ENTRYPOINT ["dotnet", "service/Customer.dll"]
Also I had this within docker-compose file before. How do I map 6001 to 80 within Dockerfile?
ports:
- 6001:80
Attempt 2
FROM mcr.microsoft.com/dotnet/core/sdk:2.2
WORKDIR /Customer
ENV DATABASE_HOST=database
ENV DATABASE_NAME=db
ENV DATABASE_USER=sa
ENV DATABASE_PASSWORD=Password
EXPOSE 80
COPY . .
CMD dotnet build
ENTRYPOINT ["dotnet", "Customer/bin/Release/netcoreapp2.2/Department.dll"]
Attempt 3
copied from main site.
Copied from here https://docs.docker.com/engine/examples/dotnetcore/
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 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 mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
Did you mean to run dotnet SDK commands? Please install dotnet SDK
from: https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
Here is a lazy and basic docker file. I added some comments and some helpful build/debug options, e.g: "RUN ls -al" to list the current directory. Just like a linux VM.
# step 1 - building you app with SDK as base image
FROM microsoft/dotnet:2.2-sdk AS build-env # call the environment build
WORKDIR /build # create a work dir
COPY . . # you don't need copy everything to build your app, but this is for simplisity
RUN ls -al # linux command to list dir content
RUN cd /Customer && dotnet publish -o out # actually building the app and publishing to /out dir
RUN cd /Customer && ls -al # navigate to the folder you copied and list dir
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS runtime # step 2, runtime env (slimmed down container)
WORKDIR /app # create workdir
COPY --from=build-env /<YOUR_BULD_PATH>/out ./ # copy from prev container build output
RUN ls -al # list again
ENTRYPOINT ["dotnet", "Department.dll", "--urls", "http://*:6001"] # example from .NET Core 2.2 webapi with port 6005, this might not be your case
Now to run the docker-compose, just point out the docker file like you have already done. But both docker/docker-compose should work just fine now. Ofcourse you need to tweak the docker file a bit, i dont know your app or folder structure.
And just a tip, if you want to run your docker file as stand alone, dont forget the args when you start it to map ports --> -p 6001:80
Related
I'm working on a Blazor Wasm (ASP.Net Core hosted consisting on the usual 3 projects Client, Server and Shared) app that I want to deploy to Linux using docker.
I'm copying a nginx.conf file to the Server project root folder but when trying to publish to Azure App Service Containers I'm getting:
failed to compute cache key: "/nginx.conf" not found: not found
This is my Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["MyLocalShop/Server/MyLocalShop.Server.csproj", "MyLocalShop/Server/"]
COPY ["MyLocalShop.Services.MongoDb/MyLocalShop.Services.MongoDb.csproj", "MyLocalShop.Services.MongoDb/"]
COPY ["MyLocalShop.Server.Contracts/MyLocalShop.Server.Contracts.csproj", "MyLocalShop.Server.Contracts/"]
COPY ["MyLocalShop/Shared/MyLocalShop.Shared.csproj", "MyLocalShop/Shared/"]
COPY ["MyLocalShop/Client/MyLocalShop.Client.csproj", "MyLocalShop/Client/"]
RUN dotnet restore "MyLocalShop/Server/MyLocalShop.Server.csproj"
COPY . .
WORKDIR "/src/MyLocalShop/Server"
RUN dotnet build "MyLocalShop.Server.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyLocalShop.Server.csproj" -c Release -o /app/publish
FROM nginx:alpine AS final
WORKDIR /app
COPY --from=publish /app/publish .
COPY nginx.conf /etc/nginx/nginx.conf
ENTRYPOINT ["dotnet", "MyLocalShop.Server.dll"]
If I run the dotnet publish command to check the ouput directory within app/publish, I can see the file is actually there.
What am I missing?
This might happen when the file you want to COPY is also in the .dockerignore
Are you setting the context of the docker build to the directory that contains the nginx.conf and not a directory below that?
e.g.
nginx.conf
|-MyLocalShop
Bad: docker build -t something:latest -f ./DockerFile ./MyLocalShop
Good: docker build -t something:latest -f ./DockerFile .
Maybe you lack of nginx.conf file in your project.
Have this Dockerfile
Dockerfile
FROM python:3.8
ADD Pipfile.lock /app/Pipfile.lock
ADD Pipfile /app/Pipfile
WORKDIR /app
COPY . /app
RUN pip install pipenv
RUN pipenv install --system --deploy --ignore-pipfile
ENV FLASK_APP=app/http/api/endpoints.py
ENV FLASK_RUN_PORT=4433
ENV FLASK_ENV=development
ENTRYPOINT ["python"]
CMD ["-m", "flask", "run"]
Why in the Docker container my app lands in
/app/app/http/api
App gets duplicated
How to copy it to:
/app/http/api
How can I fix it?
Update 1:
My docker-compose and Dockerfile and ls output.
https://0bin.net/paste/ePRws72M#IV8H6iRJ+UMJFr8lB7CRQYKwsnGYflsmOlyvFxZA7zE
You got two problems here
copy . /app copies the whole working directory from the host machine into the /app directory in the image. If you just want the ./app subdirectory change that to copy ./app /app
Your docker-compose mounts the current working directory of the host machine into the container under the mount point /app . This hides the existing directory /app of the image. So you will either need to copy your app to a different directory or use a different mount point for your volume
I try to containerize my .net core application but i have an issue on app container ports. It seems as 0.0.0.0:7001->7001/tcp. I think problem exists because of this. Is there any other problems on there?
My dockerfile and docker-compose file like below
Dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-nanoserver-1903 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:2.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["Blog.Web/Blog.Web.csproj", "Blog.Web/"]
COPY ["Blog.BLL/Blog.BLL.csproj", "Blog.BLL/"]
COPY ["Blog.DAL/Blog.DAL.csproj", "Blog.DAL/"]
COPY ["Blog.Models/Blog.Models.csproj", "Blog.Models/"]
RUN dotnet restore "Blog.Web/Blog.Web.csproj"
COPY . .
WORKDIR "/src/Blog.Web"
RUN dotnet build "Blog.Web.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Blog.Web.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Blog.Web.dll"]
Docker-compose.yaml
version: '3.4'
services:
client:
build:
dockerfile: Blog.Web\Dockerfile
context: .
ports:
- "7001:7001"
Bash Display
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5e3edcbc969 blogwebapplication_client "dotnet Blog.Web.dll" About a minute ago Up About a minute 0.0.0.0:7001->7001/tcp blogwebapplication_client_1
When I try to access my app on localhost:7001 I get an error:
"This site cant be reach".
I've read here that I can get possibility not to run docker-compose build every time code changes by adding volumes sections to docker-compose.yml. But I can't achieve this feature in my ASP.NET Core application.
Dockerfile:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["dockercompose/dockercompose.csproj", "dockercompose/"]
RUN dotnet restore "dockercompose/dockercompose.csproj"
COPY . .
WORKDIR "/src/dockercompose"
RUN dotnet build "dockercompose.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "dockercompose.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "dockercompose.dll"]
docker-compose.yml:
version: '3.4'
services:
dockercompose:
image: ${DOCKER_REGISTRY}dockercompose
build:
context: .
dockerfile: dockercompose/Dockerfile
ports:
- "9000:80"
volumes:
- .:/src/dockercompose
What am I missing?
You should use dotnet watch run for that. However that means you need to use the SDK image (microsoft/dotnet:2.1-sdk) not the runtime only image (microsoft/dotnet:2.1-aspnetcore-runtime).
I just use two Docker files and have two services defined in the docker-compose.yml. One uses the dockerfile with the runtime image (for deployment), the other one with the SDK image (for development).
The development dockerfile could look something like this:
FROM microsoft/dotnet:2.1-sdk AS build-env
# Use native linux file polling for better performance
ENV DOTNET_USE_POLLING_FILE_WATCHER 1
WORKDIR /app
ENTRYPOINT dotnet watch run --urls=http://+:5000
You might have to chnage the path according to your project and how you map it in your docker-compose.yml, but this is more or less it.
I just started a new solution with a .NET Core Project (2.1) using visual studio 15.8.8. It can run and debug it by setting the docker compose file as a startup project. It works!
Logically, I should be able to build the docker image with a simple commandline statement. However, it complains that the csproj cannot be found. This is strange. The file exist and as I told, I can run it from visual studio. I tried it from one directory up and the directory that has the dockerfile. Same problem.
How can I solve this? The only thing I want is simply build my image and then run it by just using docker commands.
Dockerfile
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["TryNewDocker2/TryNewDocker2.csproj", "TryNewDocker2/"]
RUN dotnet restore "TryNewDocker2/TryNewDocker2.csproj"
COPY . .
WORKDIR "/src/TryNewDocker2"
RUN dotnet build "TryNewDocker2.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "TryNewDocker2.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TryNewDocker2.dll"]
Het is the compose file:
version: '3.4'
services:
trynewdocker2:
image: ${DOCKER_REGISTRY}trynewdocker2
build:
context: .
dockerfile: TryNewDocker2/Dockerfile
Logically, I want "docker-compose up" to keep working when fixing this problem.
This is caused by the wrong root folder for the file path in dockerfile.
For launching from Docker, its root folder is C:\Users\...\repos\TryNewDocker2, but while running from command, its root fodler is C:\Users\...\repos\TryNewDocker2\TryNewDocker2, so the path for TryNewDocker2.csproj has changed from TryNewDocker2/TryNewDocker2.csproj to TryNewDocker2.csproj
Try dockerfile below:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 59162
EXPOSE 44342
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["TryNewDocker2.csproj", "TryNewDocker2/"]
RUN dotnet restore "TryNewDocker2/TryNewDocker2.csproj"
COPY . ./TryNewDocker2/
WORKDIR "/src/TryNewDocker2"
RUN dotnet build "TryNewDocker2.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "TryNewDocker2.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TryNewDocker2.dll"]
Update
For working in both Docker and command, do not change your dockerfile, and from path below to run your command with specifying the dockerfile path.
C:\Users\...\repos\TryNewDocker2>docker build -t gogo -f TryNewDocker2/Dockerfile .
For those of you who end up here years later like I did, I'll share my experience.
My problem was caused by the auto-generated Dockerfile that came from Visual Studio's "add > Docker Support..." was on the same level as my .csproj file.
The specific line causing me trouble was COPY ["MyApp/MyApp.csproj", "MyApp/"] which should have been just COPY ["MyApp.csproj", "MyApp/"]. Removing the extra MyApp/ in front of the .csproj got the build working fine.
Special thanks to Edward in the answer above for pointing me in the right direction.