My Dockerfile builds an image copying the content of the current working directory, but always uses the master branch. I would like to specify a branch during the COPY or ADD operation.

FROM node:10.18.0-stretch
EXPOSE 81 8080
RUN apt-get update; \
    apt-get install -y \
    ssh \
    git-core \

COPY ./ /var/www/app_source
WORKDIR /var/www/app_source

RUN npm i npm@latest \
  && npm install \
  && npm install pm2@latest -g

CMD [ "pm2-runtime", "app.js" ]

The above will always build the image using the master branch, whereas I'd like to specify a staging branch. A less elegant solution is to git clone -b staging https://github.com/example/app_source.git and build from that directory which will work given there's no other branch there.

However, I'd like to continue developing on the current master branch but build images using any branch on my local workstation.

Any ideas, responses are appreciated.

  • Docker is not concerned with Git or otherwise. It takes what’s on-disk. – Daniel B Feb 24 '20 at 13:06
  • @DanielB: Thanks for your response. I was afraid that was the case and hoped they’d be a workaround. – Ralph Feb 24 '20 at 13:24

if I understand right, you have copied your git-directory in the image. If so, you should start with the proper git checkout command to bring the proper branch to the front.

git checkout specificbranch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.