# Update /etc/hosts at rebuild

**URL:** https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703
**Category:** Self-hosting
**Created:** [8월 24, 2017, 3:19오후 UTC](https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703 "2017-08-24T15:19:18Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Chopper](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chopper/32/57073_2.png) [@Chopper](https://meta.discourse.org/u/Chopper)
#### Post date: [8월 24, 2017, 3:19오후 UTC](https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703/1 "2017-08-24T15:19:18Z")

</div>

Hello,

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 🤐

```plaintext
run:  
   - exec: "echo '192.168.0.1 custom.domain.fr' >> /etc/hosts"

```

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

---

<div class="post-metadata">

### Author: ![elijah](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/elijah/32/104055_2.png) [@elijah](https://meta.discourse.org/u/elijah)
#### Post date: [8월 25, 2017, 12:04오전 UTC](https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703/2 "2017-08-25T00:04:57Z")

</div>

Pretty sure your quotes are wrong.

```yaml
run:  
   - exec: echo '192.168.0.1 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:

```sh
./launcher enter app
echo '192.168.0.1 custom.domain.fr' >> /etc/hosts
grep custom /etc/hosts
192.168.0.1 custom.domain.fr
exit
./launcher restart app
./launcher enter app
grep custom /etc/hosts

```

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

This works:

```plaintext
./launcher start app
docker exec -it app bash -c "echo 192.168.0.1 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.)

---

<div class="post-metadata">

### Author: ![elijah](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/elijah/32/104055_2.png) [@elijah](https://meta.discourse.org/u/elijah)
#### Post date: [8월 25, 2017, 4:45오전 UTC](https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703/3 "2017-08-25T04:45:26Z")

</div>

> [@Chopper](#):
>
> Any how to update the /etc/hosts at rebuild ?

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.

```plaintext
./launcher rebuild app --docker-args "--add-host extra.dns:8.8.8.8"
./launcher enter app
grep extra /etc/hosts
8.8.8.8 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.

---

<div class="post-metadata">

### Author: ![Chopper](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chopper/32/57073_2.png) [@Chopper](https://meta.discourse.org/u/Chopper)
#### Post date: [8월 25, 2017, 7:58오전 UTC](https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703/4 "2017-08-25T07:58:56Z")

</div>

Hummm ! Intersting 👍  
Meaning I cannot use `/admin/upgrade` to update to Discourse 🤐

---

<div class="post-metadata">

### Author: ![elijah](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/elijah/32/104055_2.png) [@elijah](https://meta.discourse.org/u/elijah)
#### Post date: [8월 25, 2017, 2:44오후 UTC](https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703/5 "2017-08-25T14:44:45Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [8월 25, 2017, 2:56오후 UTC](https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703/6 "2017-08-25T14:56:20Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chopper](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chopper/32/57073_2.png) [@Chopper](https://meta.discourse.org/u/Chopper)
#### Post date: [8월 25, 2017, 3:20오후 UTC](https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703/7 "2017-08-25T15:20:04Z")

</div>

Even the update on **docker\_manager**? 🤨

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [8월 25, 2017, 3:26오후 UTC](https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703/8 "2017-08-25T15:26:50Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![elijah](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/elijah/32/104055_2.png) [@elijah](https://meta.discourse.org/u/elijah)
#### Post date: [8월 25, 2017, 8:43오후 UTC](https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703/9 "2017-08-25T20:43:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![irek](https://avatars.discourse-cdn.com/v4/letter/i/b9bd4f/32.png) [@irek](https://meta.discourse.org/u/irek)
#### Post date: [7월 18, 2019, 1:15오후 UTC](https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703/10 "2019-07-18T13:15:49Z")

</div>

This bash script is checking if smtp entry is defined in /etc/hosts in docker container:  
file: **check\_hosts\_entry**

```plaintext
#!/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";
else
  echo 'The SMTP entry is already there!';
fi

```

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

```plaintext
*/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 😉

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [6월 8, 2024, 12:37오후 UTC](https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703/11 "2024-06-08T12:37:11Z")

</div>

이 주제는 2479일 후 자동으로 닫혔습니다. 새로운 답글은 더 이상 허용되지 않습니다.
