简体   繁体   中英

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:

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. I want to keep the fastTest, ecnode, pyt_models to be accessed outside the container.

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'

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.

What is wrong with the Dockerfile or the docker run ?

Because you set WORKDIR /Docker , the gunicorn process will have its working directory set to /Docker . Which implies that relative file paths in your python app will be resolved from /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. If you intended to pass a host directory, use absolute path.

Here is an example:

docker run -v的示例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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