How do you print to console from a docker file during build? - docker

Suppose you have some Dockerfile. What needs to be added to that file such that a string (ie "Hello World") is printed to the console during build?
docker build .
RESEARCH
This question is a top hit in Google for this topic. I have researched by googling and landing here.
WHAT I HAVE TRIED
From the accepted answer:
RUN echo "hello there"
This actually doesn't work.

It's fairly simple actually.
If you just want to print stuff using in the build proccess you could just add the following line to your Dockerfile:
RUN echo "hello there"
And then add these options to your docker build command:
--progress=plain --no-cache
EDIT:
as noted by #SoftwareEngineer, when used for logging or tooling purposes you should append the echo command to the one you want to check if were successful. for example when downloading packages and wanting to get a print statement when finished:
example from nginx official image dockerfile
RUN apt-get install -y whatever && echo "installed package"

Related

Jenkins/Robot Framework - looking for chromedriver but I think it's in PATH

Hey in Jenkins I'm trying to run robot framework tests:
with command python3 robot -d results mytestsuite.robot, and it has some line to open chrome browser, but the message in log shows me typical: WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see...
Everything works ok locally, and I'm not sure which PATH jenkins wants to use so my questions are:
why do I have to input python3 instead of python (with just python in command it tells me that robot is not found)
why chromedriver is not found, and how to set it up (in what PATH and how) to make it work
Is it possible to set jenkins up to use other drivers ex. geckodriver?
My jenkins job env looks like this:
#!/bin/bash
echo $JENKINS_HOME
which python3
echo $PATH
outputs:
/Users/MYUSER/.jenkins
/usr/bin/python3
/Users/MYUSER/.jenkins/tools/chromedriver:/usr/bin:/bin:/usr/sbin:/sbin
Ok so I've fixed it with:
export PATH=/Library/Frameworks/Python.framework/Versions/3.9/bin/:$PATH
this is the location where I have chromedriver locally.
in the build shell execute but is there a way to make it more permanent (I mean not to use it every time it runs build?)

jenkinsci docker install-plugins.sh fails build

I am trying to package the vanilla Jenkins image into Docker using this tutorial: https://github.com/jenkinsci/jenkinsfile-runner/blob/master/DOCKER.md
Everything works until one of the last steps where the Dockerfile tries to run install-plugins.sh from a plugins.txt file that was just copeid into its own directory. This is the error I am getting when running docker build:
/usr/local/bin/install-plugins.sh: line 148: TEMP_ALREADY_INSTALLED: unbound variable
The command '/bin/sh -c /usr/local/bin/install-plugins.sh < /usr/share/jenkins/ref/plugins.txt' returned a non-zero code: 1
Here is my plugins.txt file:
pipeline-model-definition:latest
Just the one line.
I cannot seem to figure out what might fix this issue. I tried using the suggestion from this answer here: https://github.com/jenkinsci/docker/issues/348 but the command line spat out the exact same error as above. Any help is appreciated, thanks in advance.
That variable was defined in plugins.sh (which is deprecated and supposed to be replaced by install-plugins.sh)
# the war includes a # of plugins, to make the build efficient filter out
# the plugins so we dont install 2x - there about 17!
if [ -d "$JENKINS_HOME" ]
then
TEMP_ALREADY_INSTALLED=$JENKINS_HOME/preinstalled.plugins.$$.txt
else
echo "ERROR $JENKINS_HOME not found"
exit 1
fi
But it is not defined in install-plugins.sh, only used (in line 155)
Try and set TEMP_ALREADY_INSTALLED first, as shown above, before calling install-plugins.sh.

jenkins not executing last shell command

I have a CI project in jenkins just to run some tests and send a report via email.
Running it from a docker console(or ubuntu) there is no probelm , it runs the test and send the report via email.
But when i try to run the same command on jenkins it never runs the last command in order to send the report via email.
Here is the command
python3 -m venv . && . bin/activate && pip3 install -r requirements.txt && pytest testcases/HotelesCI/Desktop/HotelPlaya --html=/home/jenkins/hoteles_playa_desktop.html --reruns 2 --self-contained-html --tb=line -k 'test_errors' ; python3 testcases/HotelesCI/Desktop/HotelPlaya/hotels_report.py
Again if i copy and paste into a console there is no probelm
I even try to run the last command in another build shell step , but nothing happens
Am i missing something? there is no output in jenkins for the las command
Solved even i don fully understand why , changing the last concatenation quote from " ; " to " || ", it solves the problem.
As i understand , the first option should run the last command always even on build failure , and my change is telling the system that run it only on failure

