Search code examples
pythondockerdockerfilescapy

Unable to connect to socket in docker: PermissionError: [Errno 1] Operation not permitted


I have an application I am trying to move into a docker container. I have most things working, but the part of the application that requires elevated privileges (to use socket and configure network parameters) does not seem to be working.

What I've tried:

  • Giving the --privileged flag
  • Giving the --cap-add=NET_ADMIN flag
  • Removing security options with --security-opt apparmor=unconfined --security-opt seccomp=unconfined
  • Adding user groups and sudo in the Dockerfile
  • Using the --net=host \ option.
  • Adding my user to the docker group
  • Giving the --cap-add=SYS_RAWIO flag
  • Adding my user to the netdev group in the Dockerfile RUN groupadd -r netdev && usermod -a -G netdev $USER

This is the error I'm getting:

Process Process-2:
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/usr/local/lib/python3.9/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/app/support_files/wizard_configuration_routines.py", line 781, in find_device
    device_list = scan_subnet('192.168.30.0/24',g_interface)
  File "/app/support_files/wizard_configuration_routines.py", line 757, in scan_subnet
    answered, unanswered = scapy.arping(subnet,verbose=False,iface=g_interface)
  File "/usr/local/lib/python3.9/site-packages/scapy/layers/l2.py", line 890, in arping
    ans, unans = srp(
  File "/usr/local/lib/python3.9/site-packages/scapy/sendrecv.py", line 687, in srp
    s = iface.l2socket()(promisc=promisc, iface=iface,
  File "/usr/local/lib/python3.9/site-packages/scapy/arch/linux.py", line 484, in __init__
    self.ins = socket.socket(
  File "/usr/local/lib/python3.9/socket.py", line 232, in __init__
    _socket.socket.__init__(self, family, type, proto, fileno)
PermissionError: [Errno 1] Operation not permitted

Below are the relevant files

Dockerfile

# Slim version of Python
FROM python:3.9-slim

# Download Package Information
RUN apt update -y

# Install Tkinter
RUN apt install tk -y

# Install fontconfig
RUN apt install fontconfig -y

# Install Pillow
RUN python3 -m pip install Pillow
RUN python3 -m pip install ouster-sdk
RUN python3 -m pip install scapy
RUN python3 -m pip install customtkinter
RUN apt install net-tools -y
RUN fc-cache -f -v

# Commands to run Tkinter application
CMD ["/app/SCOT_wizard.py"]
ENTRYPOINT ["python3"]

build.sh

sudo docker build -t tkinter_in_docker .

run.sh

sudo docker run -u=$(id -u $USER):$(id -g $USER) \
 -e DISPLAY=$DISPLAY \
 -v /tmp/.X11-unix:/tmp/.X11-unix:rw \
 -v $(pwd)/app:/app \
 -v $(pwd)/logs:/logs \
 -v $(pwd)/records:/records \
 -v $(pwd)/fonts:/.fonts\
 -w /app \
 --privileged \
 --net=host \
 --rm \
  tkinter_in_docker

In an attempt to understand the problem, I made a simple docker container that has the line that is failing, and it works. I can't figure out why it won't work in the larger application. Here is the minimum example:

Dockerfile

# Slim version of Python
FROM python:3.9-slim

# Download Package Information
RUN apt update -y
RUN apt install net-tools -y
RUN apt install -y libpcap0.8 

RUN python3 -m pip install scapy
# Commands to run Tkinter application
CMD ["SCOT_wizard.py"]
ENTRYPOINT ["python3"]

run.sh

sudo docker run \
 -v $(pwd)/app:/app \
 -w /app \
 --net=host \
 --rm \
  tkinter_in_docker

/app/SCOT_wizard.py

import scapy.all as scapy
answered, unanswered = scapy.arping('192.168.11.0/24',verbose=False,iface='eno2')
print(answered)

Solution

  • I was able to find a solution online that lets me get x-server to work while root. This solves my problem, but I am open to a better way to get this to work without having to run a command on the host machine.

    Command in question and source. Run this command on the Docker host machine.

    xhost +local:docker