How can echo the .env.list file in docker run command in jenkins - bash

I would like to echo out the .env.list values used in the docker run command in Jenkins, I have tried as follows echo "Print the env.list file: ${.env.list}"
Could someone please advise how can I print the .env.list file in docker run command
sh """
docker run --env-file bookpro-co/.env.list ${baseUrlConfig} \
-v \"${ARTEFACT_DIR}:/artefacts\" \
-v \"${env.WORKSPACE}/bookpro-co:/bookpro-test\" \
cypress:latest \
/node_modules/.bin/cypress-tags ${cypressArgs}
echo "Print the env.list file: ${.env.list}"
"""

Related

Setting Docker environment variables in Azure DevOps using bash task

I am having a problem setting up an environment variable as part of Releasing my container. in Azure DevOps, I have a bash task in which I am trying to set an environment variable (CUSER)
export CUSER=$(CUSER) && \
echo $(MYPASS) | sudo -S docker run \
-e CUSER \
--name $(CNAME) \
-p 80:80 $(INAME):$(Build.BuildId) &
The container runs but the environment variable is not set. But it is set when I execute the script directly on the host like export CUSER="Dev9" && docker run -e CUSER --name demo1 -p 80:80 myimage:256
I suspect there is a problem with the way my command is formatted but I am not sure where or what.
Resolved it by changing how I authenticate sudo.
sudo -S <<< $(MYPASS) echo export CUSER=$(CUSER) && \
docker run -e CUSER \
--name $(CNAME) \
-p 80:80 $(INAME):$(Build.BuildId) &

Run a Docker Container from a Desktop File Without Terminal GUI?

I have a couple of Docker images I've built for this and that;one for scanner program, another for a browser etc. Once I had them working, I created .desktop files that execute a bash run scripts I've created to run a container with them.
My question is: is there a way to run the .desktop file without the terminal GUI showing up? I've tried a couple of approaches with no success.
For instance, I've tried:
[Desktop Entry]
Name=gscan2pdf
Icon=gscan2pdf.png
Exec=gnome-terminal -e
"/home/hildy/Documents/repos/docker/gscan2pdf/run_gscan.sh"
Type=Application
Terminal=false
As well as:
[Desktop Entry]
Name=gscan2pdf
Icon=gscan2pdf.png
Exec="/home/hildy/Documents/repos/docker/gscan2pdf/run_gscan.sh"
Type=Application
Terminal=true
Both of these execute the scripts just fine of course, I'd just like it better if the application launched without a terminal GUI launching first.
Host System:
CentOS 7 - Gnome 3 Desktop
One example of a run script:
#!/bin/bash
HOST_UID=$(id -u)
HOST_GID=$(id -g)
XSOCK=/tmp/.X11-unix &&
XAUTH=/tmp/.docker.xauth &&
touch $XAUTH &&
xauth nlist :0 | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge - &&
#These are only run the first time a container is run from the image
#docker run -e NEW_USER="${USER}" -e NEW_UID="${HOST_UID}" -e
#NEW_GID="${HOST_GID}" hildy/gscan2pdf:v1
#LAST_CONTAINER=$(docker ps -lq) &&
#docker commit "${LAST_CONTAINER}" hildy/gscan2pdf:v1
docker run \
-ti \
--user $USER \
--privileged \
-v /dev/bus/usb:/dev/bus/usb \
-v $XAUTH:$XAUTH -v $XSOCK:$XSOCK -v /home/$USER:/home/$USER \
-e XAUTHORITY=$XAUTH -e DISPLAY \
--entrypoint "" hildy/gscan2pdf:v1 gscan2pdf &>/dev/null
I have found an answer to my question. The issue was that the command to run the container contained the -i option for an interactive terminal. #sneep was correct in the comments to the question when he said "It should work with Terminal=false." His technique to add a line to the script to create a log file is also a great technique, which I will certainly use in the future and it helped me to diagnose the issue.
I can also confirm that replacing -it with -d for detached mode, as suggested by #Oleg Skylar, works.
Amended Docker command for the run script:
docker run \
-t \
--user $USER \
--privileged \
-v /dev/bus/usb:/dev/bus/usb \
-v $XAUTH:$XAUTH -v $XSOCK:$XSOCK -v /home/$USER:/home/$USER \
-e XAUTHORITY=$XAUTH -e DISPLAY \
--entrypoint "" hildy/gscan2pdf:v1 gscan2pdf &>/dev/null
Amended .desktop file:
[Desktop Entry]
Name=gscan2pdf
Icon=gscan2pdf.png
Exec=/home/hildy/Documents/repos/docker/gscan2pdf/run_gscan.sh
Type=Application
Terminal=false
StartupNotify=true

