Bash Script to execute Docker container operations and extract container IP Address

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:

  • Docker can't write to directory mounted using -v unless it has 777 permissions
  • Docker bridge change docker0 and internal ip
  • How can I set negative niceness of a docker process?
  • PHP application moving to Docker failed to connect on AWS
  • Multi-Node Hadoop in kubernetes
  • Many Docker Containers and Thrift
  • #!/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.

  • sbt not generating bin directory when staging or publishing docker + other questions
  • Consul and registrator to single host, it's possible?
  • Tomcat 7 fails when starts in Debian, but continue working in background
  • Getting error installing nodejs when building from docker
  • Deploy Django app with Docker
  • Docker and Spring boot: how to hide port from url?
  • 2 Solutions collect form web for “Bash Script to execute Docker container operations and extract container IP Address”

    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
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.