在docker容器中为i2c运行echo命令时面临的问题

时间:2019-07-15 12:00:35

标签: linux docker raspberry-pi docker-compose echo

在docker容器中运行命令echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device时遇到问题。

配置=>

我有一个树莓派3 b +

主机操作系统是Raspbian。

Docker版本 =>

# docker --version
Docker version 18.09.0, build 4d60db4

Docker撰写版本 =>

# docker-compose --version
docker-compose version 1.24.1, build 4667896

例如,我有一个Docker容器,我们将其命名为Test

  1. docker-compose.yml =>
version: '3'
    Test:
    volumes:
      - '/etc/localtime:/etc/localtime:ro'
      - '/sys/class/i2c-adapter/i2c-1/new_device:/sys/class/i2c-adapter/i2c-1/new_device'
      - '/lib/modules:/lib/modules'
      - '/dev:/host/dev'
      - '/proc:/host/proc'
      - '/boot:/host/boot'
      - '/lib/modules:/host/lib/modules'
      - '/usr:/host/usr'
    build:
       context: .
       dockerfile: Test/Dockerfile
    privileged: true
    restart: always
    tty: true
    network_mode: host
    cap_add:
     - SYS_RAWIO
    devices:
     - "/dev/i2c-1:/dev/i2c-1"
     - "/dev/mem:/dev/mem"
     - "/dev/ttyACM0:/dev/ttyACM0"
  1. Dockerfile =>
FROM balenalib/armv7hf-debian-python:3-stretch

RUN apt-get update && \
    apt-get install -y python-smbus \
    i2c-tools \
    kmod \
    ntpdate

ENV INITSYSTEM=on
ENV DBUS_SYSTEM_BUS_ADDRESS unix:path=/host/run/dbus/system_bus_socket

USER root

ENV UDEV=on

WORKDIR /usr/src/app/Test

COPY Test .

# Adding Permission for running script daily
RUN ["chmod","+x","/usr/src/app/Test/Others/TestScript.sh"]

CMD ["/usr/src/app/Test/start.sh"]
  1. start.sh =>
sudo modprobe rtc-ds1307
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

我正在使用docker-compose up -d命令运行docker容器。

问题=>

  1. 运行容器时,出现以下错误=>
# echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
bash: echo: write error: Invalid argument
  1. 我还尝试检查cat命令是否适用于上述路径
# cat /sys/class/i2c-adapter/i2c-1/new_device
cat: /sys/class/i2c-adapter/i2c-1/new_device: Input/output error
  1. 要确认回声是否正常,我尝试了以下命令=>
# echo 'hello world' > /root/test.txt
# cat /root/test.txt
hello world

--- Another try ---

# echo ds1307 0x68 > /root/test.txt
# cat /root/test.txt
ds1307 0x68

注意=>

  1. 我通过使用以下命令进入docker容器,尝试了问题部分的第2点第3点 docker exec -it --privileged --user=root <container_id> /bin/bash

  2. 我已验证echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device在主机操作系统上是否正常运行,没有任何问题。

0 个答案:

没有答案