Variable de entorno de docker-compose a .net core app

Oct 27 2020

Quiero pasar mi variable de entorno (API_ENDPOINT) de docker-compose a mi app.config de la API principal de .net.

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}}" />

¿Es posible obtener el valor principal de .net de API_ENDPOINT desde docker-compose?

Respuestas

Romain Oct 27 2020 at 17:12

Puede escribir un ENTRYPOINTpara realizar esta transformación.

#!/bin/sh

# Replacing {{API_ENDPOINT}} placeholder by the value of the variable
sed -ibak -e "s~{{API_ENDPOINT}}~${API_ENDPOINT}~g" \ /app.config exec "$@"

Y agréguelo en su Dockerfile.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1

# ...

COPY docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]
RUN chmod +x /docker-entrypoint.sh

Construye y prueba

# 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/"/>