I am trying to write a Linux bash script that creates and runs docker containers that run a simple python web server, extract the container IP Address and make an HTTP Request to the server through a browser. There seem to be some mistakes in my script and I need help in editing it. I am new to bash scripting. I use Ubuntu 13.10
My Script:
#!/bin/bash
for i in {1..2}
do
echo "Creating and Running Container $i "
sudo docker run -name $i PythonServer
IP=sudo docker inspect $i | grep IPAddress | cut -d '"' -f 4
echo "Ip Address is:"
echo "${IP}"
xdg-open 'http://$IP:7111/execute'
done
EDIT: Line 6 of the code doesn't seem to be working as echo "${IP}" is giving a blank output. I want to assign the result of sudo docker inspect $i | grep IPAddress | cut -d '"' -f 4 to IP variable i.e I want to capture the IP Address of the created container and use it in xdg-open 'http://$IP:7111/execute' to make the HTTP Request dynamically. But I'm not getting the right format in which this bash script line should be written: IP=sudo docker inspect $i | grep IPAddress | cut -d '"' -f 4
And also what is the correct format in which I can use IP value in xdg-open 'http://$IP:7111/execute'
This above mentioned script presently shows this message on execution:
vishal#bl-lin-01:~/docker/PythonServer/new$ ./BashScript.sh
Creating and Running Container 1
WARNING: Docker detected local DNS server on resolv.conf. Using default external servers: [8.8.8.8 8.8.4.4]
^Cserving at port 7111
Traceback (most recent call last):
File "/scripts/InitScript.py", line 40, in <module>
httpd.serve_forever()
File "/usr/lib/python2.7/SocketServer.py", line 225, in serve_forever
r, w, e = select.select([self], [], [], poll_interval)
KeyboardInterrupt
unknown option: 1
valid options are:
-license
-copyright
-crlf
-end
-link
-path_name
-tab
-ascii
-apple_macro
-assert_macro
-minmax
-unnamed
default is all checks on; otherwise options specify desired checks
Ip Address is:
Thanks.
This Edited Bash Script works fine now..
#!/bin/bash
for i in {1..2}
do
echo "Creating and Running Container $i "
sudo docker run -d -name $i PythonServer
IP=$(sudo docker inspect $i | grep IPAddress | cut -d '"' -f 4)
echo "Ip Address is:$IP"
xdg-open "http://$IP:7111/execute"
done
I ran the containers in Daemon mode and used the Command Substitution feature of bash to assign the output of the command to variable IP
The best way to obtain docker container IP address is
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER_NAME
Related
So I am trying to get the combined output of all the container logs with its container name in a log file
docker logs --tail -3 with container name >> logst.log file.
docker logs takes a single command so you would have to run them for each container. I guess I could do something along:
docker ps --format='{{.Names}}' | xargs -P0 -d '\n' -n1 sh -c 'docker logs "$1" | sed "s/^/$1: /"' _
docker ps --format='{{.Names}}' - print container names
xargs - for input
-d '\n' - for each line
-P0 - execute in parallel with any count of parallel jobs
remove this option if you don't intent to do docker logs --follow
it may cause problems, consider adding stdbuf -oL and sed -u to unbuffer the streams
-n1 - pass one argument to the underyling process
sh -c 'script' _ - execute script for each line with line passed as first positional argument
docker logs "$1" - get the logs
sed 's/^/$1: /' - prepend the name of the docker name to each log line
But a way better and industrial grade solution would be to forward docker logs to journalctl or other logging solution and use that utility to aggregate and filter logs.
Got it.
for i in docker ps -a -q --format "table {{.ID}}"; do { docker ps -a -q --format "table {{.ID}}\t{{.Names}}\n" | grep "$i" & docker logs --timestamps --tail 1 "$i"; } >> logs.log; done
logs.log is a generic file name.
I need to check if container is already present with specific name.
Using if statement for same, however container is present with same name but i am not sure why if statement is not running successfully.
if [`echo password | sudo -S docker ps -all|grep test|cut -d' ' -f1`]
then
statements
else
statements
container with test name is present but its always going inside the else statement. Could you help me on this.
You need blanks between [, ] and `, i.e. :
if [ `echo password | sudo -S docker ps -all|grep test|cut -d' ' -f1` ]
Also, you need fi after the last line.
in addition to #ÔHARA Kazutaka information.
-all does not exists, try --all instead.
The --quiet --filter name=test options are going to make docker filters output using the container's name, and make it prints to the standard output the CONTAINER ID if a container exists.
Give a try to this:
container_name=test
container_id=$(echo password | sudo -S docker ps --all --quiet --filter name="${container_name}")
if [ ! -z "${container_id}" ] ; then
printf "%s is the id of the container with name '%s'\n" "${container_id}" "${container_name}"
else
printf "no docker container with name '%s' has been found.\n" "${container_name}"
fi
I have a script and I am running it as scheduled via cron. In that script I have line to extract IP address of that machine and that line is below
ip_address=$(ip addr show | grep -E -o '(inet ([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}))' | grep -v 'inet 127.0.0.1' |
sed 's/inet//g')
If I am just executing above line on bash command line its working and echo $ip_address giving me IP of that machine but If I am calling it via crobjob , echo $ip_address giving empty output.
even If I call entire script from bash shell, I am getting IP in the output but only via cron I am not getting the IP in the output.
Could someone please help ?
Thank you.
The following command runs well when i run it localy on the dsired machine:
app_name=*some_proccess_name*
pid=`pgrep $app_name | tail -n 1`
But when i run it in the following way, from a remote pc using ssh it dosn't work:
pid=$(ssh $USER_NAME#$HOST_NAME "echo `pgrep $app_name | tail -n 1`")
The value of pid afterwards is just blank. I am not sure what's wrong (just to clarify i have tried several processes names that are all running on the target pc- that's not the problem ).
P.S when i run the command without echo, like that, i just get stuck inside the remote pc and have to use exit in order to get unstuck and return to my local pc:
pid=$(ssh tester#mir1 "`pgrep indicator-apple | tail -n 1`")
Less is more
pid=$(ssh tester#mir1 pgrep indicator-apple \| tail -n 1)
I want to convert this shell script to Windows batch file. How I could do this?
#!/bin/bash
linkreboot="http://admin:admin123!#192.168.1.1/rebootinfo.cgi"
# ping google
ping="ping -c 1 -w 3 -q www.google.ch"
if $ping | grep -E "min/avg/max/mdev" > /dev/null
then
# ping is ok
echo 'connection is ok'
else
# ping is down, reboot
/usr/bin/wget -O /dev/null $linkreboot
# if no web reboot is allowed
echo 'No valid ping, reboot'
fi
Start a Command Prompt.
Type the ping command from the bash script and see how it works. If you get stuck, type
ping /?
When you have got that, type
findstr /?
to work out an equivalent for grep and run
ping ... | findstr ...
Then disconnect your router from the Internet and see how it changes when the network is down.
Google wget and install it. See if you can reboot your router when it is disconnected from the Internet using wget.
Use http://ss64.com/nt/ if you get stuck or need examples.
you can download compiled linux command from here
then in batch file use that
in batch file syntax very complex but powershell you can
for ping in batch file you should use
#echo off
Ping www.google.ch -n 1 -w 1000
if errorlevel 1 echo Not connected
then use wget command from that like
in powershell you can use :
$server = "www.google.ch"
$linkreboot="http://admin:admin123!#192.168.1.1/rebootinfo.cgi"
$date = Get-Date
if (test-Connection $Server -Count 1 -Quiet )
{ write-host "connection is ok"
}
else{
wget $linkreboot
}
else {
write-host "No valid ping, reboot"
note: in powershell for run script you should change execution-policy