docker / docker/docker-py

Inconsistent and cut off output from client.containers.run()

Open
#3,072 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.2k
Forks
1.7k
Avg merge
13d 8h
Merged PRs (30d)
2

Description

Hello,

I have a maybe specific use case.

I am compiling and running C code inside a docker container.

Here is my python code for running the container:


        with open(f"{abs_path}/pyput", "w") as f:
            f.write(user_input)
        f.close()
.....

        container_run_result = client.containers.run(
            image="gcc",
            remove=True,
            stderr=True,
            volumes=[f"{abs_path}:/usr/src/compiled"],
            working_dir="/usr/src/compiled",
            command=[
                "/bin/bash",
                "-c",
                f'gcc -o compiled {file.filename} && cat pyput | ./compiled && rm -f compiled',
            ],
        )


Everything works fine except the time when I need user input.

For example I have this C code:

#include <stdio.h>

int main () {
   char str1[20], str2[30];

   printf("Enter name: ");
   scanf("%19s", str1);

   printf("Enter your website name: ");
   scanf("%29s", str2);

   printf("Entered Name: %s\n", str1);
   
   printf("Entered Website:%s", str2);
   
   
   return(0);
}

And this user_input

Name
Site

Everything works fine if I try out gcc -o compiled code.c && cat pyput | ./compiled in my command line and even when I try doing this operation through regular docker with command

 docker run --rm -v `pwd`:/usr/src/compiled -w /usr/src/compiled -it gcc /bin/bash -c "gcc -o compiled code.c && cat pyput | ./compiled"

Output is weirdly format but at least whole:

user@USER:~/user/playground$ docker run --rm -v `pwd`:/usr/src/compiled -w /usr/src/compiled -it gcc /bin/bash -c "gcc -o compiled code.c && cat pyput | ./compiled"
Enter name: Enter your website name: Entered Name: Name
Entered Website:Siteuser@USER:~/user/playground$

But for some reason I get this output from client.containers.run() command:

b'Enter name: Enter your website name: Entered Name: Name\n'

And what is even weirder that sometimes command gives correct expected output without me changing anything! (I just need to spam-call this method 5-10 times)

Also, I am getting correct output only when I change my C code to contain one more printf("\n")


#include <stdio.h>

int main () {
   char str1[20], str2[30];

   printf("Enter name: ");
   scanf("%19s", str1);

   printf("Enter your website name: ");
   scanf("%29s", str2);

   printf("Entered Name: %s\n", str1);
   
   
   printf("Entered Website:%s", str2);
   
   printf("\n");
   
   return(0);
}

...and then I'm getting correct output from client.containers.run() function:

b'Enter name: Enter your website name: Entered Name: Name\nEntered Website:Site\n'

Could you please help me with this issue?

Thank you for any help,
Best Regards,

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue through client.containers.run() using the shown gcc command, mounted pyput input, and C program. Compare its captured output with the regular docker command, including cases with and without the final newline. Done means the Python client consistently returns the complete output rather than truncating it.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.