Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Miniontoby/38c1ef9555b53bc0132316faa5f356b5 to your computer and use it in GitHub Desktop.
Save Miniontoby/38c1ef9555b53bc0132316faa5f356b5 to your computer and use it in GitHub Desktop.
Docker Host Mode, fix for "Failed to get hostname, using 'localhost'" warning

Docker Host Mode, fix for "Failed to get hostname, using 'localhost'" warning

Are you running Docker in Host Mode? And are you getting the following error?

Failed to get hostname, using 'localhost' java.net.UnknownHostException: 731089ff-a983-4140-8033-cb66c33766da: 731089ff-a983-4140-8033-cb66c33766da: Name or service not known

This also applies to Pterodacyl/Wings in Host Mode too!

Then use the code below to fix it!

Installation Steps

  1. Copy the code below to your server, and save it as /root/generate_hosts.sh
  2. Run crontab -e and add this to the end of the crontab: * * * * * bash /root/generate_hosts.sh
  3. Verify that it works by waiting until the time has moved to another minute and check if hosts file contains the container names
#!/bin/bash
# Installation:
# 1. Copy this file to your server in /root/generate_hosts.sh
# 2. Run `crontab -e` and add `* * * * * bash /root/generate_hosts.sh` to the end of the crontab
# 3. Verify that it works by waiting until the time has moved to another minute and check if hosts file contains the container names
# Get IP
MY_IP=$(ip -o route get to 8.8.8.8 | sed -n 's/.*src \([0-9.]\+\).*/\1/p')
# Remove existing ones
sed -e ':a;N;$!ba;s/\n*### DOCKER CONTAINERS//gm;s/\n\([0-9]\+\.\)\{3\}[0-9]\+\( \w\{8\}-\w\{4\}-\w\{4\}-\w\{4\}-\w\{12\}\)*//gm' /etc/hosts > /tmp/hosts
# Add the new ones
echo -e "\n### DOCKER CONTAINERS\n$MY_IP $(echo $(docker container ls --format ' {{.Names}}'))" >> /tmp/hosts
# Actually copy over
mv /tmp/hosts /etc/hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment