prefix or suffix docker container name in Jenkins pipeline shell - docker

I am doing the below docker command from Jenkins pipeline
sh '#!/bin/sh -e\n' + "docker run -i -v $workspace/tests:/root/tests -w /root/tests --name test ${awsAccountNo}.dkr.ecr.${env.AWS_REGION}.amazonaws.com/${imageName}:latest testrun exec test.rb --input-file sample.yml"
where the container get a name of test
I want to make --name somewhat unique, so thought of doing it test-$RANDOM which will then give some random numbers. In an interactive shell session I am getting an output in the terminal. When trying to do that in the shell script it does not seems to work. May be some sort of usage error I think.
Can someone point me how to add suffix or prefix to a container name in docker run command inside a Jenkins shell.
Thank you

$RANDOM is a bash-specific variable, it doesn't work with /bin/sh interpreter.
sh() function uses /bin/sh by default.
Also, you need to escape $ sign for shell variables in double-quotes. To improve code readability you can multiline quotes: ```
So the script string should be
sh "#!/bin/bash ... --name test-\$RANDOM ..."
or
sh ```
#!/bin/bash
docker run -i -v $workspace/tests:/root/tests \
-w /root/tests --name test-\$RANDOM \
// ... rest parameters
```

Related

Docker pass variable without it getting expanded

I want to run echo "tools path is: $TOOLSPATH" in my docker image but make sure the variable doesn't get expanded in my machine and sent to docker. I am not sure how to avoid variable expansion.
docker run -v `pwd`:/root -it --rm foobar echo 'tools path is: $TOOLSPATH'
> tools path is: $TOOLSPATH
docker run -v `pwd`:/root -it --rm foobar echo "tools path is: $TOOLSPATH"
> tools path is:
There is one way to run echo $VAR inside the container and print it in your terminal. You just need to pass an interpreter too.
docker run alpine sh -c 'echo my HOME: $HOME'
my HOME: /root
PS: I used alpine as a test. If sh doesn't work, you can try bash instead.
This might be helpful to you
docker run -v `pwd`:/root -it --rm foobar echo "tools path is: $TOOLSPATH"

Correct way to run a docker script in Jenkins pipeline that runs and quits

I have a docker container that does some FTP activity that takes 3-7 minutes then ends. My Jenkins pipeline snippet looks like this:
stage('Run image'){
sh '''
docker run -i -v \
--mount type=bind,source="$(pwd)/host-dirs,target=/host-dirs" \
chq-ic2e-sprint-images-docker-local.artifactory.swg-devops.com/ssc-cost-file-processor:approved
sh
'''
}
However, docker says
docker: invalid reference format: repository name must be lowercase.
If this were a terminal, I'd use the -it function but that's not applicable here. I want the script to do its thing then end.
For testing purposes, I run it from a terminal like this and it works fine:
#!/bin/bash
docker run -it \
--mount type=bind,source="$(pwd)/host-dirs,target=/host-dirs" \
chq-ic2e-sprint-images-docker-local.artifactory.swg-devops.com/ssc-cost-file-processor:approved sh

How to run a docker run command from Jenkinsfile shell if the command contains a "-"?

Currently I am trying to run the following command using sh from Jenkinsfile
sh "docker run -e key1=${value1} -e key2=\\'run-cli --users ${USERS} --names ${NAMES}\\' -i -t --network host ${DOCKER_HOST}/path/image:tag"
However, it fails with unknown flag: --users each time. It seems like docker isn't treating it as an env variable but instead reading it as a part of its command. I tried every possible combination of quotes and escape sequences but it doesn't work. It runs perfectly fine when running it directly in the console, but fails when running through jenkins. Any workaround to get this working?
You can use the answer I gave here
The idea is to use:
sh ("""
docker run -e key1=${value1} -e key2="run-cli --users ${USERS} --names ${NAMES" -i -t --network host ${DOCKER_HOST}/path/image:tag
""")

Why is the env variable not set when entrypoint is echo on docker run?

Why is the environment variable not visible to the command that is run as the entrypoint?
Examples:
$docker run -it -e "name=JD" --entrypoint 'echo' ubuntu 'Hello $name'
Hello $name
$ docker run -it -e "name=JD" --entrypoint 'echo' ubuntu "Hello $name"
Hello
But when I start the shell the environment variable is there:
$ docker run -it -e "name=JD" ubuntu /bin/bash
root#c3e513390184:/# echo "$name"
JD
Why in the first case with the echo as entrypoint it does not find the env variable set?
First case
docker run -it -e "name=JD" --entrypoint 'echo' ubuntu 'Hello $name'
Single quotation mark always prevents string from variable expansion. Whatever you write in single quotes remains unchanged. Try echo '$PWD' in your terminal and you will see $PWD as the output. Try echo "$PWD" and you will get your working directory printed.
Second case
docker run -it -e "name=JD" --entrypoint 'echo' ubuntu "Hello $name"
Your code is being expanded before running Docker. Your shell expands whole string and then executes it. At this moment you dont have $name declared and you get it empty. That means inside container you get "Hello " command, not "Hello $name".
If you want to echo environment variable from inside container, simplest way is to wrap script into sh-file to prevent its expansion and pass this file to container.
Third case is obvious I guess and doesn't need explanation.

Running a script in Docker container by accepting -e parameter

I have a very simple script called as myscript.sh
echo "this is test " > /tmp/myfile.txt
echo $TEST >> /tmp/myfile.txt
I have stored this script in my disk which i plan to pass it to the container as a volume like this below
docker run -d --name test \
-v /home/docker/test/myscript.sh:/tmp/myscript.sh \
-e TESTING=just-a-test \
test
The Dockerfile looks like this below
FROM ubuntu
CMD ["bash", "/tmp/myscript.sh"]
So the thought process is to get this script executed and get the result as a file myfile.txt which would contain the -e passed.
Instead i am getting
docker#boot2docker:~/test$ docker exec -it test /bin/bash Error
response from daemon: Container test is not running
Which means that this simplest program did not execute as a container.
I Could not figure it out.
The container ran, executed the script, then exited. A container only runs as long as its main process. When that stops, the container stops.
A simpler test would be to change your test script to:
#!/bin/bash
echo $TEST
I would change your Dockerfile to copy the file in and remove the "bash" part of the CMD instruction:
FROM ubuntu
COPY myscript.sh /myscript.sh
CMD /myscript.sh
Now rebuild and run:
$ docker build -t test .
...
$ docker run -e TEST=VAL test
...
The container should echo the value of the test variable and exit. (I haven't tested any of this, so apologies for any mistakes).
The answer to this question is to use the entrypoint instead of cmd.
I did some research and i came up with the solution that looks like this
ENTRYPOINT ["bash", "<script>"]
To run the script just use
docker run -d --name [--privileged] -p : \
- v /script.sh:/tmp/script.sh \
where -v <> : YOU CAN ALSO USE THE WGET TO GET THE SCRIPT LIKE MOST PEOPLE AND EXECUTE AT RUNTIME.
Appreciate all the people who tried to solve the query

Resources