Alpine not loading /etc/profile [duplicate]

I'm trying to write (what I thought would be) a simple bash script that will:
run virtualenv to create a new environment at $1
activate the virtual environment
do some more stuff (install django, add django-admin.py to the virtualenv's path, etc.)
Step 1 works quite well, but I can't seem to activate the virtualenv. For those not familiar with virtualenv, it creates an activate file that activates the virtual environment. From the CLI, you run it using source
source $env_name/bin/activate
Where $env_name, obviously, is the name of the dir that the virtual env is installed in.
In my script, after creating the virtual environment, I store the path to the activate script like this:
activate="`pwd`/$ENV_NAME/bin/activate"
But when I call source "$activate", I get this:
/home/clawlor/bin/scripts/djangoenv: 20: source: not found
I know that $activate contains the correct path to the activate script, in fact I even test that a file is there before I call source. But source itself can't seem to find it. I've also tried running all of the steps manually in the CLI, where everything works fine.
In my research I found this script, which is similar to what I want but is also doing a lot of other things that I don't need, like storing all of the virtual environments in a ~/.virtualenv directory (or whatever is in $WORKON_HOME). But it seems to me that he is creating the path to activate, and calling source "$activate" in basically the same way I am.
Here is the script in its entirety:
#!/bin/sh
PYTHON_PATH=~/bin/python-2.6.1/bin/python
if [ $# = 1 ]
then
ENV_NAME="$1"
virtualenv -p $PYTHON_PATH --no-site-packages $ENV_NAME
activate="`pwd`/$ENV_NAME/bin/activate"
if [ ! -f "$activate" ]
then
echo "ERROR: activate not found at $activate"
return 1
fi
source "$activate"
else
echo 'Usage: djangoenv ENV_NAME'
fi
DISCLAIMER: My bash script-fu is pretty weak. I'm fairly comfortable at the CLI, but there may well be some extremely stupid reason this isn't working.
If you're writing a bash script, call it by name:
#!/bin/bash
/bin/sh is not guaranteed to be bash. This caused a ton of broken scripts in Ubuntu some years ago (IIRC).
The source builtin works just fine in bash; but you might as well just use dot like Norman suggested.
In the POSIX standard, which /bin/sh is supposed to respect, the command is . (a single dot), not source. The source command is a csh-ism that has been pulled into bash.
Try
. $env_name/bin/activate
Or if you must have non-POSIX bash-isms in your code, use #!/bin/bash.
In Ubuntu if you execute the script with sh scriptname.sh you get this problem.
Try executing the script with ./scriptname.sh instead.
best to add the full path of the file you intend to source.
eg
source ./.env instead of source .env
or source /var/www/html/site1/.env

How can I create and use a variable inside docker?

I'm trying to get the version from a pom file so that I can use it later in another command. So inside the docker file I have,
RUN VERSION="$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version|grep -Ev '(^\[|Download\w+:)')"
RUN echo $VERSION
But the echo prints nothing. What I actually want to run is,
RUN mv /abc/abc-${VERSION}.jar /abc/abc.jar
Use ENV like that, it's the way Docker devs want you to do it:
ENV VERSION "$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version|grep -Ev '(^\[|Download\w+:)')"
RUN echo $VERSION
There is an example is the docs that's very similar to your case: https://docs.docker.com/engine/reference/builder/#environment-replacement
if you group your 2 lines in one, it will work
RUN VERSION="$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version|grep -Ev '(^\[|Download\w+:)')" ; echo $VERSION
the recommended way is of course the solution proposed by Ilya Peterov, I suspect you want to use it later in your Dockerfile
I will take another example, if you have a script in abc/myscript.sh
RUN cd abc ; ./myscript
will work, while
RUN cd abc
RUN ./myscript
will fail as it will not be in the abc directory

Resources