ONLYOFFICE Docker - modify /etc/hosts in Docker image via script

Good morning alltogether,

i am using ONLYOFFICE in Nextcloud (meanwhile NC 25.0.1) running office under docker.

To simplify the periodically updates in docker image files for ONLYOFFICE i have made a bash script executable via cron job.

My Script acts as follows:

#!/bin/bash
#
echo "Stoppe docker container ONLYOFFICE"
docker stop ONLYOFFICE
docker rm ONLYOFFICE
docker system prune -a -f
#
echo "Lade neuestes image herunter ..."
docker pull onlyoffice/documentserver
echo "Starte neuen Container ONLYOFFICE; Portmapping 8443:443"
#
docker run --name=ONLYOFFICE -i -t -d -p 8443:443 --restart=always \
    -v /app/onlyoffice/DocumentServer/data:/var/www/onlyoffice/Data \
    -v /app/onlyoffice/DocumentServer/logs:/var/log/onlyoffice  \
    -v /app/onlyoffice/DocumentServer/lib:/var/lib/onlyoffice \
    -v /app/onlyoffice/DocumentServer/db:/var/lib/postgresql \
    -e JWT_ENABLED='true' -e JWT_SECRET='MY-SECRET' onlyoffice/documentserver
#
echo "ONLYOFFICE neu gestartet"
docker ps
echo "Sende Email >>"
/usr/bin/sendEmail -v -f MY-EMAILADRESS -s MY-SMTP-SERVER:587 -xu USERNAME-FOR-MAILSERVER -xp PASSWORD-FOR-MAILSERVER -t TO-ADRESS -o tls=yes -u ONLYOFFICE Image update und Docker Neustart -m Der ONLYOFFICE docker container wurde mit einem neuen Image gestartet! -a /var/log/oo_update.log
echo "Starte Dienste neu"
systemctl stop php8.1-fpm && systemctl stop nginx && systemctl restart postgresql
echo "PostgreSQL erfolgreich neu gestartet"
sleep 1
systemctl start php8.1-fpm
echo "PHP 8.1 erfolgreich neu gestartet"
sleep 1
systemctl start nginx
echo "NGinx Wegserver erfolgreich neu gestartet"
exit 0

That is working good for me so far and i have periodically checked the docker image server to download a fresh ONLYOFFICE.

I know that i can enter my docker instance by terminal via

sudo docker exec -ti ONLYOFFICE bash

In docker i can append a line to my /etc/hosts file in ONLYOFFICE docker via

echo "IP-OF-MY-NEXTCLOUDSERVER NAME NAME.MY-DOMAIN" >>/etc/hosts

So far so good.
Every time i let the bash script install a new image, this setting is gone, clear.
How can i automatically do this appendix to the /etc/hosts file in docker via expanding my script above?

The reason for that:
Since update to NC 25.0.1 i got errors like

Error while downloading the document file to be converted

[Onlyoffice] Error while downloading the document file to be converted

Thnaks for any ideas :+1:

You could do that a few ways. Simplest (but perhaps not safest) might be to use docker cp to overwrite the hosts file. Or you could write a script for it, copy it in with docker cp and then run it with docker exec.

You might even be able to run the echo command in docker exec, but I’m not sure if passing the output redirection will work.

Many thanks for your reply.

I tried to use cp lika that

docker cp ONLYOFFICE:/etc/hosts /tmp/hosts (worked good!)

Then appended the IP of my Nextcloudserver with echo “…”

But then

docker cp /tmp/hosts ONLYOFFICE:/etc/hosts

failed with error because the /etc/hosts file in the docker container is secured.
Unable to copy over hosts file into container: "device or resource busy" · Issue #22281 · moby/moby · GitHub

But i found another solution maybe also for others.

Using the –add-host Option is doing the trick!

My script looks like this now and it is working as expected:

#!/bin/bash
#
echo "Stoppe docker container ONLYOFFICE"
docker stop ONLYOFFICE
docker rm ONLYOFFICE
docker system prune -a -f
#
echo "Lade neuestes image herunter ..."
docker pull onlyoffice/documentserver
echo "Starte neuen Container ONLYOFFICE; Portmapping 8443:443"
#
docker run --name=ONLYOFFICE -i -t -d -p 8443:443 --restart=always --add-host=HOSTNAME-OF-THE-NC-SERVER:IP-OF-THE-NC-SERVER \
    -v /app/onlyoffice/DocumentServer/data:/var/www/onlyoffice/Data \
    -v /app/onlyoffice/DocumentServer/logs:/var/log/onlyoffice  \
    -v /app/onlyoffice/DocumentServer/lib:/var/lib/onlyoffice \
    -v /app/onlyoffice/DocumentServer/db:/var/lib/postgresql \
    -e JWT_ENABLED='true' -e JWT_SECRET='MY-SECRET' onlyoffice/documentserver
#
echo "ONLYOFFICE neu gestartet"
docker ps
echo "Sende Email >>"
/usr/bin/sendEmail -v -f MAILADRESS -s SMTP-SERVER:587 -xu USERNAME -xp PASSWORD -t RECEIPIENT -o tls=yes -u ONLYOFFICE Image update und Docker Neustart -m Der ONLYOFFICE docker container wurde mit einem neuen Image gestartet! -a /var/log/oo_update.log
echo "Starte Dienste neu"
systemctl stop php8.1-fpm && systemctl stop nginx && systemctl restart postgresql
echo "PostgreSQL erfolgreich neu gestartet"
sleep 1
systemctl start php8.1-fpm
echo "PHP 8.1 erfolgreich neu gestartet"
sleep 1
systemctl start nginx
echo "NGinx Wegserver erfolgreich neu gestartet"
exit 0

So every time the script generates a new ONLYOFFICE image it also inserted the hostname and IP Adress of the Nextcloud Server as well.

Hope this can help others with similar questions.

Nice feature of the --add-host Option is, that it can used multiple times
–add-host=HOST1:IP --add-host=HOST2:IP …

:grin: