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:
- Create a container with all the necessary environment (client.containers.run)
- Run a separate exec for each step (1st compilation, 2nd run)
- 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?
- 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,
)
- create exec
self.exec['Id'] = client.api.exec_create(self.container.id, command, tty=True, stdin=True)
- 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?