change198
change198

Reputation: 2085

how to pass arguments to docker run command specific to few scripts but not to entrypoint

I have created docker file like below,

FROM XXXXXX.dkr.ecr.eu-west-1.amazonaws.com/baseImage:1.0.0

ARG USER_NAME
ARG PASSWD

COPY --chown=sdc:sdc libs/mysql-connector-java-8.0.11.jar  ${SDC_HOME}/datacollector-jdbc-lib/lib/

#REPLACE CONFIG FILLE
COPY policies/config.properties "${SDC_CONF}"/config.properties

#######################################
# COPY  PIPELINE
#######################################

COPY --chown=sdc:sdc pipelines/fullload/sns.json  "${SDC_HOME}"/

COPY --chown=sdc:sdc postPipeline.sh /
RUN chmod 755 /postPipeline.sh

# Calling the script to post pipeline with argument1: pipeline name
#                    and argument2: pipeline filename
#                    and argument3: username(permission to deploy the pipeline)
#                    and argument3: password of above username
FROM base
RUN sed -i "/^exec.*/i \/postPipeline.sh  'fullload' 'sns.json' ${USER_NAME} ${PASSWD} &"  /docker-entrypoint.sh

#######################################

USER ${SDC_USER}
EXPOSE PORT

ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["dc", "-exec"]

This works fine, if I hardcode the value. But My question is, how can I pass the arguments during run command so that, postPipeline.sh will be impacted by those arguments and not the entrypoint.

docker run -p port:port buildname <username> <passwd> 

Because as per docker runbook, these arguments will override the current entrypoint parameter. which I don't want.

Hence, to preserve this as arguments I tried below, but not this is not working as I wanted to pass the arguments in run time not build time.

    FROM alpine:latest as base
    ARG USER_NAME
    ARG PASSWD

    ENV USER_NAME=${USER_NAME}
    ENV PASSWD=${PASSWD}

    FROM XXXXXX.dkr.ecr.eu-west-1.amazonaws.com/baseImage:1.0.0



        COPY --chown=sdc:sdc libs/mysql-connector-java-8.0.11.jar  ${SDC_HOME}/datacollector-jdbc-lib/lib/

        #REPLACE CONFIG FILLE
        COPY policies/config.properties "${SDC_CONF}"/config.properties

        #######################################
        # COPY  PIPELINE
        #######################################

        COPY --chown=sdc:sdc pipelines/fullload/sns.json  "${SDC_HOME}"/

        COPY --chown=sdc:sdc postPipeline.sh /
        RUN chmod 755 /postPipeline.sh

        # Calling the script to post pipeline with argument1: pipeline name
        #                    and argument2: pipeline filename
        #                    and argument3: username(permission to deploy the pipeline)
        #                    and argument3: password of above username
        FROM base
        RUN sed -i "/^exec.*/i \/postPipeline.sh  'fullload' 'sns.json' ${USER_NAME} ${PASSWD} &"  /docker-entrypoint.sh

        #######################################

        USER ${SDC_USER}
        EXPOSE PORT

        ENTRYPOINT ["/docker-entrypoint.sh"]
        CMD ["dc", "-exec"]

creating multistage just for arguments to me looks like far stretched. So, is there any advise how to achieve this?

Upvotes: 1

Views: 165

Answers (2)

Mafor
Mafor

Reputation: 10711

RUN ... postPipeline.sh is executed in build time so obviously you cannot pass arguments to it on run time. What you could do, is wrapping the calls to the postPipeline.sh and docker-entrypoint.sh in another script and calling it in the ENTRYPOINT.

Upvotes: 0

Shashank V
Shashank V

Reputation: 11243

You can pass configuration to docker containers in multiple ways -

  1. Command line parameters

  2. Environment variables

  3. Mounting configuration files

For your use case, I understand docker-entrypoint.sh will call postPipeline.sh internally.

If you have access to modify docker-entrypoint.sh, you can make it take the arguments required for postPipeline.sh as well and pass them on internally.

If you have access to modify postPipeline.sh, you can make it read the configuration from environment variables.

You can mount a configuration file into the container and modify either docker-entrypoint.sh or postPipeline.sh to read the arguments from config file.

Upvotes: 1

Related Questions