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 Service updatedAt timestamp
  • Pass docker-compose environment to symfony configuration
  • Build .NET Core Console Application into Docker
  • Docker: Error processing tar file(exit status 1): Error setting up pivot dir: not a directory
  • Can a Docker container be started directly from Bluemix Workload Scheduler?
  • DNS issue with docker image
  • #!/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.

  • Using host machines CA cert within Docker container
  • Constraint in docker-compose gives container not found error in ucp
  • How do I get the address of the host when using 'docker build'?
  • docker-machine without TLS verification
  • How can I create a Jenkins Docker image that uses provided ssh keys for jenkins user?
  • How can JVMs running inside Bluemix container groups be monitored?
  • 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.