Docker Community Forums

Share and learn in the Docker community.

Facing issue in running echo command for i2c inside docker container

I am facing an issue when running the command echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device inside a docker container.

Configuration =>

I have a raspberry pi 3 b+

Host os is Raspbian.

Docker version =>

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

Docker compose version =>

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

I have a Docker container, for instance, let’s name it 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"

2.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"]

3.start.sh =>

sudo modprobe rtc-ds1307
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

I am running the docker container using docker-compose up -d command.

Problem =>

1.When I run my container I get the following error =>

# echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
bash: echo: write error: Invalid argument

2.I have also tried checking if the cat command works for the above path

# cat /sys/class/i2c-adapter/i2c-1/new_device
cat: /sys/class/i2c-adapter/i2c-1/new_device: Input/output error

3.To confirm if the echo was working properly I tried the following command =>

# 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

Note=>

1.I have tried point 2 and point 3 from problem section by entering the docker container using the following command => docker exec -it --privileged --user=root <container_id> /bin/bash

2.I have verified if the echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device is working properly on the Host OS without any issue.