0

I'm writing something like a website to run code in various languages. I chose docker as a way to isolate the process from the internet and the file system. Typical docker usage example:

  1. Create a container with all the necessary environment (client.containers.run)
  2. Run a separate exec for each step (1st compilation, 2nd run)
  3. For each step, analyze - the return code of the process, stdout and stderr

I have a problem. For each step, I get a socket to read and write information to the running exec . But it mixes stdout and stderr. I read that there is an attach_socket method and you can attach multiple sockets, but this only works for a running container (docker run), how do I start a separate socket for docker exec?

  1. create container
# I start the compiler container with command expectation
self.container = client.containers.run(
            'python:3.10',
            command=f"sleep infinity",
            working_dir='/app',
            volumes=[f'{Path(self.folder.name)}:/app'],
            tty=True,
            detach=True,
            network_disabled=True,
        )
  1. create exec
self.exec['Id'] = client.api.exec_create(self.container.id, command, tty=True, stdin=True)
  1. run exec, return socket to write and read
self.socket = client.api.exec_start(self.exec['Id'], socket=True, tty=True, demux=True)

I want to use attach_socket but this only works for container (self.container.id) but not for command (self.exec['Id'])

Is there any way to connect 2 sockets separately. One for stdout and one for stderr?

semwai
  • 1
  • 1
  • I think you're looking for [these docs](https://docker-py.readthedocs.io/en/stable/user_guides/multiplex.html); it doesn't look like you can get a separate socket but you can get separate streams for stdout and stderr. – larsks Oct 22 '22 at 13:22
  • @larsks Thanks, this is a good solution and I tried it, but the function only returns a pair for two elements to read. It is not clear how to write to stdin. In contrast, you can write data to the socket, but the demux parameter does not separate the output of the socket – semwai Oct 22 '22 at 14:21

0 Answers0