4

As per my application requirement, I need to get the server IP and the server name from the python program. But my application is resides inside the specific docker container on top of the Ubuntu.

I have tried like the below

import os
os.system("hostname")  # to get the hostname
os.system("hostname -i") # to get the host ip 

Output: 2496c9ab2f4a172.*.*.*

But it is giving the host name as a it's residing docker containerid and the host_ip as it's private ip address as above. I need the hostname as it is the server name. But when I type these above commands in the terminal I am able to get result what I want.

5 Answers 5

5

You won't be able to get the host system's name this way. To get it, you can either define an environment variable, either in your Dockerfile, or when running your container (-e option). Alternatively, you can mount your host /etc/hostname file into the container, or copy it...

This is an example run command I use to set the environment variable HOSTNAME to the host's hostname within the container:

docker run -it -e "HOSTNAME=$(cat /etc/hostname)" <image> <cmd>

In python you can then run os.environ["HOSTNAME"] to get the hostname. As far as the IP address goes, I use this command to get retrieve it from a running container:

route -n | awk '/UG[ \t]/{print $2}'

You will have to install route to be able to use this command. It is included in the package net-tools. apt-get install net-tools

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your suggestions. I have tried to add the hostname to the environmental variable to running container. It is not accepting my command. my command is docker run <containername> -e HOST_HOSTNAME = 'hostname' And coming to the IP, i can get it using the ipgetter module like ipgetter.myip()
PyCharm is also being a smart ass and has escaped the $(cat /etc/hostname) part. So in the container, when I type os.environ["HOSTNAME"] I get $(cat /etc/hostname) as a string. Facepalm
0

An alternative might be the following:

ENV:

NODENAME: '{{.Node.Hostname}}'

This will get you the Hostname of the Node, where the container is running as an environment variable (tested on Docker-Swarm / CoreOs Stable).

1 Comment

This worked great for me. I can get the a3fc2... style name from os.gethostname(), but this gave me the friendly name (e.g. manager-1). Not sure why it was downvoted.
0

you can go for something like this:

def determine_docker_host_ip_address():
    cmd = "ip route show"
    import subprocess
    process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
    output, error = process.communicate()
    return str(output).split(' ')[2]

Comments

0
import os

os.uname().nodename

2 Comments

Could you please add a bit more description, either why/how the code works or any information about the module
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

You can get the hostname on Linux/Unix systems by using the os package:

import os

os.uname().nodename

However this won't work on systems like Windows, as os.uname is not available there.

Instead, use the platform package, as it works on all systems/platforms:

import platform

platform.node()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.