Search code examples
pythondockerubuntucontainers

Issue in submitting Python Script run inside Docker from local machine (outside)


I am trying to run python job inside a running docker from outside of it (from the same local machine hosting the docker run).

When I go inside the docker and run the code, it creates the log, as expected:

Command to go inside docker:

docker exec -it pytorch_container /bin/bash

A random Python code I am running (to write log to the same directory to see if it is running successfully):

#importing module 
import logging 
  
#Create and configure logger 
logging.basicConfig(filename="newfile.log", 
                    format='%(asctime)s %(message)s', 
                    filemode='w') 
  
#Creating an object 
logger=logging.getLogger() 
  
#Setting the threshold of logger to DEBUG 
logger.setLevel(logging.DEBUG) 
  
#Test messages 
logger.debug("Harmless debug Message") 
logger.info("Just an information") 
logger.warning("Its a Warning") 
logger.error("Did you try to divide by zero") 
logger.critical("Internet is down") 

But when I submit the python job from outside, it is neither throwing any error nor is it running (inferred that by seeing no log file created).

Command to run the python code from outside docker:

sudo docker exec -ti pytorch_container python /workspace/test/test.py

Please help!


Solution

  • Your issue is that you are using a relative path in your script. So when you run the command the output will be in the current directory of the container and not the place where the script is placed

    sudo docker exec -ti pytorch_container python /workspace/test/test.py
    

    You can check what the current directory is using below

    sudo docker exec -ti pytorch_container pwd