How to execute printing from an application running into a container

I am creating 2 containers with the docker-compose.yml, first container is a MariaDB and the second is a Vaadin application. These two containers are communicating with via a custom network

    networks:
      - my-net

In the app there is a functionality that prints a barcode.png on a label printer with specific dimensions. This function works perfectly when it’s not running in the docker container, of course because the printer can be detected.

So basically I have a layout and generate a button to trigger the printing

     HorizontalLayout actionButtonsLayout = new HorizontalLayout(
         createNewDialog(ext), 
         createNewExt(clService, ext), 
         extBtn,
         BarcodeGenerator.createBarcodeButton(ext.getSerialNumber())); //<---THIS BUTTON

Inside the BarcodeGenerator class I have multiple methods but the one with the problem is the below:

public static void printBarcode(BufferedImage sticker) {
    try {
        PrinterJob printerJob = PrinterJob.getPrinterJob();

        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
        PrintService ql700Printer = null;
        for (PrintService ps : printServices) {
            System.err.println("Printer name found --- " + ps.getName());
            if (ps.getName().contains("Brother QL-700")) {
                ql700Printer = ps;
                break;
            }
        }

        if (ql700Printer == null) {
            System.err.println("Error while printing. No printer found.");
            throw new IllegalStateException("Brother QL-700 not found.");
        }

        PageFormat pageFormat = printerJob.defaultPage();
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
        Paper paper = pageFormat.getPaper();

        // Here I do label configuration 
        // .
        // .
        // .
        //

        try {
            printerJob.print();
            System.out.println("✅ Print job sent successfully.");
        } catch (PrinterException e) {
            e.printStackTrace();
        }

    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Error while printing");
    }
}

Providing the above method in order to give context on how I do the printing in the app.

I’ve searched the forums for an answer to my problem for the last 2 days but everything that I tried seems to not be working for my situation.

How can I detect that specific printer that is pluged in with USB and print the label when the app runs in a container? Any guidance will be appreciated. Thank you.

You created the topic in the Docker Desktop category, so if you run Docker Desktop, everything is in a virtual machine not on the actual host. In Docker CE you could probably mount the USB device, but not in Docker Desktop for which you can check this documentation

1 Like

Yes, I use Docker Desktop and now I get it why it did not work when I was just mounting the USB device. I will read this and try it out ASAP. Thank you for your feedback!