When I was using CPython to create the Python barcode extension with Dynamsoft Barcode Reader, I had to take concern for the compatibility of Python versions. However, I’m reluctant to install all Python versions in my operating system. To test the compatibility of my Python barcode app in Windows, I picked Docker container as the workaround.
Creating Docker Image with Python Barcode SDK
Install Docker desktop for Windows.
I don’t have Python 3.8 installed, so I pulled a Docker image that contains Python 3.8 from Docker hub and run it instantly:
docker run -it --rm python:3.8 bash
The command above will automatically trigger the download if there’s no target docker image existed.
To build a new Docker image that contains the Dynamsoft Barcode Reader SDK, create a simple Dockerfile:
FROM python:3.8 RUN pip install dbr
Build the Docker image:
docker build --rm -t yushulx/dynamsoft-barcode-reader:python-3.8 .
Run a container to test whether Dynamsoft Barcode Reader for Python can work:
docker run -it --rm yushulx/dynamsoft-barcode-reader:python-3.8
If there’s no problem, publish the Docker image to Docker hub :
docker login
docker push yushulx/dynamsoft-barcode-reader:python-3.8
If you want to test the image, please visit https://hub.docker.com/repository/docker/yushulx/dynamsoft-barcode-reader.
Mounting Local Folder to Docker Container
I’m going to mount a Windows folder to the Linux container and run a Python script using Python 3.8.
Open the settings of the Docker desktop and select the shared drive:
Run the following command to mount the Windows folder to the Linux container:
docker run -it --rm -v d:/code/docker:/dynamsoft yushulx/dynamsoft-barcode-reader:python-3.8 bash
Debugging Python Code with Visual Studio Code Remote-Containers
I like to use the Visual Studio Code to write and debug Python code.
Install Docker extension for VSCode.
Press F1 to run the “ Attach to Running Container “ option.
Open the project folder.
I can now edit the Python file in VSCode. To debug the code, install the Python debugger for the container first.
Then you can press F5 to debug the code remotely.
Here is the full Python barcode detection code:
Originally published at https://www.codepool.biz on January 14, 2020.