Iterating bash commands inside docker container - bash

I am a beginner to Docker and was trying to optimize the following bash shell code. The below code continuously has to create a docker container and remove it. What I would like is a one-time creation of the docker container; then based on the if-else condition, the corresponding command gets iteratively executed in while loop and finally, the docker container gets deleted. I have tried to play around with docker entrypoint commands but all of them seem to insist on a single line command with docker run. Some help will be deeply appreciated! Thanks!
#!/bin/bash
FILE=$1
flag=1
while read line; do
#echo $line
#echo $flag
if [ $flag -gt 0 ] && [ $flag -lt 23189 ]; then
docker run --rm -v $(pwd):/eric -w /eric --entrypoint /bin/sh fedora -c "dnf info $line | tee -a manifest-fed:Core.txt; dnf repoquery --srpm $line"
((++flag))
elif [ $flag -gt 23188 ] && [ $flag -lt 46379 ]; then
docker run --rm -v $(pwd):/eric -w /eric --entrypoint /bin/sh fedora -c "dnf info $line | tee -a manifest-fed:Old.txt; dnf info $line"
((++flag))
elif [ $flag -gt 46378 ] && [ $flag -lt 69571 ]; then
docker run --rm -v $(pwd):/eric -w /eric --entrypoint /bin/sh fedora -c "dnf info $line | tee -a manifest-fed:Graphics.txt"
((++flag))
fi
done<"$1"

Create container in detached mode:
docker run -d -t --rm --name my_container -v $(pwd):/eric -w /eric fedora
Run your commands in this container:
docker exec my_container sh -c "echo test"

You may wrap those commands in a single script and put it in your pwd mapped to '/eric' volume. And then execute them something like:
docker run --rm -v $(pwd):/eric -w /eric --entrypoint /bin/sh fedora -c "/eric/dnf-info.sh 1 '$line'"
Script dnf-info.sh may be:
#! /bin/sh
case $1 in
1)
dnf info "$2" | tee -a manifest-fed:Core.txt; dnf repoquery --srpm "$2"
;;
2)
dnf info "$2" | tee -a manifest-fed:Old.txt; dnf info "$2"
;;
3)
dnf info "$2" | tee -a manifest-fed:Graphics.txt
;;
esac

Related

How do I prompt for Yes/No in a Linux shell script? [duplicate]

This question already has answers here:
How do I prompt for Yes/No/Cancel input in a Linux shell script?
(37 answers)
Closed 2 years ago.
#!/bin/bash
docker rm -f $(docker ps -qa)
docker rmi -f $(docker images -q)
docker volume rm $(docker volume ls -q)
sudo docker run -d --restart=unless-stopped -p 9090:80 -p 9091:443 --privileged -v /opt/rancher:/var/lib/rancher --name=rancher_docker_server rancher/rancher:latest
I want in this part, before sudo docker a question arises whether to continue?
Try this:
echo -n "Do you want to continue [Y/n]: "; read choice
if [ $choice == 'Y' ] || [ $choice == 'y' ]; then
sudo docker run -d --restart=unless-stopped -p 9090:80 -p 9091:443 --privileged -v /opt/rancher:/var/lib/rancher --name=rancher_docker_server rancher/rancher:latest
fi

Bash Script fails with error: OCI runtime exec failed

I am running the below script and getting error.
#!/bin/bash
webproxy=$(sudo docker ps -a --format "{{.Names}}"|grep webproxy)
webproxycheck="curl -k -s https://localhost:\${nginx_https_port}/HealthCheckService"
if [ -n "$webproxy" ] ; then
sudo docker exec $webproxy sh -c "$webproxycheck"
fi
Here is my docker ps -a output
$sudo docker ps -a --format "{{.Names}}"|grep webproxy
webproxy-dev-01
webproxy-dev2-01
when i run the command individually it works. For Example:
$sudo docker exec webproxy-dev-01 sh -c 'curl -k -s https://localhost:${nginx_https_port}/HealthCheckService'
HEALTHCHECK_OK
$sudo docker exec webproxy-dev2-01 sh -c 'curl -k -s https://localhost:${nginx_https_port}/HealthCheckService'
HEALTHCHECK_OK
Here is the error i get.
$ sh healthcheck.sh
OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: \"webproxy-dev-01\": executable file not found in $PATH": unknown
Could someone please help me with the error. Any help will be greatly appreciated.
Because the variable contains two tokens (on two separate lines) that's what the variable expands to. You are running
sudo docker exec webproxy-dev-01 webproxy-dev2-01 ...
which of course is an error.
It's not clear what you actually expect to happen, but if you want to loop over those values, that's
for host in $webproxy; do
sudo docker exec "$host" sh -c "$webproxycheck"
done
which will conveniently loop zero times if the variable is empty.
If you just want one value, maybe add head -n 1 to the pipe, or pass a more specific regular expression to grep so it only matches one container. (If you have control over these containers, probably run them with --name so you can unambiguously identify them.)
Based on your given script, you are trying to "exec" the following
sudo docker exec webproxy-dev2-01
webproxy-dev-01 sh -c "curl -k -s https://localhost:${nginx_https_port}/HealthCheckService"
As you see, here is your error.
sudo docker exec webproxy-dev2-01
webproxy-dev-01 [...]
The problem is this line:
webproxy=$(sudo docker ps -a --format "{{.Names}}"|grep webproxy)
which results in the following (you also posted this):
webproxy-dev2-01
webproxy-dev-01
Now, the issue is, that your docker exec command now takes both images names (coming from the variable assignment $webproxy), interpreting the second entry (which is webproxy-dev-01 and sepetrated by \n) as the exec command. This is now intperreted as the given command which is not valid and cannot been found: That's what the error tells you.
A workaround would be the following:
webproxy=$(sudo docker ps -a --format "{{.Names}}"| grep webproxy | head -n 1)
It only graps the first entry of your output. You can of course adapt this to do this in a loop.
A small snippet:
#!/bin/bash
webproxy=$(sudo docker ps -a --format "{{.Names}}"| grep webproxy )
echo ${webproxy}
webproxycheck="curl -k -s https://localhost:\${nginx_https_port}/HealthCheckService"
while IFS= read -r line; do
if [ -n "$line" ] ; then
echo "sudo docker exec ${line} sh -c \"${webproxycheck}\""
fi
done <<< "$webproxy"

