简体   繁体   English

将外部文件挂载到Docker容器中

[英]Mount external files into Docker container

I have a prediction application with the below folder structure: 我有一个具有以下文件夹结构的预测应用程序:

Docker
├── dataset
│   └── fastText
│       └── crawl-300d-2M.vec
├── Dockerfile
├── encoder
│   └── sentencoder2.pkl
├── pyt_models
│   └── actit1.pt
├── requirements.txt
└── src
    ├── action_items_api.py
    ├── infer_predict.py
    ├── model.py
    ├── models.py
    └── sent_enc.py

Dockerfile: Dockerfile:

FROM python:3.6

EXPOSE 80


# copy and install packages for flask

COPY /requirements.txt /tmp/
RUN cd /tmp && \
    pip3 install --no-cache-dir -r ./requirements.txt


WORKDIR /Docker

COPY src src

CMD gunicorn -b 0.0.0.0:80 --chdir src action_items_api:app

In the Docker file I try only to copy the src folder where all the Python files are placed. 在Docker文件中,我只尝试复制放置所有Python文件的src文件夹。 I want to keep the fastTest, ecnode, pyt_models to be accessed outside the container. 我想让fastTest,ecnode,pyt_models可以在容器外部访问。

When I tried: 当我尝试:

docker run -p8080:80 -v /encoder/:/encoder/;/pyt_models/:/pyt_models/;/dataset/:/dataset/ -it actit_mount:latest

But by doing this my code gives me FileNotFoundError No such file or directory: 'encoder/sentencoder2.pkl' 但是通过这样做,我的代码给了我FileNotFoundError No such file or directory: 'encoder/sentencoder2.pkl'

But by keeping the same folder structure if I run from the docker folder: gunicorn --chdir src --bind 0.0.0.0:80 action_items_api:app It works. 但是,如果我从gunicorn --chdir src --bind 0.0.0.0:80 action_items_api:app文件夹运行,则保持相同的文件夹结构: gunicorn --chdir src --bind 0.0.0.0:80 action_items_api:app它起作用。

What is wrong with the Dockerfile or the docker run ? Dockerfile或docker docker run什么问题?

Because you set WORKDIR /Docker , the gunicorn process will have its working directory set to /Docker . 因为您将WORKDIR /Docker设置为WORKDIR /Docker ,所以gunicorn进程的工作目录将设置为/Docker Which implies that relative file paths in your python app will be resolved from /Docker . 这意味着您的python应用程序中的相对文件路径将从/Docker解析。

Give a try to 试试看

docker run -p8080:80 \ 
  -v $(pwd)/encoder/:/Docker/encoder/ \
  -v $(pwd)/pyt_models/:/Docker/pyt_models/ \
  -v $(pwd)/dataset/:/Docker/dataset/ \
  -it actit_mount:latest

docker: Error response from daemon: create ./folder: "./folder" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. docker:来自守护程序的错误响应:create ./folder:“./folder”包含本地卷名的无效字符,只有“ [a-zA-Z0-9] [a-zA-Z0-9 _.-]”允许的。 If you intended to pass a host directory, use absolute path. 如果要传递主机目录,请使用绝对路径。

Here is an example: 这是一个例子:

docker run -v的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM