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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With