在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
。
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"
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"]
start.sh
=> sudo modprobe rtc-ds1307
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
我正在使用docker-compose up -d
命令运行docker容器。
# echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
bash: echo: write error: Invalid argument
# cat /sys/class/i2c-adapter/i2c-1/new_device
cat: /sys/class/i2c-adapter/i2c-1/new_device: Input/output error
# 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
我通过使用以下命令进入docker容器,尝试了问题部分的第2点和第3点
docker exec -it --privileged --user=root <container_id> /bin/bash
我已验证echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
在主机操作系统上是否正常运行,没有任何问题。