Docker本地JS文件IO错误
我在用Docker加载本地的js文件时遇到了麻烦。在本地机器上测试时没有问题,但如果我把它放在运行在Docker上的网络服务器上,就会出现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
中读取这个文件时没有问题,代码是<script src="../static/js/data_data.js"></script>
在application.py
中,我使用file = open("static/js/data.js", "w+")
来创建文件,这在本地机器上运行得很好,但在网络服务器(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"]
1 个回答
1
问题在于你没有在应用程序中指定一个绝对链接。
你可以在本地轻松重现这个问题,方法是
cd /
python /<pathtosrc>/src/application.py
默认情况下,Docker的工作目录是 /
。
所以当你尝试运行你的程序时,它会去找 /static/js/data.js
,但这个文件并不存在。
你可以通过使用绝对链接来解决这个问题,或者在你的Dockerfile中更改Docker的工作目录。只需在最后添加以下一行
# Run
CMD ["python", "/src/application.py"]
WORKDIR /src
记住,WORKDIR
会改变在 WORKDIR
行之后运行的每个命令的工作目录。