asp.net core docker-compose refresh on code changes - docker

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.

Related

.Net Core 5 Web API Project not working with Docker Compose

I am trying to use docker-compose with my project.
Having issue with .Net 5 API project.
It works properly with ISS Express and Docker but when I run project with docker-compose api project is not working as expected.
As expeced when running docker-compose it should run API project and launch swagger api page instead it’s displaying different erros when exposed ports are accessed via browser. Please find attached snaps below.
Docker container image
browser run1
browser run2
browser run3
As you can see for each url it's displaying different error message in browser.
Following solutions to resolve issues but none worked.
1. Expose ports using below in docker file
ports:
- 51850:80
Docker expose ports
2. Try with debug environment
ARG BUILD_CONFIGURATION=Debug
ENV ASPNETCORE_ENVIRONMENT=Development
ENV DOTNET_USE_POLLING_FILE_WATCHER=true
ENV ASPNETCORE_URLS=http://+:80
stackoverlow suggestion - set environment to debug
3. Tried using VSCode docker-compose and Visual Studio docker-compose
Regenerated docker-compose file with help of Visual Studio and VS Code.
4. Tried setting up specific ports on Launchsettings.json
Tried settings specif ports with docker in launsettings.json
and exporting from docker, docker compose
Docker
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 44370
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["TAPI/TAPI.csproj", "TAPI/"]
RUN dotnet restore "TAPI/TAPI.csproj"
COPY . .
WORKDIR "/src/TAPI"
RUN dotnet build "TAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "TAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TAPI.dll"]
docker-compose
version: '3.4'
services:
todoworker:
image: tworker
build:
context: .
dockerfile: TWorker/Dockerfile
todoapi:
image: tapi
build:
context: .
dockerfile: TAPI/Dockerfile
ports:
- 80
- 443
- 44370
tangular:
image: tangular
build:
context: .
dockerfile: TAngular/Dockerfile
ports:
- 4200:4200
5. Tried with deleting all containers
Tried deleting all containers, images and recreating all after restarting docker desktop (using docker on windows system)
As suggested in following article Docker for clean build of an image
docker system prune
Docker desktop image
Even deleting images and containers from docker and recreating images some time it displayes image created time from past hours.
Date: 20-May-2021:
I have created another small Sample with two Web APIs. Tested on my local Laptop. Please get that to your local box and execute. Please let me know if you need any further assistance.
GitHub URL: https://github.com/vishipayyallore/mini-projects-2021/tree/master/Projects/CollegeDockerDemo
College.WebAPI Docker File
#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 ["Source/College.WebAPI/College.WebAPI.csproj", "Source/College.WebAPI/"]
RUN dotnet restore "Source/College.WebAPI/College.WebAPI.csproj"
COPY . .
WORKDIR "/src/Source/College.WebAPI"
RUN dotnet build "College.WebAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "College.WebAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "College.WebAPI.dll"]
ToDo.WebAPI Docker File
#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 ["Source/ToDo.WebAPI/ToDo.WebAPI.csproj", "Source/ToDo.WebAPI/"]
RUN dotnet restore "Source/ToDo.WebAPI/ToDo.WebAPI.csproj"
COPY . .
WORKDIR "/src/Source/ToDo.WebAPI"
RUN dotnet build "ToDo.WebAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ToDo.WebAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ToDo.WebAPI.dll"]
docker-compose.yml File
version: '3.4'
services:
college.webapi:
image: ${DOCKER_REGISTRY-}collegewebapi
build:
context: .
dockerfile: Source/College.WebAPI/Dockerfile
todo.webapi:
image: ${DOCKER_REGISTRY-}todowebapi
build:
context: .
dockerfile: Source/ToDo.WebAPI/Dockerfile
docker-compose.override.yml File
version: '3.4'
services:
college.webapi:
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "8000:80"
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
todo.webapi:
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "8001:80"
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro

Running .Net Core Application with docker container but i have an issue like this `0.0.0.0:7001->7001/tcp`

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".

unable to run a net core docker container

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

Use a DockerFile to create an empty MSSQL Express database in a container

I am a little confused as to how all of this works as this is my first time playing with Docker.
I've downloaded the following docker image of Microsoft SQL Server Express (https://hub.docker.com/r/microsoft/mssql-server-windows-express/).
I am trying to docker-compose together some existing containers and every time I run docker-compose up, what I want to have happen is that a new database is created and a target container is run.
Here is the existing Dockerfile for the container that I want to have execute. "Execute this" is the container that I want to have dotnet run run on and any "HelperService" are services that should be up before the target container is run:
FROM microsoft/dotnet:2.0-runtime-nanoserver-1709 AS base
WORKDIR /app
FROM microsoft/dotnet:2.0-sdk-nanoserver-1709 AS build
WORKDIR /src
COPY ExecuteThis/ExecuteThis.csproj ExecuteThis/
COPY ../HelperService/HelperService.csproj ../HelperService/
COPY ../HelperService2/HelperService2.csproj ../HelperService2/
COPY ../HelperService3/HelperService3.csproj ../HelperService3/
COPY ../HelperService4/HelperService4.csproj ../HelperService4/
COPY ../HelperService5/HelperService5.csproj ../HelperService5/
COPY ../HelperService6/HelperService6.csproj ../HelperService6/
RUN dotnet restore ExecuteThis/ExecuteThis.csproj
COPY . .
WORKDIR /src/ExecuteThis
RUN dotnet build ExecuteThis.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish ExecutThis.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "ExecuteThis.dll"]
Here is a docker-compose.yml file:
version: '3.4'
services:
sqlserver:
image: microsoft/mssql-server-windows-express
helperservice:
image: helperservice
helperservice2:
image: helperservice2
helperservice3:
image: helperservice3
helperservice4:
image: helperservice4
helperservice5:
image: helperservice5
helperservice6:
image: helperservice6
executethis:
image: ${DOCKER_REGISTRY}executethis
depends_on:
- sqlserver
- helperservice
- helperservice2
- helperservice3
- helperservice4
- helperservice5
- helperservice6
build:
context: .
dockerfile: ExecuteThis\Dockerfile
My question is: how do I generate a database and where should I write those instructions? How do I dotnet run my "ExecuteThis" container?

Visual Studio for Mac & .NET Core Web API with Docker

I just got a MacBook Pro and am trying to run a dotnet core web api project I created on my Windows PC. I also am learning how to use Docker and am trying to integrate it into my project.
When I right click on my API project and hit Add > Docker Support, a docker-compose project and corresponding .yml file are generated and a Dockerfile is added to my Api project.
This is what the .yml file looks like:
version: '3.4'
services:
api:
image: api
build:
context: .
dockerfile: Api/Dockerfile
And here is the Dockerfile that is generated:
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY AdamWebsiteApi.sln ./
COPY Api/Api.csproj Api/
RUN dotnet restore -nowarn:msb3202,nu1503
COPY . .
WORKDIR /src/Api
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Api.dll"]
When I run the solution, using the docker-compose project as the startup, I get the following error:
Cannot start service api: Mounts denied:
The path /usr/local/share/dotnet/sdk/NuGetFallbackFolder
is not shared from OS X and is not known to Docker.
You can configure shared paths from Docker -> Preferences... -> File Sharing.
See https://docs.docker.com/docker-for-mac/osxfs/#namespaces for more info.
Any idea what I need to do to get this working? It might be really simple and obvious but I am very new to Docker and Macs so I'm not sure what I'm doing.
Add the folder /usr/local/share/dotnet/sdk/NuGetFallbackFolder to the File Sharing list in Docker Preferences. It will work.

Resources