Docker error : No access to /dev/mem. Try running as root

送分小仙女□ 提交于 2020-01-05 03:27:23

问题


I have a raspberry pi and I have installed dockers in it. I have made a python script to read gpio status in it. So when I run the below command

sudo docker run -it --device /dev/gpiomem app-image

It runs perfectly and shows the gpio status. Now I have created a docker-compose.yml file as I want to deploy this app.py to the swarm cluster which I have created.

Below is the content of docker-compose.yml

version: "3"
services:
    app:
        image: app-image
        deploy:
            mode: global
        restart: always
        privileged: true

When I start the deployment using sudo docker stack deploy command, the image is deployed but it gives error:

No access to /dev/mem. Try running as root

So it says that it do not have access to /dev/mem, but this is very strange when I am using device, why the service do not have access. It also says trying running as root which I think all the containers are in root already. I also tried giving the full permissions to the file by including the command chmod 777 /dev/gpiomem in the code but it still shows this error.

My main question is that when it runs normally using docker run.. command why it is showing error in docker-compose file when deploying using sudo docker stack deploy.? How to resolve this issue.?

Thanks


回答1:


As stated in docker-compose devices

Note: This option is ignored when deploying a stack in swarm mode with a (version 3) Compose file.

The devices option is ignored in swarm. You can use privileged: true which will give access to all devices.




回答2:


Adding devices, capabilities, and using privileged mode are not supported in swarm mode. Those options in the yml file exist for using docker-compose instead of docker stack deploy. You can track the progress on getting these features added to swarm mode in github issue #24862.

Since all you need to do is access a device, you may have luck adding the file for the device as a volume, but that's a shot in the dark:

volumes:
- /dev/gpiomem:/dev/gpiomem


来源:https://stackoverflow.com/questions/48441737/docker-error-no-access-to-dev-mem-try-running-as-root

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!