簡體   English   中英

如何解決exceptions.OSError:[Errno 1]不允許操作(泊塢窗容器)?

[英]How to resolve exceptions.OSError: [Errno 1] Operation not permitted (docker container)?

我正在嘗試使用bluepy掃描BLE設備。 我的scan.py代碼是-

from bluepy.btle import Scanner, DefaultDelegate

class ScanDelegate(DefaultDelegate):
    def __init__(self):
        DefaultDelegate.__init__(self)

    def handleDiscovery(self, dev, isNewDev, isNewData):
        if isNewDev:
            print "Discovered device", dev.addr
        elif isNewData:
            print "Received new data from", dev.addr

# prepare scanner
scanner = Scanner().withDelegate(ScanDelegate())

# scan for 5 seconds
devices = scanner.scan(5.0)

for dev in devices:
    print "Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi)
    for (adtype, desc, value) in dev.getScanData():
        print "  %s = %s" % (desc, value)

根據文檔(最后提到為注釋)-

(1) LE scanning must be run as root

這意味着我們需要使用sudo運行腳本。 我以-

sudo python scan.py

基本上, bluepy-helper需要sudo進行掃描。 需要設置blupe-helper的功能來運行不帶sudo的代碼。 根據解決方案 ,我做到了-

sudo setcap 'cap_net_raw,cap_net_admin+eip' /usr/local/lib/python2.7/site-packages/bluepy/bluepy-helper

從終端開始,掃描代碼現在無需sudo運行-

python scan.py

最后,我制作了一個Dockerfile-

FROM arm32v7/python:2.7.15-jessie
WORKDIR /usr/app/gfi_ble
COPY . /usr/app/gfi_ble
RUN chmod 755 ./setcap_for_bluepy_helper.sh
RUN pip install -r requirements.txt
CMD ["./setcap_for_bluepy_helper.sh", "--", "python", "src/scan.py"]

setcap_for_bluepy_helper.sh的內容是-

#!/bin/bash
cmd="$@"
>&2 setcap 'cap_net_raw,cap_net_admin+eip' /usr/local/lib/python2.7/site-packages/bluepy/bluepy-helper
exec $cmd

映像已成功創建,但是當我運行容器時,出現了如下錯誤:

Creating con_gfi_ble ... done
Attaching to con_gfi_ble
con_gfi_ble | 2019-01-12 23:06:24+0000 [-] Unhandled Error
con_gfi_ble |   Traceback (most recent call last):
con_gfi_ble |     File "/usr/app/gfi_ble/src/scan.py", line 17, in new_devices
con_gfi_ble |       devices = scanner.scan(5.0)
con_gfi_ble |     File "/usr/local/lib/python2.7/site-packages/bluepy/btle.py", line 852, in scan
con_gfi_ble |       self.start(passive=passive)
con_gfi_ble |     File "/usr/local/lib/python2.7/site-packages/bluepy/btle.py", line 789, in start
con_gfi_ble |       self._startHelper(iface=self.iface)
con_gfi_ble |     File "/usr/local/lib/python2.7/site-packages/bluepy/btle.py", line 284, in _startHelper
con_gfi_ble |       preexec_fn = preexec_function)
con_gfi_ble |     File "/usr/local/lib/python2.7/subprocess.py", line 394, in __init__
con_gfi_ble |       errread, errwrite)
con_gfi_ble |     File "/usr/local/lib/python2.7/subprocess.py", line 1047, in _execute_child
con_gfi_ble |       raise child_exception
con_gfi_ble |   exceptions.OSError: [Errno 1] Operation not permitted
con_gfi_ble | 

問題: exceptions.OSError:[Errno 1]不允許什么操作?

當我從Terminal運行它時,我的代碼很好。 容器怎么了? 任何想法!

Docker容器以降低的功能運行。 這樣可以通過運行不帶名稱空間的內核命令並訪問容器外部主機的某些部分(例如原始網絡接口或物理設備)來防止容器內部的根逃脫容器。 如果需要,您需要在外部向容器添加功能,但是了解這會降低docker默認設置所提供的安全性。

docker run ,它看起來像:

docker run --cap-add=NET_ADMIN --cap-add=NET_RAW ...

https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities

在撰寫文件中,如下所示:

version: '2'

services:
  app:
    image: your_image
    cap_add:
      - NET_ADMIN
      - NET_RAW

參考: https : //docs.docker.com/compose/compose-file/

這不適用於群體模式。 在群體模式下,添加功能來運行命令的工作正在進行中。 有丑陋的解決方法,如果您需要此。

請注意,您不應在容器內部運行sudo 這樣做意味着一切都可以提升自己的生根,並破壞了以用戶身份運行任何東西的目的。 相反,您應該以root用戶身份啟動容器,並盡快移交給普通用戶,這是一種單向操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM