Unstable connection to remote server in Docker

I have created a docker container to run my python program inside.

My program requires retrieving the known_host under my .ssh folder:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect(server, username=username, password=password)

I have mounted it into the docker container using:

docker run --name test_cntr --rm  
-v $SCRIPT_DIR:/home/ 
-v $DATA_DIR:/home/data 
-v $HOME/.ssh/known_hosts:/root/.ssh/known_hosts 
-e PYTHONPATH=/home/sciprt_dir:/home/sciprt_dir/lib 
-e INDEX=0 
dummy_image python /home/run.py

Found that my program can successfully get the known_hosts file sometimes, but sometimes not, below error is shown:

Exception is [Errno -2] Name or service not known

I didn’t re-run the container during the run.py execution. Suppose the known_hosts mounted to the container at the beginning and run.py should be able to use it throughout whole running.

Source: StackOverflow