0

I am trying to compile and run python code in Docker.

Dockerfile

FROM python:3
WORKDIR /app
USER root
ADD . .
RUN chmod a+x ./main.py
RUN chmod a+x ./run.sh
ENTRYPOINT ["sh","./run.sh"]

run.sh

#! usr/bin/env bash
timeout --signal=SIGTERM --foreground 500 python3 main.py
exit $?

python code (using docker sdk for python):

client = docker.from_env()

client.images.build(path="./Dockerimagefolder/",tag="sample322")

container = client.containers.create(image="sample322", stdin_open = True)
container.start()
time.sleep(5)
s = container.attach_socket(params={'stdin': 1, 'stream': 1})
s.send('test'.encode())

container log:

Begin script

Enter your nameyou entered

test

as you can see, "you entered" is not shown on the next line, instead, it's placed in the same line as input. It also doesn't show the input that was sent i.e test

I am also unable to get the output, the log I shown above is from the docker app

print(container.logs())
xineta5158
  • 117
  • 1
  • 6
  • What's the eventual goal of this setup? A more typical approach is to set up a single long-running container with a network interface, maybe using an HTTP server like Flask or communicating over a message bus like RabbitMQ. Then you can use ordinary client libraries to interact with it, without needing the Docker SDK (and its attendant security implications). – David Maze Sep 18 '21 at 12:40
  • i have tried using subprocess.popopen for this, and attemtping to send inputs to container and trying to send output to no avail, the existing code i have got is the working one so far – xineta5158 Sep 18 '21 at 12:41

0 Answers0