Python3 script doesn't start (always or directly) from start.sh

I have a dockerfile that starts from FROM ubuntu:20.04. It runs apt-get update and install nordvpn, python3.6, pip and Python dependencies. It copies my clientscript.py in the folder /usr/local/bin/ just like start.sh and finally, the dockerfiles ends with:

COPY ./start.sh /usr/local/bin/start.sh
RUN chmod +x /usr/local/bin/start.sh 
# Use the start script as the entrypoint
ENTRYPOINT ["/usr/local/bin/start.sh"]

I can build my image without any problems from the terminal:


> docker build -t virtualclient .

From a serverscript.py, I run the container 20 times (one by one) with a different argument cl>

cl = 1 (this is number 1...20) 
container = dockerclient.containers.run("virtualclient", cl, cap_add=['NET_ADMIN'], detach=True)

The docker container starts succesfully with entrypoint start.sh. The code for start.sh:

#!/bin/bash
echo "Start virtualclient$1"
/etc/init.d/nordvpn start && sleep 5

case "$1" in
    "1") nordvpn login --token mytoken
    nordvpn connect be
    ;;
    "2") nordvpn login --token mytoken
    nordvpn connect ie  
    ;;
    "3") nordvpn login --token mytoken
    nordvpn connect de 
    ;;
  (But then 20 times for each client)
esac
python3 /usr/local/bin/Client_v3_virtual.py $1
echo "Start script with argument $1" 
# Keep the container running
tail -f /dev/null

Via docker logs, I see that client starts with the right client number and makes connection with nordvpn. But the script Client_v3_virtual.py with argument $1 doesn’t always start, or it starts but only after a long time. I also do not see the echo “Start script with argument $1” So it’s like the docker container hangs in starting the script.

Since that it sometimes works and sometimes not, I have no idea what do change in my start.sh.
I hope that my description is clear enough and somebody can help.
thanks in advance.

Unless the API key you added is fake, you should probably delete this post and make a new one without it


Hi
Thx for your reaction. It’s an old not working API key, I changed it here to ‘mytoken’
Best regards

I don’t know why the script doesn’t start if it is true, but if you are waiting for output from Python, you should probably set the PYTHONUNBUFFERED varible: django - What is the use of PYTHONUNBUFFERED in docker file? - Stack Overflow as Python can deleay the output unless you are using the logging library.

And instead of installing everything in a single container, I would run a vpn container and set the vpn start command as command of that container, and run the other processes in separate containers using the vpn container’s network namespace. In Docker Compose:

services:
  vpncontainer:
    # ...
  pythoncontainer:
     network_mode: service:vpncontainer

Then you don’T need a tail command to keep the container alive and docker stop will work properly as well

1 Like

Hi,
Thanks for your respons. The pythonunbuffered was indeed the problem. The script started, but I didn’t see the output.
As you suggest, I did split it in a vpn container and a python container. So far, I didn’t work with compose. I’ll read some documentation about that.