How to use docker-py socket to communicate with the container?

  docker, python, terminal

I am trying to provide the user with the ability to run a virtual machine on my server by providing them with docker containers. I figure out that you can use docker attach on your server but I want ot stream the output to the user. However, when I try to connect to the container socket to provide an I/O stream, it does not seem to work at all and I could not find any docs on these behaviors. My code is as below:

socket = container.attach(stdin=True, stdout=True, stderr=True)
#socket._sock.send(command + 'rn'.encode('utf-8')
data = socket.readall().decode('utf-8')
print data

Some command seems to pass such as pip install but pip uninstall and apt update usually returns nothing and does not run in the container at all. I check this by running pip freeze inside the container using my terminal. My question is there anyway to fix my code or it is just not possible to do what I want at all?

Source: Docker Questions

LEAVE A COMMENT