How to prevent Dockerfile caching git clone

后端 未结 7 594
忘了有多久
忘了有多久 2020-11-30 08:19

I have a Dockerfile trying to package and deploy a web app to a container. The code of app fetches from git repository during Docker image building. Here\'s the Dockerfile s

7条回答
  •  旧时难觅i
    2020-11-30 09:11

    If you use github you can use github API to not cache specific RUN command. You need to have jq installed to parse JSON: apt-get install -y jq

    Example:

    docker build --build-arg SHA=$(curl -s 'https://api.github.com/repos/Tencent/mars/commits' | jq -r '.[0].sha') -t imageName .
    

    In Dockerfile (ARG command should be right before RUN):

    ARG SHA=LATEST
    RUN SHA=${SHA} \
        git clone https://github.com/Tencent/mars.git
    

    or if you don't want to install jq

    SHA=$(curl -s 'https://api.github.com/repos/Tencent/mars/commits' | grep sha | head -1)
    

    If repository has new commits, git clone will be executed.

提交回复
热议问题