Update /etc/hosts at rebuild


I try to add a command to the app.yml file to add an entry to the host file in the docker container at the rebuild.
But this command never works and i can’t understand why :zipper_mouth_face:

   - exec: "echo ' custom.domain.fr' >> /etc/hosts"

Any how to update the /etc/hosts at rebuild ?

Pretty sure your quotes are wrong.

   - exec: echo ' custom.domain.fr' >> /etc/hosts

The issue seems to be, that command will work just fine, but will be lost on container start. Go ahead and enter the container, run the command by hand, then restart it:

./launcher enter app
echo ' custom.domain.fr' >> /etc/hosts
grep custom /etc/hosts custom.domain.fr
./launcher restart app
./launcher enter app
grep custom /etc/hosts

Not quite sure what process is rewriting the /etc/hosts file.

This works:

./launcher start app
docker exec -it app bash -c "echo custom.domain.fr >> /etc/hosts"

But you’d need to do that every start. (And if you try to modify launcher to do that for you, launcher will get upset when it can’t update itself with git. Ask me how I know this? I’ve done that.)


Okay, I figured it out. You cannot use the yml file because /etc/hosts is created at runtime by Docker itself. To add entries to it, you need to use the --docker-args option to launcher at rebuild time.

./launcher rebuild app  --docker-args "--add-host  extra.dns:"
./launcher enter app
grep extra /etc/hosts extra.dns

There are a slew of dns / resolver / similar options for docker run, and those are the ones you need to use for this sort of stuff.


Hummm ! Intersting :+1:
Meaning I cannot use /admin/upgrade to update to Discourse :zipper_mouth_face:

Well, here’s a slightly messy alternative.

Changes to /etc/hosts file are effectively immediate for the system. Docker creates the file at boot time, while setting up all of the container internal networking. But after that Docker leaves it alone. A “plug-in” that inserted itself into late system boot could add values to the /etc/hosts file. (Or could be a script created via yml.)

I might try to tackle that later today.

/admin/upgrade shouldn’t touch the /etc/hosts file - it just updates stuff within the docker container, it doesn’t do a full rebuild.

Even the update on docker_manager ? :face_with_raised_eyebrow:

Yep, updating docker_manager just updates the docker_manager discourse plugin, which contains all the scripts for updates. It still doesn’t even restart the container, let alone rebuild it.


Huh, add to list of things I didn’t realize. Never paid that much attention to what was happening, since it always just worked. :blush:


This bash script is checking if smtp entry is defined in /etc/hosts in docker container:
file: check_hosts_entry

#!/usr/bin/env bash

smtp=$(docker exec -t app bash -c "cat /etc/hosts | grep -m 1 your_smtp_domain" | tr -d '\r');
if [[ "$smtp" != "your.ip your_smtp_domain" ]]; then
  echo 'No SMTP entry. Adding it...';
  docker exec -t app bash -c "echo your.ip your_smtp_domain >> /etc/hosts";
  echo 'The SMTP entry is already there!';

Add it to your host’s crontab to be run every 30 mins or whatever you like:

*/30 * * * * /root/bin/check_hosts_entry >/tmp/stdout.log 2>/tmp/stderr.log

*Don’t forget to replace your.ip and your_smtp_domain with your specific data :wink: