Can we include git commands in docker image?
Solution 1
Yes, you can. Let me recommend some things about that.
- Define a git token in github associated to a generic user. I like to give only read permissions to that user.
- Declare some ARGs related to git in your Dockerfile, so you can customize your build.
- Add Git installation to your Dockerfile.
- 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.
Related videos on Youtube
Comments
-
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
- Get commited files from current git repo
- COPY myapp/ /app
- 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 almost 6 yearsI need to run
git diff
while running the container to find changed files. -
Alejandro Galera almost 6 yearsHave 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 almost 6 yearsDockerfile 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 almost 6 yearsI 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 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 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 addRUN git diff...
. If you need several, add to your entry point. -
Techie almost 6 years
git diff
is not working in docker , saying not a git repository :-( -
Alejandro Galera almost 6 yearsYou have to set workdir to location where you've done
git clone