Variabile di ambiente da docker-compose a .net core app
Oct 27 2020
Voglio passare la mia variabile d'ambiente (API_ENDPOINT) da docker-compose alla mia API .net core app.config.
docker-compose:
services:
api_service:
image: api_image
environment:
- API_ENDPOINT=http://service:3000/api/v1/
ports:
- "44399:44399"
.NET core, app.config:
<add key="apiUrl" value="{{API_ENDPOINT}}" />
È possibile ottenere il valore core .net di API_ENDPOINT da docker-compose?
Risposte
Romain Oct 27 2020 at 17:12
Puoi scrivere un ENTRYPOINTper eseguire questa trasformazione.
#!/bin/sh
# Replacing {{API_ENDPOINT}} placeholder by the value of the variable
sed -ibak -e "s~{{API_ENDPOINT}}~${API_ENDPOINT}~g" \ /app.config exec "$@"
E aggiungilo nel tuo file Dockerfile.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
# ...
COPY docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]
RUN chmod +x /docker-entrypoint.sh
Costruisci e prova
# build
docker build --rm . -t test-net
# test (mimic what is done on compose)
docker run --rm -it --env API_ENDPOINT="http://service:3000/api/v1/" test-net cat /app.config
# <add key="apiUrl" value="http://service:3000/api/v1/"/>