Can we include git commands in docker image?

17,061

Solution 1

Yes, you can. Let me recommend some things about that.

  1. Define a git token in github associated to a generic user. I like to give only read permissions to that user.
  2. Declare some ARGs related to git in your Dockerfile, so you can customize your build.
  3. Add Git installation to your Dockerfile.
  4. Do git clone cloning only needed folders, not the whole repo.

So, your Dockerfile could be, for example for debian/ubuntu:

FROM <your linux distribution>
ARG GIT_USER
ARG GIT_TOKEN
ARG GIT_COMMIT
ARG SW_VERSION
RUN apt-get update
RUN apt-get -y install git
RUN git clone -n https://${GIT_USER}:${GIT_TOKEN}@github.com/<your git>/your_git.git --depth 1 && cd <your_git> && git checkout ${GIT_COMMIT} <dir_you_want_to_checkout>
COPY myapp/ /app
CMD /app/entry.pl /<your_git>/<dir_you_want_to_checkout>/...

Then, you can build as you know with:

docker build -t --build-arg GIT_USER=<your_user> --build-arg GIT_TOKEN=d0e4467c63... --build-arg GIT_COMMIT=a14dc9f454... <your_image_name> .

Good luck.

Solution 2

If you want a minimal docker image where you can use a git command I can recommend using gitlab/gitlab-runner:alpine from here as your image.

At the time of writing this image comes at 40Mb, which is close to the smallest I can picture that has git available. This will skip you some time in installing git using a package manager.

The image itself seem to be frequently maintained by the gitlab folks.

Share:
17,061

Related videos on Youtube

Techie
Author by

Techie

Java, Linux, Perl.

Updated on October 12, 2022

Comments

  • Techie
    Techie over 1 year

    I am new to docker. I would like to create an image which will run as part of my project build to check commited files.

    My requirement is, Docker file should have these statements

    1. Get commited files from current git repo
    2. COPY myapp/ /app
    3. CMD /app/entry.pl <git files> (as command line arg)

    I would like to create an image for this process.

    I should get the git commited files list from current local repo and pass those files to my application to scan those files.

    While building my image I just need to copy my application but while running the container I need to run git diff to find changed files and pass that to my application as arguments.

    Is it possible to run git diff by docker container?

    UPDATE:

    I tried to execute git diff on current repo from docker but it is saying not a git repository. I am running my docker images as part of continuous integration in gitlab yml file.

    Please help me to achieve this requirement?

  • Techie
    Techie almost 6 years
    I need to run git diff while running the container to find changed files.
  • Alejandro Galera
    Alejandro Galera almost 6 years
    Have you tried to execute git diff and it doesn't work? I don't understand why my proposal is not valid to you. When you install git you have all its commands.
  • Techie
    Techie almost 6 years
    Dockerfile should have only one CMD command right. So, how can I run both git diff and my application ? This is what I am trying to achieve . I am checking with Docker-compose. I started working on Docker just a week ago. So, I am trying to explore things.
  • ntwrkguru
    ntwrkguru almost 6 years
    I would put all of that logic into the perl script, but if you don't want to do that, then make the git diff as a RUN statement.
  • Techie
    Techie almost 6 years
    git diff should be executed by container whereas RUN is executed as part of build right ? correct me if I am wrong.
  • Alejandro Galera
    Alejandro Galera almost 6 years
    @Rekha, it depends on how many times do you want to execute git diff during container live cycle. If it's only once, just add RUN git diff.... If you need several, add to your entry point.
  • Techie
    Techie almost 6 years
    git diff is not working in docker , saying not a git repository :-(
  • Alejandro Galera
    Alejandro Galera almost 6 years
    You have to set workdir to location where you've done git clone