Docker Community Forums

Share and learn in the Docker community.

Change app.config by bash in linux container

Hello. I try to change value in below line in file app.config in Linux container.
<add key="dmtUrl" value="{{API_ENDPOINT}}" /
I created bash script and put in ENTRYPOINT in docker-compose:
Bash script(entrypoint.sh):

#!/bin/sh
set -e
for i in ./app.config; do
    sed -i "s|{{API_ENDPOINT}}|$API_ENDPOINT|" "$i"
done
exec "$@"

Dockerfile:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base

WORKDIR /app
EXPOSE 44399 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["Project1.Api/Project1.Api.csproj", "Project1.Api/"]
COPY ["Project2.Test/Project2.Test.csproj", "Project1.Test/"]
RUN dotnet restore "Project1.Api/Project1.Api.csproj"
COPY . .
WORKDIR "/src/Project1.Api"
RUN dotnet build "Project1.Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Project1.Api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
ENV ASPNETCORE_URLS http://*:44399
COPY --from=publish /app/publish .
ENTRYPOINT ["/bin/bash", "./entrypoint.sh"]
CMD ["dotnet", "Project1.Api.dll"]

Docker-compose:

services:
  mse_api_service:
    image: mse_api
    environment:
      - DMT_ENDPOINT=http://dbm_service:3000/api/v1/

I run bash script in windows and it change API_ENDPOINT correctly, but on my Linux container in does not work. Any idea what am I doing wrong?

You could add, for example an appsettings.docker.json file as a template for settings. Only a template, so no real values are stored in the settings file. Only placeholders.
The release in your CI/CD pipeline could replace the placeholders with the real values. This appsettings.docker.json file looks like this:

{
“ServiceBus”: {
“Url”: “#{ServiceBusUrl}#”,
“Key”: “#{ServiceBusKey}#”
},
“Database”: {
“ConnectionString”: “#{DatabaseConnectionString}#”
}
}

REPORT THIS AD
REPORT THIS AD

This appsettings.docker.json file should be loaded in Program.cs as optional file:

public static IWebHost BuildWebHost(string args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((buildercontext, config) =>
{
config.AddJsonFile(“settings/appsettings.docker.json”, optional: true);
})
.UseStartup()
.Build();
The settings folder specified above will be provided by mapping a Volume to the Docker container. First you need to add the secret:

kubectl create secret generic settingsdemo-docker-appsettings --from-file=./appsettings.docker.json
The following deploymentfile deploys a Docker Image and mounts a volume to the secret file. The BuildWebHost mentioned above is reading from this path:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: settingsdemo-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: settingsdemo
spec:
containers:
- name: settingsdemo
image: myregistry.azurecr.io/settingsdemo:#{ContainerVersion}#
ports:
- containerPort: 80
imagePullPolicy: Always
volumeMounts:
- name: settings
mountPath: /app/settings
readOnly: true
volumes:
- name: settings
secret:
secretName: settingsdemo-docker-appsettings