Docker build specific local git branch

2019-08-28 10:20发布

问题:

I am new to docker and would like to containerize a specific git branch of my app.If I run docker build and give the location of my dockerfile, an image will be build but from the local master branch by default.I want to be able to build another branch say "develop".I have done some research and all the answers I found suggest building from a specific remote branch and not a specific local branch.

回答1:

You’d do this the same way you’d build or run any other program, from source, from a non-default branch: check out the branch you want to run locally, then run docker build.

git clone git@github.com:myname/project
cd project
git checkout branchname
docker build \
  -t myname/project:branchname-g$(git rev-parse --short HEAD) \
  .

(I need to do this fairly routinely in my day job: it lets me check out my colleague’s proposed feature branches and actually run them myself. Doing things like this, or trying to build a Docker image of yesterday’s code and not the thing that broke today, tends to be a pretty good reason to not git clone from within your Dockerfile.)