Docker IO错误与本地js文件

我有很难与docker加载本地js文件。 我没有问题在本地计算机上testing,但如果我托pipe它运行在No such file or directory: 'static/js/data.js上的Web服务器No such file or directory: 'static/js/data.js错误。

我的代码组织如下:

 Root/ application.py static/ js/ data.js templates/ index.html 

application.py我生成文件data.js和从index.html我读文件data.js没有问题<script src="../static/js/data_data.js"></script>

application.py我使用file = open("static/js/data.js", "w+")创build一个在本地机器上正常工作但不在Web服务器(EC2)

这是我的dockerfile

 FROM ubuntu:14.04 RUN apt-get update RUN apt-get install python-setuptools -y && DEBIAN_FRONTEND=noninteractive apt-get install python-setuptools -y RUN apt-get install python-pip -y RUN apt-get install python-numpy -y RUN apt-get install python-matplotlib -y RUN apt-get install python-mysqldb -y RUN apt-get install libpq-dev -y RUN apt-get install python-psycopg2 -y RUN apt-get install python-pandas -y RUN pip install flask ADD . /src # Expose EXPOSE 80 # Run CMD ["python", "/src/application.py"] 

问题是你没有在应用程序中指定绝对链接。


你可以很容易地在你的本地重现这个问题

 cd / python /<pathtosrc>/src/application.py 

Docker的工作目录是/默认的。

所以当你试图执行你的程序时,它会search/static/js/data.js 。 但它不存在。

您可以通过使用绝对链接或通过更改Dockerfile中的Docker工作目录来解决问题。 最后只需添加以下行

 # Run CMD ["python", "/src/application.py"] WORKDIR /src 

请记住, WORKDIR行将更改WORKDIR行后面的每个命令的工作目录