<p>We're using gitlab for CI/CD.I'll include the script which we're using</p>
<pre><code>services:
  - docker:19.03.11-dind
workflow:
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH == &quot;developer&quot; || $CI_COMMIT_BRANCH == &quot;stage&quot;|| ($CI_COMMIT_BRANCH =~ (/^([A-Z]([0-9][-_])?)?SPRINT(([-_][A-Z][0-9])?)+/i))
      when: always
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH != &quot;developer&quot; || $CI_COMMIT_BRANCH != &quot;stage&quot;|| ($CI_COMMIT_BRANCH !~ (/^([A-Z]([0-9][-_])?)?SPRINT(([-_][A-Z][0-9])?)+/i))
      when: never 
stages:
  - build
  - Publish
  - deploy
cache:
  paths:
    - .m2/repository
    - target

build_jar:
  image: maven:3.8.3-jdk-11
  stage: build
  script: 
    - mvn clean install package -DskipTests=true
  artifacts:
    paths:
      - target/*.jar

docker_build_dev:
  stage: Publish
  image: docker:19.03.11
  services:
    - docker:19.03.11-dind      
  variables:
    IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
  script: 
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build --build-arg environment_name=development -t $IMAGE_TAG .
    - docker push $IMAGE_TAG
  only:
    - /^([A-Z]([0-9][-_])?)?SPRINT(([-_][A-Z][0-9])?)+/i
    - developer

docker_build_stage:
  stage: Publish
  image: docker:19.03.11
  services:
    - docker:19.03.11-dind   
  variables:
    IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
  script: 
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build --build-arg environment_name=stage -t $IMAGE_TAG .
    - docker push $IMAGE_TAG   
  only:
    - stage

deploy_dev:
  stage: deploy
  image: stellacenter/aws-helm-kubectl
  before_script:
    - aws configure set aws_access_key_id ${DEV_AWS_ACCESS_KEY_ID}
    - aws configure set aws_secret_access_key ${DEV_AWS_SECRET_ACCESS_KEY}
    - aws configure set region ${DEV_AWS_DEFAULT_REGION}
  script:
    - sed -i &quot;s/&lt;VERSION&gt;/${CI_COMMIT_SHORT_SHA}/g&quot; patient-service.yml     
    - mkdir -p  $HOME/.kube
    - cp $KUBE_CONFIG_DEV $HOME/.kube/config
    - chown $(id -u):$(id -g) $HOME/.kube/config 
    - export KUBECONFIG=$HOME/.kube/config
    - kubectl apply -f  patient-service.yml -n ${KUBE_NAMESPACE_DEV}
  only:
    - /^([A-Z]([0-9][-_])?)?SPRINT(([-_][A-Z][0-9])?)+/i
    - developer

deploy_stage:
  stage: deploy
  image: stellacenter/aws-helm-kubectl
  before_script:
    - aws configure set aws_access_key_id ${DEV_AWS_ACCESS_KEY_ID}
    - aws configure set aws_secret_access_key ${DEV_AWS_SECRET_ACCESS_KEY}
    - aws configure set region ${DEV_AWS_DEFAULT_REGION}
  script:
    - sed -i &quot;s/&lt;VERSION&gt;/${CI_COMMIT_SHORT_SHA}/g&quot; patient-service.yml    
    - mkdir -p  $HOME/.kube
    - cp $KUBE_CONFIG_STAGE $HOME/.kube/config
    - chown $(id -u):$(id -g) $HOME/.kube/config 
    - export KUBECONFIG=$HOME/.kube/config
    - kubectl apply -f  patient-service.yml -n ${KUBE_NAMESPACE_STAGE}
  only:
    - stage
</code></pre>
<p>According to the script, We just merged the script not to face conflicts/clashes for stage and development while deployment. Previously , we having each docker files for each environment. Now I want to merge the dockerfile also , I merged, but the dockerfile is not fetching. Having clashes in kubernetes. I don't know about kubernetes . I'll enclosed the docker file which I merged.</p>
<pre><code>FROM maven:3.8.3-jdk-11 AS MAVEN_BUILD
COPY pom.xml /build/
COPY src /build/src/
WORKDIR /build/
RUN mvn clean install package -DskipTests=true
FROM openjdk:11
ARG environment_name 
WORKDIR /app
COPY --from=MAVEN_BUILD /build/target/patient-service-*.jar /app/patient-service.jar
ENV PORT 8094
ENV env_var_name=$environment_name
EXPOSE $PORT
ENTRYPOINT [&quot;java&quot;,&quot;-Dspring.profiles.active= $env_var_name&quot;,&quot;-jar&quot;,&quot;/app/patient-service.jar&quot;]
</code></pre>
<p>the last line , we used before ,</p>
<pre><code>ENTRYPOINT [&quot;java&quot;,&quot;-Dspring.profiles.active=development&quot;,&quot;-jar&quot;,&quot;/app/patient-service.jar&quot;]
</code></pre>
<p>at the time, its working fine, I'm not facing any issue on kubernetes. I'd just add  environment variable to fetch along with whether development or stage. You can check  ,my script after the docker build. After adding the varaible only , we facing the clashes. Please help me to sort it out this. Thanks in advance</p>

Cyril I
<p>As already mentioned in <a href="https://stackoverflow.com/a/37904830/13097422">the question</a> in the comment section, you would need to execute a shell form, because the exec form won't do directly do variable substitution.</p>
<pre><code>ENTRYPOINT [&quot;java&quot;,&quot;-Dspring.profiles.active= $env_var_name&quot;,&quot;-jar&quot;,&quot;/app/patient-service.jar&quot;]
</code></pre>
<p>needs to be</p>
<pre><code>ENTRYPOINT [ &quot;sh&quot;, &quot;-c&quot;, &quot;java&quot;,&quot;-Dspring.profiles.active=$env_var_name&quot;,&quot;-jar&quot;,&quot;/app/patient-service.jar&quot; ]
</code></pre>
<p>Relevant documentation from Docker Docs: <a href="https://docs.docker.com/engine/reference/builder/#exec-form-entrypoint-example" rel="nofollow noreferrer">Shell form ENTRYPOINT example</a></p>
<blockquote>
<p>Unlike the <em>shell</em> form, the <em>exec</em> form does not invoke a command shell. This means that normal shell processing does not happen. For example, <code>ENTRYPOINT [ &quot;echo&quot;, &quot;$HOME&quot; ]</code> will not do variable substitution on <code>$HOME</code>. If you want shell processing then either use the <em>shell</em> form or execute a shell directly, for example: <code>ENTRYPOINT [ &quot;sh&quot;, &quot;-c&quot;, &quot;echo $HOME&quot; ]</code>. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.</p>
</blockquote>
