Hi all,
I do realize this is more linux-related question, but maybe (just maybe) it is docker-specific. The reason I can’t test it outside of the docker container is that IPsec connection is limited to external and internal IP addresses of the docker container linux instance.
So, I have manual/crontab.txt file:
* * * * * /usr/sbin/ipsec status | /bin/grep -q 'ESTABLISHED' || /usr/sbin/ipsec restart
@reboot /usr/sbin/ipsec restart
@reboot /usr/local/bin/python3.10 -m pip install requests requests_ntlm
@reboot /usr/local/bin/python3.10 -m pip install --upgrade pip
@reboot /bin/sleep 60 && /usr/sbin/ipsec status | /bin/grep -q 'ESTABLISHED' || /usr/sbin/ipsec restart
Dockerfile:
RUN if [ ! -f "/app/manual/crontab.txt" ]; then echo && echo && echo && echo "File /manual/crontab.txt does NOT exist: the file must be created by user before running docker build -- check git repository README file, build aborted." && echo && echo && echo && exit 1; fi
RUN if [ ! -s "/app/manual/crontab.txt" ]; then echo && echo && echo && echo "File /manual/crontab.txt is empty: the file must be populated by the user before running docker build -- check git repository README file, build aborted." && echo && echo && echo && exit 1; fi
RUN apt update
RUN apt install -y cron
COPY ./manual/crontab.txt /etc/cron.d/ipsec-restart-cron
RUN chmod 0644 /etc/cron.d/ipsec-restart-cron
RUN crontab /etc/cron.d/ipsec-restart-cron
RUN touch /var/log/cron.log
I do realize that installing pip packages in crontab inside of docker container is not according to the best practise and there is a Dockerfile for that, but this is to demonstrate how I try to set it and it does work for me with pip, but somehow it does not with IPsec. The same ipsec command shown above works for me when running it manually.
Ok, actually I figured out how to workaround that in Python:
import requests
from requests_ntlm import HttpNtlmAuth
import subprocess
ipsec_status = subprocess.run(['/usr/sbin/ipsec', 'status'], capture_output=True, text=True)
if 'ESTABLISHED' not in ipsec_status.stdout:
subprocess.run(['/usr/sbin/ipsec', 'restart'])
...
[the rest of my Python code]
But hey, it should not be this hard to do it properly in docker, right? It is always great to have a workaroound instead of nothing, but I do believe there are gurus here to help.
Another thing is, I need a jupyter notebook running as a service inside of my docker container. I can run it anytime manually like this:
/usr/local/bin/jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10 --ip 0.0.0.0 --no-browser --allow-root --NotebookApp.token='some_token1'
But my less advanced in docker team mates need this as well, I don’t want to be a bottleneck in case I forgot to run it manually, so here is my attempt to do it in crontab:
@reboot /usr/local/bin/jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10 --ip 0.0.0.0 --no-browser --allow-root --NotebookApp.token='some_token1'
*/5 * * * * /usr/bin/pgrep -f "jupyter notebook" > /dev/null || /usr/local/bin/jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10 --ip 0.0.0.0 --no-browser --allow-root --NotebookApp.token='some_token1'
None of those crontab record worked for me.
Please advise how to do it properly.