python - 从本地机器(外部)提交在 Docker 内运行的 Python 脚本时出现问题

标签 python docker ubuntu containers

我正在尝试从外部(从托管 docker run 的同一本地计算机)在正在运行的 docker 内运行 python 作业。
当我进入 docker 并运行代码时,它会按预期创建日志:
进入docker的命令:

docker exec -it pytorch_container /bin/bash
我正在运行的随机 Python 代码(将日志写入同一目录以查看它是否运行成功):
#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") 
但是当我从外部提交 python 作业时,它既没有抛出任何错误,也没有运行(通过看到没有创建日志文件来推断)。
从外部 docker 运行 python 代码的命令:
sudo docker exec -ti pytorch_container python /workspace/test/test.py
请帮忙!

最佳答案

您的问题是您在脚本中使用了相对路径。因此,当您运行命令时,输出将位于容器的当前目录中,而不是放置脚本的位置

sudo docker exec -ti pytorch_container python /workspace/test/test.py
您可以在下面检查当前目录正在使用什么
sudo docker exec -ti pytorch_container pwd

关于python - 从本地机器(外部)提交在 Docker 内运行的 Python 脚本时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66582218/

相关文章:

mysql - elastic beanstalk docker app无法连接到mysql

ubuntu - 双启动屏幕问题 : windows 10 and Ubuntu

python - FFmpeg python不合并

python - 使用pyUSB从ELM327 OBDII读取数据到USB设备

python - 将自动化测试代码抽象为 Python 对象

docker - eventstore Docker 镜像的默认用户名和密码是什么?

haskell - 使用堆栈和自定义选项构建Docker容器

Django 用户注册问题

c++ - "/usr/lib/libstdc .so.6: version ` GLIBCXX_3.4.1 5' not found"是什么意思,我该如何解决?

python - PyPlot ColorMesh 使用错误的颜色映射创建绘图