Running and Debugging a Python Barcode App in Docker Container
Tags: python, docker, containers, programming
Published: 2023-08-16 09:55:38

When creating Python barcode extensions using CPython and Dynamsoft Barcode reader, one of the major concerns you have to face is to test your code compatibility with different versions of Python. 

To test if your Python barcode extension works in your Windows, Linux or macOS you will need to have each version of Python installed on your computer. However, it’s time consuming. 

Here, we have an easy hack for you! Instead of installing all those Python versions, you can use Docker container.  


## Creating Docker Image with Python Barcode SDK
 

First of all, download Docker desktop and install it.  

If your system does not have Python 3.8, then go to Docker hub and pull out a Docker image that contains Python 3.8. Now invoke the command from the command prompt: 

`docker run -it --rm python:3.8 bash` 

If the docker image does not exist, the command given above will by itself trigger the download. 

![Create Docker Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vzmdpjvqxtoh31qz87zz.png)

Next you can create a Dockerfile to create a fresh Docker image that will include the [Dynamsoft Barcode Reader SDK](https://www.dynamsoft.com/barcode-reader/overview/): 
 

`FROM python:3.8 
RUN pip install dbr `
 
To generate a Docker image: 

`docker build --rm -t yushulx/dynamsoft-barcode-reader:python-3.8`  

Further run a container test. It will help you validate if your Python based Dynamsoft Barcode Reader is working fine or not.  
 
`docker run -it --rm yushulx/dynamsoft-barcode-reader:python-3.8` 

![Validate Python Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c9l9b3kjuyn7dtj6miux.png)

Next you need to publish the Docker Image to your Docker Hub after your container is validated: 

`docker login 
docker push yushulx/dynamsoft-barcode-reader:python-3.8 `

You can visit the below given link to test the image:

https://hub.docker.com/repository/docker/yushulx/dynamsoft-barcode-reader. 


## Mounting Local Folder to Docker Container
 
To mount a local Window folder to Linux Docker Container, execute the Python script using Python 3.8. 

After that, select the shared drive from the settings of the Docker desktop: 

![Docker Shared Device](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yvrvo3720iv1hnz63fwd.png)
 
To mount the Windows folder to the Linux container execute the following command: 

`docker run -it --rm -v d:/code/docker:/dynamsoft yushulx/dynamsoft-barcode-reader:python-3.8  bash` 


![Execute Linux Command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nddxcnf0yp8xp2f0x3pc.png) 
 

## How To Debug Python Code with Visual Studio Code Remote-Containers
 

The preferred option to write and debug the Python code is to use Visual Studio Code. 

To start with it, first install Docker extension for VSCode as shown below: 

![VSCode Extention](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z4xizykoqdmac4h1cekp.png)

Click F1 key to execute the “Attach to Running Container” option. 

![Running Container](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6qa4igjruuqw7vnkk47.png)

Double-click on the project folder. 

![Docker Project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iapqpdxsgmq2074pskcn.png)

![Python Project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnkmtucxzuxgdn3bop5e.png)
 
It will allow you to edit the Python file in VSCode. Next install the Python debugger for the container to debug the code:  

![Install Python Debugger](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qd9mn0lxx0jalotvsn0h.png)

For debugging of the code remotely, click F5. 

![Code Debug](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5tffrpwderwve3v700vl.png)

![Python Debug](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kz3upy1v374mqipqb0vw.png)
The complete Python barcode detection code is given below:

```
import os 
 

from dbr import DynamsoftBarcodeReader 

dbr = DynamsoftBarcodeReader() 

 
 

def InitLicense(license): 

    dbr.InitLicense(license) 

 
 

def DecodeFile(fileName): 

    try: 

        results = dbr.DecodeFile(fileName) 

        textResults = results["TextResults"] 

        resultsLength = len(textResults) 

        print("count: " + str(resultsLength)) 

        if resultsLength != 0: 

            for textResult in textResults: 

                print(textResult["BarcodeFormatString"]) 

                print(textResult["BarcodeText"]) 

                localizationResult = textResult["LocalizationResult"] 

                x1 = localizationResult["X1"] 

                y1 = localizationResult["Y1"] 

                x2 = localizationResult["X2"] 

                y2 = localizationResult["Y2"] 

                x3 = localizationResult["X3"] 

                y3 = localizationResult["Y3"] 

                x4 = localizationResult["X4"] 

                y4 = localizationResult["Y4"] 

                localizationPoints = [(x1,y1),(x2,y2),(x3,y3),(x4,y4)] 

                print(localizationPoints) 

        else : 

            print("No barcode detected") 

    except Exception as err: 

        print(err) 

 
 

if __name__ == "__main__": 

    # Get the license from https://www.dynamsoft.com/customer/license/trialLicense/ 

    licenseKey = "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="  

    fileName = r"test.jpg" 

    InitLicense(licenseKey) 

    dir_path = os.path.dirname(os.path.realpath(__file__)) 

    DecodeFile(os.path.join(dir_path, fileName)) 
```