简体   繁体   中英

Shared volume between Docker containers with python code

Maybe i'm going at it wrong, but i can't seem to get a shared volume working between two docker containers running custom python code.

I'm using the following docker-compose.yml:

version: "2"

services:
    rabbitmq:
        image: username/rabbitmq
        ports:
            - 15672:15672
            - 5672:5672
    producer:
        image: username/producer
        depends_on:
            - rabbitmq
        volumes:
            - pdffolder:/temp
    consumer:
        image: username/consumer
        depends_on:
            - producer
        volumes:
            - pdffolder:/temp
volumes:
    pdffolder:

The idea is that the producer service polls an exchange server for information and a pdf-file. The consumer service then has to send this information and pdf-file elsewhere. During this action I have to store the pdf locally temporally.

I access the volumes from the custom python-code like this:

producer

# attachment = object I get when requesting attachments from an exchange server

# path to pdf to be saved
pdf_path = os.path.join("temp", attachment.name)

with open(pdf_path, 'wb') as f:
    f.write(attachment.content)

# now in this container, /temp/attachment.pdf exists. I then send this path in a message to the consumer (along with other information)

consumer

# consumer tries to find path created by producer (/temp/attachment.pdf) via
pdf_path = os.path.join("temp", "attachment.pdf")

Via the command line i can see that the producer-container is writing the files to temp/attachment.pdf like expected. The consumer-container however sees no files (resulting in errors).

Btw, I am running the containers on docker for windows

I think I figured out what was wrong. I used the following in both the Dockerfiles for the producer and consumer:

FROM python:3.7-slim
WORKDIR /main
ADD . /main
RUN pip install --trusted-host pypi.python.org -r requirements.txt
CMD ["python", "-u", "main.py"]

Because I moved the python code to the /main folder in both containers, the temp folder created later (via docker-compose) was to be found at /main/temp, and not just /temp. A little bit weird because the main.py should be at the same level as /temp, but hey it works. I got it working with the following docker-compose.yml:

version: "2"

services:
    rabbitmq:
        image: username/rabbitmq
        ports:
            - 15672:15672
            - 5672:5672
    producer:
        image: username/producer
        depends_on:
            - rabbitmq
        volumes:
            - pdffolder:/main/temp
    consumer:
        image: username/consumer
        depends_on:
            - producer
        volumes:
            - pdffolder:/main/temp
volumes:
    pdffolder:

So i guess the steps to debugging this are:

  • Check the spelling of all mentions of volumes in the docker-compose.yml file
  • Check the way paths are being built/referenced in the python code (Linux uses a different format to windows)
  • Check if the paths that have to be accessed from the python code actually exist

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