Dockerizing a python 3 script and saving data to share.


HH0718

Recommended Posts

Hello everyone.

I have created a python script that runs indefinitely. It downloads security camera PNGs from a url and saves it to a folder called images.

I wanted to run this on my Unraid docker but seem to have trouble.

 

I created the dockerfile with the following content.

 

FROM python:3.8
RUN pip install --upgrade pip
COPY main.py /
COPY requirements.txt .
RUN pip install -r requirements.txt
CMD [ "python", "./main.py"]

and I have the following sanitized python 3 script

import itertools
import pendulum
import requests


source = {"source_1": "Source=1_URL",
          "source_2": "Source=2_URL"}


for k, v in itertools.cycle(source.items()):
    print(k, v)
    image_url = v

    img_data = requests.get(image_url)
    img_name = f"images/{k}_{pendulum.now().format('YYYY_MM_DD_HH.mm.ss')}.png"
    with open(img_name, 'wb') as handler:
        handler.write(img_data.content)

When I create the docker image, I added the `/images` path where the container path is `/images' and the host path is a share i made under '/mnt/user/BH/'

 

if I run it and look in the share, there is nothing at all. But if I console into the running image, I see the images.

Can anyone help me understand what I need to do to place the images in the shared folder?

 

I have a feeling I didn't setup the Dockerfile correctly or maybe I need to change the path to my python script.

Thanks for your help.

Link to comment

Are you sure the full path inside the container is really /images (i.e. it as at the top level of the container file system) ?   The code you posted has no leading / on the name which means it is a path relative to the current folder.   When you console into the container you might want to check with the pwd command what is the current folder.

Link to comment

Thanks for the response. When I console in and check pwd I get a simple / which tells me this is the root. and when I list the directory I see the images directory. So I'm not certain how to point to it.

Could you point me to a guide or reference to properly setup my container for this script. I don't expect anyone to do the work for me but I could certainly use a nudge.

Thanks.

Edit: even with / in front of my images directly (in python script) it does not resolve the issue.
 

Edited by HH0718
Supplemental information.
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.