bash script hangs wihout obvious reason

I've got this bash script that builds a Docker image and kicks off a set of tests on it:
#!/bin/bash
fails=''
inspect() {
if [ $1 -ne 0 ]; then
fails="${fails} $2"
fi
}
docker-compose up -d --build
docker-compose exec -T users python manage.py test
inspect $? users
docker-compose exec -T users flake8 project
inspect $? users-lint
docker-compose exec -T client npm test -- --coverage
inspect $? client
docker-compose down
docker-compose -f docker-compose-prod.yml up -d --build
docker-compose -f docker-compose-prod.yml exec -T users python manage.py recreate_db
./node_modules/.bin/cypress run --config baseUrl=http://localhost
inspect $? e2e
docker-compose -f docker-compose-prod.yml down
if [ -n "${fails}"]; then
echo "Test failed: ${fails}"
exit 1
else
echo "Tests passed !"
exit 0
fi
but it won't exit out when finished. It just hangs. What can I do to troubleshoot this ?

How to docker-compose run in windows?

How to use this command in windows 10 familly :
docker-compose run api composer install --no-interaction
Example:
docker-compose run api composer install --no-interaction
- Interactive mode is not yet supported on Windows.
Please pass the -d flag when using `docker-compose run`.
Is it possible ?
Do you have an example ?
The interactive mode support for docker-compose on Windows is tracked by issue 2836 which proposes some alternatives:
Use bash from within the container:
docker exec -it MY_CONTAINER bash
Use a docker-compose-run script by Rodrigo Baron:
Script ( put the function in ~/.zshrc or ~/.bashrc in a Windows git bash shell for instance):
#!/bin/bash
function docker-compose-run() {
if [ "$1" = "-f" ] || [ "$1" = "--file" ] ; then
docker exec -i $(docker-compose -f $2 ps $3 |grep -m 1 $3 | cut -d ' ' -f1) "${#:4}"
else
docker exec -i $(docker-compose ps $1 | grep -m 1 $1 | cut -d ' ' -f1) "${#:2}"
fi
}
docker-compose-run "$#"
Usage:
usage:
docker-compose-run web rspec
# or:
docker-compose-run -f docker-compose.development.yml web rspec
Simpler alternative is to use option -d and to get logs
docker-compose run -rm <service> <command>
is replaced by:
docker-compose-run <service> <command>
For this to work, add this snippet in your ~/.bashrc :
docker-compose-run() {
CONTAINER_NAME=$(docker-compose run -d $#)
docker logs -f $CONTAINER_NAME
docker rm $CONTAINER_NAME
}

How to detect fully interactive shell in bash from docker?

I'm wanting to detect in "docker run" whether -ti has been passed to the entrypoint script.
docker run --help for -t -i
-i, --interactive=false Keep STDIN open even if not attached
-t, --tty=false Allocate a pseudo-TTY
I have tried the following but even when tested locally (not inside docker) it didn't work and printed out "Not interactive" always.
#!/bin/bash
[[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive'
entrypoint.sh:
#!/bin/bash
set -e
if [ -t 0 ] ; then
echo "(interactive shell)"
else
echo "(not interactive shell)"
fi
/bin/bash -c "$#"
Dockerfile:
FROM debian:7.8
COPY entrypoint.sh /usr/bin/entrypoint.sh
RUN chmod 755 /usr/bin/entrypoint.sh
ENTRYPOINT ["/usr/bin/entrypoint.sh"]
CMD ["/bin/bash"]
build the image:
$ docker build -t is_interactive .
run the image interactively:
$ docker run -ti --rm is_interactive "/bin/bash"
(interactive shell)
root#dd7dd9bf3f4e:/$ echo something
something
root#dd7dd9bf3f4e:/$ echo $HOME
/root
root#dd7dd9bf3f4e:/$ exit
exit
run the image not interactively:
$ docker run --rm is_interactive "echo \$HOME"
(not interactive shell)
/root
$
This stackoverflow answer helped me find [ -t 0 ].

Resources