-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-EXECenv-docker.py
More file actions
35 lines (27 loc) · 898 Bytes
/
Copy pathpython-EXECenv-docker.py
File metadata and controls
35 lines (27 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'''code Interpreter'''
import docker
import os
# Get current script absloute path
current_directory = os.path.abspath(os.path.dirname(__file__))
# Connect to Docker daemon
client = docker.DockerClient(base_url='npipe:////./pipe/docker_engine')
try:
# Create and start the container
container = client.containers.run(
image="python:3.11-slim",
name="my-python-exec-env",
volumes={
current_directory: {'bind': '/app', 'mode': 'ro'} # Mount local files to the app directory of the container.
},
command='python /app/script.py',
detach=True
)
# Wait for the container to finish and retrieve logs
container.wait()
container_logs = container.logs()
result = container_logs.decode('utf-8')
# Print the logs
print(result)
finally:
# Clean up the container
container.remove(v=True, force=True)