skip to Main Content

I have the following simple script (for testing purposes), which creates a QR code that points to a URL, and saves it to the root directory as a .svg file (except when running the docker container).

print('python code starting')

print('importing modules...')

import pyqrcode
from pyqrcode import QRCode

#String for QR code
qrcode_webpage = "https://www.bbc.com/news"

#Generate QR Code
url = pyqrcode.create(qrcode_webpage)

#Create and save the QR code as a png file
url.svg("news.svg", scale=8)

print('code finished')

Here is the Dockerfile:

# Dockerfile, Image, Container

FROM python:3.8

ADD main.py .

RUN pip install pyqrcode

CMD ["python", "./main.py"]

The problem that I have is when I run the docker container inside VSCode’s Terminal, it doesn’t create the news.svg file. It must’ve saved it somewhere, as it finishes the script (but not sure where?)…

To run the docker container, I’m using the following command:
docker run python-qr

It prints out the following results, but no svg file is saved in the root directory, as oppose to when I’ve previously run python main.py.

Statements it’s printing (as expected).

python code starting
importing modules...
code finished

Is there a way to fix this, so that it saves the file to the root directory?

Thanks in advance

2

Answers


  1. It’s probably because when you run the code in your workspace where Python itself is, it will save the image somewhere in that directory, however when running in Docker it should probably be saving it inside a folder inside the container that is the python code, to make accessing this svg file easier, it would be interesting if you make the workdir clear in the dockerfile so you can already have a path to search.

    Dockerfile:

    FROM python:3.8
    
    WORKDIR /app  #Set the workdir with the name you want
    
    ADD main.py .
    
    RUN pip install pyqrcode
    
    CMD ["python", "./main.py"]
    

    You can also try browsing inside the container to try to find the files you are looking for with:

    docker exec -it your-container /bin/bash
    

    then search for your folder or file with:

    cd /folder_name
    

    but if you just want the images to be saved in your directory and not inside the container you can add this command and it will change the directory in which the images will be saved (put it in the dockerfile)

    volumes: /path/to/your/directory:/folder/in/container
    

    you can see more about that volume here : dockerfile_volume

    Login or Signup to reply.
  2. You can try saving your svg to a particular path and mount a volume to the container to save and access your file

    Start by changing where you save the svg file within your script like below

    ...
    url.svg("/data/news.svg", scale=8)
    ...
    

    The directory where you have your dockerfile and main.py should look like below

    app
    ├── dockerfile
    └── main.py
    

    Once done with the changes, re-build the container

    cd app
    docker build . python-qr
    

    and then start it using a command like below

    docker run -v "/path/to/data:/data" python-qr
    

    Where /path/to/data is a directory on your local machine,once you run the command you should have the svg file at that path

    /data is the directory it maps to on the container

    The reason your solution isn’t working is because the file is probably saved somewhere in /etc/docker/… location on your machine, and while the file is saved on the root dir. within the container you won’t be able to access it since the container is not running

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search