We have one API project with DockerFile
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 43001
EXPOSE 43002
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY Api.csproj Api/
RUN dotnet restore Api/Api.csproj
COPY . .
WORKDIR /src/Api
RUN dotnet build Api.csproj --no-restore -c Release -o /app
FROM build AS publish
WORKDIR /src/Api
RUN dotnet publish Api.csproj --no-restore --no-self-contained -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Api.dll"]
And with docker-compose
version: '3.5'
services:
Api:
image: Api
restart: always
environment:
- ASPNETCORE_URLS=https://+:443;http://+:80
- ASPNETCORE_ENVIRONMENT=Production
build:
context: .
dockerfile: Api/Dockerfile
ports:
- "43001:80"
- "43002:443"
This project pushed to private docker registry.
And we have second Prod project that should use and connect to Api project.
docker-compose Prod project:
version: '3.5'
services:
Prod:
image: Prod
restart: always
environment:
- ASPNETCORE_ENVIRONMENT=Production
- ASPNETCORE_URLS=http://+:80
build:
context: .
dockerfile: Prod/Dockerfile
depends_on:
- Api
networks:
- proxy-net
expose:
- "80"
Api:
image: {RegistryAddress}/Api
restart: always
networks:
- proxy-net
ports:
- "43002:443"
networks:
proxy-net:
external:
name: proxy-net
Issue - When I am connecting from Prod to Api I am getting Connection refused. Perhaps something with ports settings, but I tried a lot of changes and it didn`t help. I thought Api ports settings should be enough, but looks like we need to set something in Prod also.
Related
I'm trying to containerize a .NET6 WebApi with a postgrescontainer. I have this Docker file to build the web api image:
#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:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MicroserviceTemplateDDD.csproj", "MicroserviceTemplateDDD/"]
RUN dotnet restore "MicroserviceTemplateDDD/MicroserviceTemplateDDD.csproj"
WORKDIR "/src/MicroserviceTemplateDDD"
COPY . .
RUN dotnet build "MicroserviceTemplateDDD.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MicroserviceTemplateDDD.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
WORKDIR /app/publish
ENTRYPOINT ["dotnet", "MicroserviceTemplateDDD.dll"]
This runs successfully. But when i run docker-compose up to start the postgre and webapi container i get this error:
microservicetemplateddd_service1 | Process terminated. Couldn't find a valid ICU package installed on the system. Please install libicu using your package manager and try again. Alternatively you can set the configuration flag System.Globalization.Invariant to true if you want to run with no globalization support. Please see https://aka.ms/dotnet-missing-libicu for more information.
My docker compose:
version: "3.7"
services:
api:
image: microservicetemplateddd_service1
container_name: microservicetemplateddd_service1
restart: always
build:
context: .
dockerfile: Dockerfile
environment:
- ConnectionStrings:Context=Server=MicroserviceTemplateDDD_Service1_database;Database=Database;User Id=pa;Password=P4ssW0rd!;
- DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
depends_on:
- database
networks:
- network
ports:
- 8090:80
database:
image: postgres
container_name: microservicetemplateddd_service1_database
restart: always
environment:
- ACCEPT_EULA=Y
- POSTGRES_PASSWORD=P4ssW0rd!
- POSTGRES_USER=pa
networks:
- network
ports:
- 1433:1433
volumes:
- database:/var/opt/mssql
networks:
network:
volumes:
database:
I already tried to use other ms images like alpine and change the place of setting the env.
Per the docs, you should set the environment variable to 1 to disable globalization. Like this
environment:
- ConnectionStrings:Context=Server=MicroserviceTemplateDDD_Service1_database;Database=Database;User Id=pa;Password=P4ssW0rd!;
- DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1
Since you always need to set it to run your container, it'll be a good idea to put the configuration into the image. You can put it at the beginning of the dockerfile and remove it from the docker-compose file
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1
WORKDIR /app
EXPOSE 80
EXPOSE 443
That way you can't forget to set it and have the error pop up again.
I had a similar issue and got around it by adding this condition in the csproj
<ItemGroup Condition="$([MSBuild]::IsOSPlatform('Windows'))">
<RuntimeHostConfigurationOption Include="System.Globalization.AppLocalIcu" Value="68.2" />
</ItemGroup>
I've an Asp.Net core web application, that will get deployed within docker images. How can I allow users to provide an appsettings.json?
I build my docker image 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 ["Backend/Some-Project.csproj", "Backend/"]
RUN dotnet restore "Backend/Some-Project.csproj"
COPY Backend Backend
WORKDIR "/src/Backend"
RUN dotnet build "Some-Project.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Some-Project.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Some-Project.dll"]
My current docker-compose.yml look like this:
version: "3"
services:
mongo:
image: mongo:latest
container_name: "my-app-mongo"
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: my-app-db
ports:
- 27017:27017
vs-central:
image: my-app:latest
container_name: "my-app"
restart: always
How should modify those to replace the appsettings.json ? I taught about a volume, but since it's only one file and not a directory, I think it will not work
You can volume map a single file. It just requires that it exists on the host. If it doesn't, Docker will create a directory on the host and map that.
In your case you can do
vs-central:
image: my-app:latest
container_name: "my-app"
restart: always
volumes:
- ./appsettings.json:/app/appsettings.json:ro
I've added the ro modifier to make the file read-only, so it can't be modified by the container by mistake.
I have a problem / question what should I do when I have docker-compose.yml with several services and want to push this as a project into the repository.
My docker-compose.yml
version: '3.8'
configs:
prometheus_config:
file: ./prometheus.yml
networks:
inner_net:
driver: overlay
services:
app:
build: .
ports:
- 6020:80
container_name: AVM.Core
image: avmtemplate_app
prometheus:
image: quay.io/prometheus/prometheus:latest
ports:
- 9090:9090
configs:
- source: prometheus_config
target: /etc/prometheus/prometheus.yml
depends_on:
- app
container_name: Prometheus
grafana:
image : grafana/grafana
depends_on:
- prometheus
ports:
- 3000:3000
volumes:
- ./grafana/data/:/var/lib/grafana
- ./grafana/provisioning/:/etc/grafana/provisioning/
env_file:
- ./config.monitoring
container_name: Grafana
As you can see, I have three services that depend on each other.
The "grafana" service depends on the "prometheus" service and this depends on the "app" service.
And this is what my "dockerfile" looks like:
#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:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
EXPOSE 3000
EXPOSE 9090
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["../AVM.Core/AVM.Core.csproj", "../AVM.Core/"]
COPY ["../AVM.Domain/AVM.Domain.csproj", "../AVM.Domain/"]
COPY ["../AVM.Repository/AVM.Repository.csproj", "../AVM.Repository/"]
COPY ./prometheus.yml /etc/prometheus/prometheus.yml
RUN dotnet restore "../AVM.Core/AVM.Core.csproj"
COPY . .
WORKDIR "/src/AVM.Core"
RUN dotnet build "AVM.Core.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "../AVM.Core/AVM.Core.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app/AVM.Core
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "AVM.Core.dll"]
When I use docker-compose up i get three images and one project which include all this images. I Paste a screenshots that show it.
Maybe some of you know how to push it into the repository so that a person who would like to use my template has to download only a project , and then she just launched the project to make it work? If this is not possible, what else should I do?
Thanks for all your answers
Cheers
There are several microservices in solution: api, auth, filtering. I'm wondering how to run them all in one dockerfile and docker-compose.
Here what i've done:
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 ["ShoppingCart.API/ShoppingCart.API.csproj", "ShoppingCart.API/"]
RUN dotnet restore "ShoppingCart.API/ShoppingCart.API.csproj"
COPY . .
WORKDIR "/src/ShoppingCart.API"
RUN dotnet build "ShoppingCart.API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ShoppingCart.API.csproj" -c Release -o /app/publish
WORKDIR /app
COPY --from=build /app/build .
ENTRYPOINT ["dotnet", "ShoppingCart.API.dll"]
Docker-compose:
version: '3.9'
networks:
localdev:
name: localdev
services:
api:
container_name: api
build: .
depends_on: [ mssql ]
ports:
- "8080:80"
networks:
- localdev
ordering:
container_name: ordering
mssql:
image: "mcr.microsoft.com/mssql/server"
container_name: mssql
hostname: mssql
environment:
SA_PASSWORD: "fA0bD7rO3iE1mU0w"
ACCEPT_EULA: "Y"
restart: unless-stopped
networks:
- localdev
ports:
- "1433:1433"
expose:
- 1433
rabbitmq:
image: "rabbitmq:3-management"
container_name: "rabbitmq"
ports:
- "5672:5672"
- "15672:15672"
So, API i could wrapped in docker. How to make it with others? For instance, in solution is ShoppingCart.Auth
when i use docker build i receive this error: error image
i've change the relative path on docker file to absolute path changing --from=build-env to bin/Release/netcoreapp3.1/publish/ but when i use docker-compose the error show again
Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 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:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "SmartSchool.WebAPI.dll"]
docker-compose
version: "3.8"
volumes:
SmartSchoolDb:
networks:
mysqlNET:
smartschoolNET:
services:
mysql:
image: "mysql:5.7"
container_name: mysql
ports:
- "3306:3306"
volumes:
- SmartSchoolDb:/var/lib/mysql
networks:
- mysqlNET
environment:
- MYSQL_USER=root
- MYSQL_PASSWORD=test
- MYSQL_ROOT_PASSWORD=test
- MYSQL_ROOT_HOST=%
- bind-address:0.0.0.0
smartschool:
build:
context: .
dockerfile: Dockerfile
container_name: smart
networks:
- mysqlNET
- smartschoolNET
ports:
- 5000:80
environment:
- DBHOST=mysql
depends_on:
- mysql
I add .dockerignore created using vscode command ctrl + shift + p and docker: add docker files to workspace
i used this .dockerignore below
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
README.md
bin/
obj/
out/
TestResults/