Created
October 23, 2024 22:09
-
-
Save pythoninthegrass/ab2a3fbc253951aa460a8d1c7f7e5c1a to your computer and use it in GitHub Desktop.
Use the python docker sdk to run a container in detached mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import docker | |
client = docker.from_env() | |
container = client.containers.run("ubuntu:latest", | |
command=["echo", "hello", "world!"], | |
detach=True, | |
remove=False, | |
stdout=True, | |
stderr=True | |
) | |
def main(): | |
result = container.wait() | |
logs = container.logs() | |
print(logs.decode('utf-8')) | |
container.remove() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment