我正在尝试为python / flask webapp创建一个dockerfile,并且尽管根据我读过的内容进行了多次更改,仍然会遇到问题
我目前使用的Dockerfile如下:
FROM ubuntu:latest
#Update OS
RUN sed -i 's/# \(.*multiverse$\)/\1/g' /etc/apt/sources.list
RUN apt-get update
RUN apt-get -y upgrade
# Install Python
RUN apt-get install -y python-dev python-pip
# Add requirements.txt
ADD requirements.txt /webapp
ADD requirements.txt .
# Install uwsgi Python web server
RUN pip install uwsgi
# Install app requirements
RUN pip install -r requirements.txt
# Create app directory
ADD . /webapp
# Set the default directory for our environment
ENV HOME /webapp
WORKDIR /webapp
# Expose port 8000 for uwsgi
EXPOSE 8000
ENTRYPOINT ["uwsgi", "--http", "127.0.0.1:8000", "--module", "app:app", "--processes", "1", "--threads", "8"]
#ENTRYPOINT ["python"]
CMD ["app.py"]
尝试使用命令sudo docker build -t imgcomparer .
给出错误:
Step 10/15 : ADD . /webapp
Error processing tar file(exit status 1): Error setting up pivot dir: mkdir /var/lib/docker/aufs/mnt/53420471c832e61b7f75ac5fc5268d64b932a4d589a8464c63bf5868f127ff04/webapp/.pivot_root981494252: not a directory
经过一些研究后,我发现在路径的末尾添加一个尾随的/
会有效(请参阅this question和this one)
这样做(以下几行相同)我的dockerfile中有以下内容:
# Create app directory
ADD . /webapp/
# Set the default directory for our environment
ENV HOME /webapp/
WORKDIR /webapp/
出现此错误:
Step 10/15 : ADD . /webapp/
stat /var/lib/docker/aufs/mnt/f37b19a8d72d39cbbdfb0bae6359aee499fab0515e2415e251a50d528708bdd3/webapp/: not a directory
最后,我尝试完全删除有问题的行。当我有
# Create app directory
# ADD . /webapp
# Set the default directory for our environment
ENV HOME /webapp
WORKDIR /webapp
docker文件成功构建!但是,不出所料,尝试运行它会产生错误:
sudo docker run -t imgcomparer
docker: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "chdir to cwd (\"/webapp\") set in config.json failed: not a directory"
: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
目录结构如下
app.py
image_data.db
README.txt
requirements.txt
Dockerfile
templates
- index.html
static/
- image.js
- main.css
img/
- camera.png
images/
- empty
答案 0 :(得分:0)
我相信你必须先创建目录才能引用它:
RUN mkdir /webapp
修改强>
(ADD requirements.txt /webapp
之前)
使用
ADD somefile.ext /folder
(没有斜杠到文件夹)你引用一个文件,所以你在根目录下得到一个名为folder
的文件,里面有somefile.ext
的内容。
需要引用目录和文件时要小心。
因此你也可以:
ADD requirements.txt /webapp/
此外:为什么要两次添加requirements.txt
?您应该在Dockerfile中尽可能采取一些步骤,这样您就可以:
[...]
RUN apt-get install -y python-dev python-pip && \
pip install uwsgi
ADD . /webapp/
RUN pip install -r /webapp/requirements.txt
[...]