The observation is that some scenarios in which I'm using .with_exec to write to a file using echo don't always work.
This always works, when the container is created client.container().from_() and chain the .with_exec command:
ubuntu_container = (
client.container()
.from_("ubuntu:latest")
.with_exec(
[
"sh",
"-c",
(
"echo -n Hello World > /tmp/sandbox_test.txt"
),
])
)
file_contents = await ubuntu_container.file("/tmp/sandbox_test.txt").contents()
print(f"==== file contents 1 /n| {file_contents }"
What also works is if I take my ubuntu_container object and run a .with_exec command and set the result of that to a variable:
ubuntu_container_with_newFile = ubuntu_container.with_exec(
[
"sh",
"-c",
(
"echo -n Hello World 2 > /tmp/sandbox_test2.txt"
),
])
# This works, and creates sandbox_test2.txt
newFile_contents = await ubuntu_container_with_newFile.file("/tmp/sandbox_test2.txt").contents()
print(f"==== New file contents 1 /n| {newFile_contents}")
What does not work, is just calling .with_exec and not setting to a var, it throws an error upon read with the file not existing
ubuntu_container.with_exec(
[
"sh",
"-c",
(
"echo -n Hello World > /tmp/sandbox_test3.txt"
),
])
What am I not understanding?
I would assume you can use the same obj/var to execute multiple commands against the container.