Unable to pass Variables to Dockerfile in Cloud Run

Viewed 188

Trying to pass runtime Environment variable from Cloud Run > Variables Environment variables:

Below is Dockerfile. I tried to pass through ARG through CONTAINER tab CONTAINER arguments as well

In both cases echo in Dockerfile didn't receive those parameters in build log.

Container is NGINX instance with React build output. App itself is loading fine, but i am not able to pass api url to NGINX proxy_pass directive in nginx.conf file

--set-env-vars=api_service_url=https://apiserverurl is set as well, it shows in the console, but Dockerfile is not getting that value.

Here is my Dockerfile:

# build environment
FROM node:10-alpine as react-build
WORKDIR /app
COPY . ./

# server environment

FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/configfile.template
ENV PORT 3000
ENV HOST 0.0.0.0
ARG api_service_url
RUN echo "========api_service_url========="$api_service_url
ENV API_SERVICE_URL $api_service_url
RUN echo "========API_SERVICE_URL========="$API_SERVICE_URL
RUN sh -c "envsubst '\$PORT \$API_SERVICE_URL' < /etc/nginx/conf.d/configfile.template > /etc/nginx/conf.d/default.conf"
#RUN sh -c "envsubst  < /etc/nginx/conf.d/configfile.template > /etc/nginx/conf.d/default.conf"
RUN cat /etc/nginx/conf.d/default.conf

COPY --from=react-build /app/webapp /usr/share/nginx/html

EXPOSE 3000
CMD ["nginx", "-g", "daemon off;"]

How can I pass the build arg to the container?

3 Answers

ARG is provided and used at the time of container image build. It has nothing to do with Cloud Run.

You can use environment variables on Cloud Run, and change your ENTRYPOINT to a script that does the necessary modifications based on the given environment variable in the runtime.

The cloud run variable are runtime variable, they are injected in the container, as environment variables and they are available to your code.

changing Dockerfile with this command seems to work

CMD sh -c "envsubst '$PORT $API_SERVICE_URL' < /etc/nginx/conf.d/site.template > /etc/nginx/conf.d/site.conf && exec nginx -g 'daemon off;'"

earlier it is split into 2 commands RUN sh -c "envsubst '$PORT $API_SERVICE_URL' < /etc/nginx/conf.d/configfile.template > /etc/nginx/conf.d/default.conf" CMD ["nginx", "-g", "daemon off;"]

i think substitutions were not happening before nginx starts as a result it gives error.

Now problem is resolved. thanks for your answers

Related