Why does my bash script move beyond "docker run" even tho docker is still running?

I am trying with a bash script to run a docker container then print a message. However the finished message is executed whilst the container is still running - I can exec into it and see PID 1 and multiple other processes.
How can I force the docker run command to complete first?
docker run --name registr \
-v ~/v1:/v1 \
-v ~/logging.yaml:/root/logging.yaml \
-v ~/.aws:/root/.aws \
-v ~/luigi.cfg:/root/luigi.cfg \
-v ~/params:/root/params \
-p 8082:8082 \
simonm3/registr
echo "docker finished"
The docker image has CMD ["python", "/root/worker/start.py"]

docker: invalid reference format in shell script

I'm trying to create a shell script to run a docker container and am struggling. My script is like this:
#!/bin/bash
if [ "$1" == "" ]; then
echo "Usage > run.sh IMAGE NAME"
echo
echo "i.e. ./build.sh cd2:0.0.49"
exit
fi
echo $1
docker run -it --rm \
-e NODE_PATH='./src'\
-e NODE_HOST='0.0.0.0'\
-e NODE_ENV='production'\
-e DOCKER=true\
-e PORT='8080'\
-e STAGING=true\
-e SENDGRID_API_KEY='<redacted>'\
-p 8080:8080 $1
When I run: bash run.sh cd2:0.0.50
I get: docker: invalid reference format: repository name must be lowercase.
Even if I do bash run.sh cd:0.0.50 it still fails (echo $1 results in cd2:0.0.50).
If I run docker run -it --rm -p 8080:8080 cd2:0.0.50 from the command line it works...
Can anyone help?
docker run \
-e NODE_PATH='./src' \
-e NODE_HOST='0.0.0.0' \
-e NODE_ENV='production' \
-e DOCKER=true \
-e PORT='8080' \
-e STAGING=true \
-e SENDGRID_API_KEY='<redacted>' \
-p 8080:8080 --rm -it $1
The image name should be immediately after the -it parameter and so re arrange your run command.

docker run -i -t image /bin/bash - source files first

This works:
# echo 1 and exit:
$ docker run -i -t image /bin/bash -c "echo 1"
1
# exit
# echo 1 and return shell in docker container:
$ docker run -i -t image /bin/bash -c "echo 1; /bin/bash"
1
root#4c064f2554de:/#
Question: How could I source a file into the shell? (this does not work)
$ docker run -i -t image /bin/bash -c "source <(curl -Ls git.io/apeepg) && /bin/bash"
# content from http://git.io/apeepg is sourced and shell is returned
root#4c064f2554de:/#
In my case, I use RUN source command (which will run using /bin/bash) in a Dockerfile to install nvm for node.js
Here is an example.
FROM ubuntu:14.04
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
...
...
RUN source ~/.nvm/nvm.sh && nvm install 0.11.14
I wanted something similar, and expanding a bit on your idea, came up with the following:
docker run -ti --rm ubuntu \
bash -c 'exec /bin/bash --rcfile /dev/fd/1001 \
1002<&0 \
<<<$(echo PS1=it_worked: ) \
1001<&0 \
0<&1002'
--rcfile /dev/fd/1001 will use that file descriptor's contents instead of .bashrc
1002<&0 saves stdin
<<<$(echo PS1=it_worked: ) puts PS1=it_worked: on stdin
1001<&0 moves this stdin to fd 1001, which we use as rcfile
0<&1002 restores the stdin that we saved initially
You can use .bashrc in interactive containers:
RUN curl -O git.io/apeepg.sh && \
echo 'source apeepg.sh' >> ~/.bashrc
Then just run as usual with docker run -it --rm some/image bash.
Note that this will only work with interactive containers.
I don't think you can do this, at least not right now. What you could do is modify your image, and add the file you want to source, like so:
FROM image
ADD my-file /my-file
RUN ["source", "/my-file", "&&", "/bin/bash"]

Resources