如何让 CAN 总线在 docker 容器内运行?

How to make CAN bus operate from within a docker container?

我在 Beaglebone black 上成功 docker 化了我的 nodejs 应用 运行ning。但是现在我想从 docker 容器中访问 CAN 但它不起作用。

请注意,我已成功配置主机中的 CAN 端口,并且 candump 显示它可以正常工作。

$ 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

我在我的 Dockerfile 中构建了 socketcan 节点模块,如下所示:

 #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 
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 

一旦我从容器 运行 我的应用程序,我得到以下异常:

 -- -  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 

将网络接口转发到 Docker 或多或少在此处进行了描述:Sharing virtual network with docker container

感谢帮助我找到解决方案的贡献者。

终于发现让容器访问宿主机CAN总线最简单的方法就是使用--network参数如下:

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