Home > Net >  Expand ARG/ENV in CMD dockerfile
Expand ARG/ENV in CMD dockerfile

Time:09-20

I have a Dockerfile and I am taking in a LAMBDA_NAME from a jenkins pipeline.

I am passing in something like this: source-producer

And I want to call the handler of this function, which is named handler in the code.

This code does not work

ARG LAMBDA_NAME
ENV LAMBDA_HANDLER="${LAMBDA_NAME}.handler"
RUN echo "${LAMBDA_HANDLER}"
CMD [ "${LAMBDA_HANDLER}" ]

The result of the run echo step gives back "sourceproducer.handler", which is correct.

The code above produces this error [ERROR] Runtime.MalformedHandlerName: Bad handler '${LAMBDA_HANDLER}': not enough values to unpack (expected 2, got 1)

But, when this same value is hardcoded, it works fine and executes the lambda function.

ARG LAMBDA_NAME
ENV LAMBDA_HANDLER="${LAMBDA_NAME}.handler"
RUN echo "${LAMBDA_HANDLER}"
CMD [ "sourceproducer.handler" ]

How can I correctly use LAMBDA_HANDLER inside of CMD so that the top code block executes the same as the one below it?

CodePudding user response:

You need to use the shell form of the CMD statement. With the exec form of the statement, as you have now, there's no shell to replace environment variable.

Use

CMD "${LAMBDA_HANDLER}"

instead.

This is equivalent to this, using the exec form, which you can also use, if you prefer the exec form

CMD [ "/bin/sh", "-c", "${LAMBDA_HANDLER}" ]
  • Related