• 15
    Feb - 2019

    Raspberry Pi
    5 min | 4809

    #Raspberry Pi: CUPS Printer Server using Docker

    Raspberry Pi | 5 min | 4809


    cup server
    docker
    dockerized
    print server
    printer
    raspberry pi
    tutorial

    A good friend of mine has an old printer and he was always thinking of buying some adapter to make it Wi-Fi compatible. He actually bought one, but it was not compatible! I told him to buy a Raspberry Pi W Zero and build a CUPS Print Server, but he did not have time to do that, so I planned as XMas gift! I hope he is not going to read this before he gets his present. :) - If you received this as a gift, your instructions are here.

    A big challenge was to make it plug & play. I did not want to have his Wi-Fi credentials, and he should not use a terminal over SSH to configure it. I looked for some Raspberry Pi Access Point (AP) + Client, so that he could connect directly to the Raspberry Pi using Wi-Fi (AP), and used some webserver to configure the credentials for his Wi-Fi network (client), so that he could access the Raspberry Pi from the home network and configure the printers. The second thing that I wanted, was to build a micro service application. That means, everything is configured and running using Docker!

    Basically, there were three things that I needed:

    1. A Raspberry Pi AP + Client
    2. A webserver to configure the Raspberry WiFi Client (wpa_supplicant file)
    3. A CUPS print server

    I added another step: Portainer. This can help me in case that I need to update the Docker images: he can "recreate" the containers and pull the new Docker images using Portainer.

    This is what we will try to accomplish in this tutorial:

    1. Install a Raspberry Pi AP + Client
    2. Install a webserver and its files for configuration purpose
    3. Install a CUPS print server
    4. Run everything using Docker.

    Raspberry Pi Access Point (AP) + Client

    I was looking on the Internet to find a wpa_supplicant configurator that runs on a Docker container and I found the cjimti/iotwifi project ...

    This project is intended to aid in developing "configure wifi over wifi" solutions for IOT projects using the Raspberry Pi.

    ... and that was what I needed. Typing the following:

    # iotwifi network and AP configuration - you need to change
    # the password (and the ssid). If you change the network ip range "192.168.27.1", you need
    # to change the ip address inside the js/main.js file (webserver - next point).
    wget https://raw.githubusercontent.com/cjimti/iotwifi/master/cfg/wificfg.json
    
    # prevent wpa_supplicant from starting on boot
    sudo systemctl mask wpa_supplicant.service
    # rename wpa_supplicant on the host to ensure that it is not used.
    sudo mv /sbin/wpa_supplicant /sbin/no_wpa_supplicant
    # kill any running processes named wpa_supplicant
    sudo pkill wpa_supplicant
    
    # run the Container
    sudo docker run -d --restart unless-stopped --privileged --net host -v $(pwd)/wificfg.json:/cfg/wificfg.json  cjimti/iotwifi

    The first bullet point of this tutorial is accomplished!

    Webserver

    The iotwifi Docker can be configured using the available REST api. curl, postman can be used to configure the server, it needs something like this:

    # post wifi credentials
    $ curl -w "\n" -d '{"ssid":"home-network", "psk":"mystrongpassword"}' \
         -H "Content-Type: application/json" \
         -X POST <ip-access-point>:8080/connect

    But as I said, I wanted a solution without the use of a terminal or another program to configure the AP. Then, I looked for some small webserver (of course Dockerized!), and the txn2/asws project was also a nice solution.

    A very small web server written in Go for static sites served on devices such as the Raspberry Pi.

    Then I wrote some HTTP, JavaScript and I used Bootstrap to "make it lovely!" (see Fig. 1). I needed the available SSIDs, and it was possible to get them using the REST API from cjimti/iotwifi. You get a JSON response, if you visit the <ip-access-point>:8080/scan URL. With this information, I filled a selector element with the network SSIDs (see Fig. 1).

    Interface to configure the Raspberry Pi WiFi client to connect to the network.
    Fig. 1: Interface to configure the Raspberry Pi Wi-Fi client to connect to the network.
    If you change the network ip address inside the `wificfg.json` file, you need to change the IP address inside `js/main.js` too.

    To deploy the container with the webserver, you need the following:

    # download www files"
    git clone https://github.com/lemariva/wifi-cups-server.git
    mv wifi-cups-server/iot-website $(pwd)/www
    
    # run the Container
    sudo docker run -d --restart unless-stopped -e DEBUG=true -p 80:80 -v $(pwd)/www:/www txn2/asws:armhf-1.2.3

    Then, the second bullet point of the tutorial is also done!

    CUPS Print Server

    There are a lot of tutorial for CUPS on Raspberry Pi, and I also found a Docker image, but with an old version of CUPS. Then, I decided to build my own Docker image: lemariva/rpi-cups. It contains almost every driver that you could need, but, if you can give me some advice of another drivers, I will update the container ASAP.

    The third bullet point of this tutorial can be also done typing the following:

    sudo docker run -d --restart unless-stopped -p 631:631 --privileged -v /var/run/dbus:/var/run/dbus -v /dev/bus/usb:/dev/bus/usb lemariva/rpi-cups

    This starts the container with the CUPS Print Server, then you can point to <ip-address>:631/ and add your printers. If you need a username and password, they are print and print. Sorry, if you want to change the password, just use the Portainer console to connect to the container and change the password of the print user.

    How to use the rpi-cups server!

    I prepared a bash file, that automatically installs everything that you need. It also pulls the Docker images. You can download it on your Raspberry Pi and run it using:

    $ wget https://raw.githubusercontent.com/lemariva/wifi-cups-server/master/rpi-cup-server.sh
    $ chmod +x rpi-cup-server.sh
    $ ./rpi-cup-server.sh

    As you see, the Raspberry Pi should be connected to the Internet to download and install everything. Then, the credentials are deleted (wpa_supplicant.conf) The bash file asks you for a SSID and a PASSWORD. These values are for the AP. Choose them correctly: it should not be your/neighbor network SSID, and the PASSWORD should be secure. After this, the bash file starts to install everything automatically (grab a cup of tea!).

    You will see the AP (SSID that you configured or got) as an available Wi-Fi network. Connect to it using the credentials that you configured or you got. Go to `http://192.168.27.1`, if it does not pop-up, and select your SSID (home Wi-Fi network) and enter the password (of your Wi-Fi). Wait a couple of minutes, or reboot you Raspberry Pi. Connect again to the AP, and go to `http://192.168.27.1:8080/status`. You should get something like this (in one line):
    {
    "status":"OK",
    "message":"status",
    "payload":{
    "address":"b8:27:eb...",
    "bssid":"c8:0e:14...",
    "freq":"2462",
    "group_cipher":"CCMP","id":"0",
    "ip_address":"192.168.178.115",
    "key_mgmt":"WPA2-PSK",
    "mode":"station",
    "p2p_device_address":"4e:06:...",
    "pairwise_cipher":"CCMP",
    "ssid":"L...",
    "uuid":"a7b5...",
    "wpa_state":"COMPLETED"}
    }

    That means that the Raspberry Pi is connected to your home network. Then, connect to your home Wi-Fi network, and go to the address reported by the status. In my case e.g. 192.168.178.115. The CUPS Print Server is running on port 631 (e.g. http://192.168.178.115:631) (see Fig. 2). Go to that URL, and add your printers (user: print, password: print) and have fun!

    CUPS Print Server running
    Fig. 2: Interface to configure the Raspberry Pi WiFi client to connect to the network.

    Conclusion

    This tutorial helps you to setup a dockerized CUPS Print Server together with an AP. You don't need to use a terminal/console to configure it, and you can give it as a gift for XMas! ;P


    **Note:** If you need the PPD files from HP, download this file: foo2zjs. I've installed them, but sometimes you need them again! ;P


    Comments

    Diego 09.30.2019

    Hey buddy!

    I'm having some issues to correctly print with my HP M1132, can you please take a look and see if you can help me?

    I've recorded my screen and uploaded to YouTube: https://www.youtube.com/watch?v=jY0Mma-jDRg

    Basically I can use cups outside docker and it works flawlessly, but via docker it just doesn't work at all.

    Oh! And besides cups, would you be able to integrate SANE in your docker image + a way to use it without specific windows apps? Maybe using a web interface like this image is using (I couldn't make it to work properly aswell, maybe because the image is too outdated). https://hub.docker.com/r/toxic0berliner/docker-rpi-scan-server

    Thanks in advance!