Search code examples
dockershellamazon-ecsdatadog

Unable to pass the env variable to container


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

ENV CORECLR_ENABLE_PROFILING=1 \
    CORECLR_PROFILER={846F5F1C-F9AE-4B07-969E-05C26BC060D8} \
    CORECLR_PROFILER_PATH=/opt/datadog/Datadog.Trace.ClrProfiler.Native.so \
    DD_INTEGRATIONS=/opt/datadog/integrations.json \
    DD_DOTNET_TRACER_HOME=/opt/datadog

WORKDIR /app

RUN apk --no-cache update \
    && apk add bash make curl

ARG TRACER_VERSION=1.19.1

RUN mkdir -p /opt/datadog
RUN curl -L https://github.com/DataDog/dd-trace-dotnet/releases/download/v${TRACER_VERSION}/datadog-dotnet-apm-${TRACER_VERSION}.tar.gz \
    |  tar xzf - -C /opt/datadog
WORKDIR /app
COPY --from=buildcontainer /app/build .

COPY ./Entrypoint.sh /
RUN chmod +x /Entrypoint.sh && /Entrypoint.sh

ENTRYPOINT ["dotnet","testdatadog.dll"]

Entrypoint.sh

#!/bin/bash
set -e
curl http://169.254.169.254/latest/meta-data/local-ipv4 > temp_var
export DD_AGENT_HOST=$(cat temp_var)
exec "$@"

When I ssh in to my ec2 and see for environment variables I don't see the DD_AGENT_HOST set. When I am manually trying to set the env it works. Am I missing something? appreciate the inputs.


Solution

  • The gist of the problem is that your directive RUN chmod +x /Entrypoint.sh && /Entrypoint.sh got executed at BUILD time, and had no impact on your RUNTIME environment.

    Konrad's correct in changing your ENTRYPOINT to have Entrypoint.sh change your environment.

    The issue with Konrad's solution is that the run command he used to start the container had no impact on the bash shell that docker ran when he issued the exec command.

    Expanding on Konrad's answer a little, you can actually get entrypoint.sh to set your environment for you.

    I copied Konrad's Dockerfile, but changed the ENTRYPOINT as follows:

    FROM alpine:latest AS build
    
    COPY entrypoint.sh /entrypoint.sh
    RUN chmod +x /entrypoint.sh
    
    FROM alpine:latest
    
    RUN apk add --no-cache bash
    COPY --from=build /entrypoint.sh /entrypoint.sh
    
    ENTRYPOINT [ "/entrypoint.sh", "bash" ]
    

    Now, the entrypoint.sh will set the environment, and execute bash.

    I used Konrad's command to run the container:

    docker run -itd --rm --name entrypoint entrypoint-test:latest
    24a7f3d740c37bb345374a835a465482e3bf49a04361e55205b6d63af48d5c3d
    

    This results in the container being launched. Entrypoint.sh will run bash for us, but it's in daemon mode, so we will have to attach to it:

    $ docker attach 24a7f3d740c37bb345374a835a465482e3bf49a04361e55205b6d63af48d5c3d
    bash-5.0# echo $DD_AGENT_HOST 
    127.0.0.1
    

    Alternatively, you can just run the docker image without the -d option, avoiding the daemon mode:

    $ docker run -it --rm --name entrypoint  entrypoint-test:latest
    bash-5.0# echo $DD_AGENT_HOST 
    127.0.0.1
    bash-5.0# echo Hit Ctrl-P Ctrl-Q to leave this container running
    

    All that said, will this work for your dotnet invocation?

    ENTRYPOINT [ "/entrypoint.sh", "dotnet" , "testdatadog.dll" ]