Search code examples
dockerdockerpy

Issue with docker-py in executing /bin/bash


I have a docker image built from ubuntu base image with few softwares installed.

i have a startup script, as below

#!/bin/bash
/usr/local/sbin/process1 -d
/usr/local/sbin/process2 -d 
/bin/bash

Now I use docker-py python library to start multiple of these containers from a python file.

c = docker.Client(base_url='unix://var/run/docker.sock',
              version='1.12',
              timeout=10)

container = c.create_container("p12", command="/startup.sh", hostname=None, user=None,
               detach=False, stdin_open=False, tty=False, mem_limit=0,
               ports=None, environment=None, dns=None, volumes=None,
               volumes_from=None, network_disabled=False, name=None,
               entrypoint=None, cpu_shares=None, working_dir=None,
               memswap_limit=0)

c.start(container, binds=None, port_bindings=None, lxc_conf=None,
    publish_all_ports=False, links=None, privileged=False,
    dns=None, dns_search=None, volumes_from=None, network_mode=None,
    restart_policy=None, cap_add=None, cap_drop=None)

This worked fine and I can start multiple (say 3) when I tested this on a Ubuntu Desktop, Ubuntu 14.04.1 LTS and with docker-py version of 1.10. It will start the dockers and I can do a docker attach later and work on the terminal.

Now i moved my testing environment to a Ubuntu Server edition with Ubuntu 14.04.1 LTS and with docker-py version of 1.12.

The issue i see is that, when I use the same script and try to start 3 dockers, after starting process1 and process 2 as background processes, all the dockers simply exit. It appears as if /bin/bash doesnt execute at all.

If i execute the same docker image as "docker run -t -i p14 /startup.sh --> then everything is fine again. The docker is started appropriately and i get the terminal access.

The only issue is when i execute this python library.

anybody has any similar issues...any idea on how to debug this problem...or any pointers for the fix ?

Thanks, Kiran


Solution

  • The difference is you're in tty (-t) mode with an open stdin (-i) when you run your docker image with docker run -t -i p14 /startup.sh, whereas you set both stdin_open=False and tty=False in your docker-py configuration.

    Because your docker container has no tty and can't take any input from stdin, your call to /bin/bash has nothing to do so exits with code 0.

    Try it yourself:

    An open stdin with a tty

    $ docker run -t -i ubuntu:14.04 /bin/bash                                  
    root@1e7eda2bba03:/# ls -la                                                
    total 7184                                                                 
    drwxr-xr-x  21 root root    4096 Sep 19 21:30 .                            
    drwxr-xr-x  21 root root    4096 Sep 19 21:30 ..                           
    -rwxr-xr-x   1 root root       0 Sep 19 21:30 .dockerenv                   
    -rwx------   1 root root 7279686 Jul 21 10:50 .dockerinit                  
    drwxr-xr-x   2 root root    4096 Sep  3 03:33 bin                          
    drwxr-xr-x   2 root root    4096 Apr 10 22:12 boot                         
    drwxr-xr-x   4 root root     360 Sep 19 21:30 dev                          
    drwxr-xr-x  61 root root    4096 Sep 19 21:30 etc                          
    drwxr-xr-x   2 root root    4096 Apr 10 22:12 home                         
    drwxr-xr-x  12 root root    4096 Sep  3 03:33 lib                          
    drwxr-xr-x   2 root root    4096 Sep  3 03:33 lib64                        
    drwxr-xr-x   2 root root    4096 Sep  3 03:33 media                        
    drwxr-xr-x   2 root root    4096 Apr 10 22:12 mnt                          
    drwxr-xr-x   2 root root    4096 Sep  3 03:33 opt                          
    dr-xr-xr-x 240 root root       0 Sep 19 21:30 proc                         
    drwx------   2 root root    4096 Sep  3 03:33 root                         
    drwxr-xr-x   7 root root    4096 Sep  3 03:33 run                          
    drwxr-xr-x   2 root root    4096 Sep  4 18:41 sbin                         
    drwxr-xr-x   2 root root    4096 Sep  3 03:33 srv                          
    dr-xr-xr-x  13 root root       0 Sep 19 18:44 sys                          
    drwxrwxrwt   2 root root    4096 Sep  4 18:41 tmp                          
    drwxr-xr-x  10 root root    4096 Sep  3 03:33 usr                          
    drwxr-xr-x  11 root root    4096 Sep  3 03:33 var                          
    root@1e7eda2bba03:/#  
    

    An open stdin with no tty (i.e., no prompt, but you can still send commands via stdin)

    $ docker run -i ubuntu:14.04 /bin/bash                                     
    ls -la                                                                     
    total 7184                                                                 
    drwxr-xr-x  21 root root    4096 Sep 19 21:32 .                            
    drwxr-xr-x  21 root root    4096 Sep 19 21:32 ..                           
    -rwxr-xr-x   1 root root       0 Sep 19 21:32 .dockerenv                   
    -rwx------   1 root root 7279686 Jul 21 10:50 .dockerinit                  
    drwxr-xr-x   2 root root    4096 Sep  3 03:33 bin                          
    drwxr-xr-x   2 root root    4096 Apr 10 22:12 boot                         
    drwxr-xr-x   4 root root     340 Sep 19 21:32 dev                          
    drwxr-xr-x  61 root root    4096 Sep 19 21:32 etc                          
    drwxr-xr-x   2 root root    4096 Apr 10 22:12 home                         
    drwxr-xr-x  12 root root    4096 Sep  3 03:33 lib                          
    drwxr-xr-x   2 root root    4096 Sep  3 03:33 lib64                        
    drwxr-xr-x   2 root root    4096 Sep  3 03:33 media                        
    drwxr-xr-x   2 root root    4096 Apr 10 22:12 mnt                          
    drwxr-xr-x   2 root root    4096 Sep  3 03:33 opt                          
    dr-xr-xr-x 243 root root       0 Sep 19 21:32 proc                         
    drwx------   2 root root    4096 Sep  3 03:33 root                         
    drwxr-xr-x   7 root root    4096 Sep  3 03:33 run                          
    drwxr-xr-x   2 root root    4096 Sep  4 18:41 sbin                         
    drwxr-xr-x   2 root root    4096 Sep  3 03:33 srv                          
    dr-xr-xr-x  13 root root       0 Sep 19 18:44 sys                          
    drwxrwxrwt   2 root root    4096 Sep  4 18:41 tmp                          
    drwxr-xr-x  10 root root    4096 Sep  3 03:33 usr                          
    drwxr-xr-x  11 root root    4096 Sep  3 03:33 var   
    

    A closed stdin with a tty (you can see the prompt but you can't enter any commands)

    $ docker run -t ubuntu:14.04 /bin/bash
    root@95904c21e5a5:/# ls -la
    hello
    this does nothing :(
    

    A closed stdin with no tty - /bin/bash has nothing to do

    $ docker run ubuntu:14.04 /bin/bash
    $