skip to Main Content

I successfully dockerized my nodejs app running on a Beaglebone black. But now I’d like to access to the CAN from within the docker container but it doesn’t work.

Note that I successfully configured the CAN ports in the host and candump shows it works.

$ candump can1
  can1  18FF30D0   [8]  00 00 00 00 00 00 00 00
  can1  18FF02D0   [4]  00 00 00 00
  can1  18FF21D0   [3]  00 00 00
  can1  18FF3CD0   [4]  00 7D 28 7D
  can1  18FF30D0   [8]  00 03 00 00 00 00 00 00
  can1  18FF02D0   [4]  00 00 00 00

I build socketcan node module in my Dockerfile as follows:

 #If you don't have node/npm already, add that first
RUN apk add --no-cache nodejs

# Add the necessary build and runtime dependencies (see https://stackoverflow.com/questions/36202095/node-serialport-failing-on-alpine-linux)
RUN apk add --no-cache make gcc g++ python3 linux-headers udev

#RUN apk add --no-cache --virtual .gyp python3 make g++
RUN  npm install 

Once I run my app from the container, I get the following exception:

 -- 🟑- 🚎 Canbus:can0: switched to channel "can0"…
Pepsr v2.1.192 5:36:40 PM πŸ“ˆ [pepsr-iingenierie]  -- 🟑- 🚎 Canbus:can0: cancel CAN as other channel "can0" doesn’t work either
Pepsr v2.1.192 5:36:40 PM πŸ“ˆ [pepsr-iingenierie]  -- πŸ”΄- 🚎 Canbus:can0: Error: Error while creating channel
    at Object.exports.createRawChannel (/home/debian/Desktop/devel/iot/node_modules/socketcan/socketcan.js:38:12)
    at Canbus._connect (/home/debian/Desktop/devel/iot/pepsr.ddk.protocol.canBus.js:308:34)
    at Canbus.connect (/home/debian/Desktop/devel/iot/pepsr.module.js:272:9)
    at Object.<anonymous> (/home/debian/Desktop/devel/iot/pepsr.js:608:20)
    at Module._compile (internal/modules/cjs/loader.js:1015:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
    at Module.load (internal/modules/cjs/loader.js:879:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47 

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to the contributors who led me to a solution.

    I finally found that the easiest way to let the container access to the host CAN bus is to use the --network parameter as follows:

    $ docker run --rm --network=host <your image>
    

  2. Forwarding network interfaces to Docker is more or less described here: Sharing virtual network with docker container

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