I am making a script to show, for each of the dockers that I have set up on my machine, which contain various applications managed as git clones, the last commit and the author of this one. At the moment I have made a loop that runs through each one of the dockers, with:
for a in $(docker ps -aq); do
...
done
now the idea is to run the git command inside this loop:
git log -1 --pretty=format:\"%h-%an\"
The problem I have is that this command, although it shows only the last line and with the format that I want, it waits for the pagination of the git command, so until I do ctrl^C, it stays waiting in the command , in each of the docker.
I have tried with:
git -c pager.diff=false log -1 --pretty=format:"%h-%an"
In theory, with turning off pager.diff at run time, it shouldn't wait for said pager (since it's only one line, the last one), but it stays the same (it seems to "ignore" that I add pager.diff)
Currently the command I have (which stops due to the pagination problem) is this:
for contenedor in $(docker ps -aq); do
$comando="git -c pager.diff=false log -1 --pretty=format:\"%h-%an\""
docker exec -ti $contenedor $comando
done
I had to add the \ in the quotes, because format requires the format string to be in quotes, and because I store the command in a variable, and then execute it with docker exec.
Does anyone come up with something, or maybe there is some other way to achieve this???
Answer 1 To avoid pagination you can use the command:
ref
ANSWER 2
In the end we use the tools according to our needs, making a git clone inside a container does not have to be wrong. And neither do you run commands inside the containers (I only do it for debugging purposes).
Please note the following:
I will tell you how I put the code of my application and how I access the git version:
In my repository where I have the application code I have the code of how to deploy it in the infrastructure (Following the practices of Infrastructure as code). The bare minimum I have next to the application code is the Dockerfile.
An example Dockerfile:
Notice the ARG, ENV and LABEL layers
docker build -t mi_imagen --build-arg GIT_COMMIT=$(git log -1 --format=%h) .
ENV will create an environment variable inside the container called GIT_COMMIT with the value of ARG. My application has access to that variable, and could return that value to a REST request.
LABEL will put that information in the metadata of the docker image. So you will have access to that information with the docker inspect container_name command . Your script can use this option. (or make api rest queries to your containers with the previous option :-P)