简体   繁体   中英

Accessing data in an external drive by adding it as a volume in docker-compose, then using docker-compose up

I've got a docker container running a simple python flask application. It's a linux container but my machine is a windows machine. I've attached an external hard drive via USB and it's visible in windows as the E drive. I've given docker permission to access everything at C and E via the settings.

All I want my python to do is display the folder contents from a folder in E. But nothing is found, Previously I was getting an error about the file path not existing. but I was able to fix that, However. now it doesn't even seem to find any contents at my desired path, I've seen examples of how to run a docker container with a volume as a parameter. but I'm running this with docker-compose.

Here's my Dockerfile:

FROM python:3
WORKDIR /code
ENV FLASK_APP practice.py
ENV FLASK_RUN_HOST 0.0.0.0
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . .

CMD ["flask", "run"]

Here's my docker-compose file:

version: '3'
services:
    web:
        build: .
        ports:
            - "5000:5000"
        volumes:
            - .:/code
            - /mnt/e/folderWithStuff/
        environment:
            FLASK_ENV: development
    redis:
        image: "redis:alpine"

The reason that second volume has a path of /mnt/e/folderWithStuffInIt is because that is the path I can access from my Window's machine's Ubuntu subsystem. Which leads me to believe it should be the same path when being accessed from inside a linux container.

The endpoint that I'm hitting does this:

@app.route('/list')
def list_files():
    files = os.listdir("/mnt/e/folderWithStuff")
    result = ""

    for file in files:
        result = result + file

    return result

All I get back is a blank page. The route is good because if I change it to return a string, I see the string at that route. It doesn't complain about the directory so it seems like there should be something in that directory. It just doesn't look like it's actually accessing the folder in the external drive that I've given it permission to access. What am I missing or misunderstanding?

I figured it out!

In the volumes section I have defined in my docker-compose file, you need to create a mapping!

So it should actually look like this:

    volumes:
        - .:/code
        - E:\\files:/mnt/e/files

The path on your machine that is running the container has to be represented on the left side of the: symbol. The path on the right can be whatever you want it to be. Your code will be able to access the path on the right, and the mapping will then let your code access whatever is on the C or E or X drive you've got connected to your host machine.

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