Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we include git commands in docker image?

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?

like image 432
Techie Avatar asked Jun 15 '18 06:06

Techie


2 Answers

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.

like image 148
Alejandro Galera Avatar answered Sep 19 '22 11:09

Alejandro Galera


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.

like image 26
Stunts Avatar answered Sep 17 '22 11:09

Stunts