3

I am using docker-py (create_container function) to create multiple Docker containers, my code is working properly and creating containers for me and after some time the container exits automatically, but the thing i want to implement here is that, i want all my container to be in running mode for the desired time, for example i want all the containers to be in running mode for 5 minutes or 10 minutes etc etc.. i have added "sleep" in command parameter but it is not working for me, please help ! i am not much experienced in python.. the function i implement is;

for i in range(0,5):
    container = client.create_container(
                    image='syed/syedclients:helloworld',
                    stdin_open=True,
                    tty=True,
                    command='/bin/bash saad.sh /bin/sleep 180',
                    name=hello-1,                    
                    volumes=volumes,
                    host_config=host_config,
                    environment=['VARIABLE=xyz123'],
                    detach=True,
    )
    client.start(container)
Saad
  • 916
  • 1
  • 15
  • 28

1 Answers1

2

This isn't to do with python, but it is more to do with Docker. The container just does what you have specified, which is: run the script and exit. Therefore, I would suggest that you add the sleep command to the 'saad.sh' script

Sergiu
  • 2,928
  • 3
  • 27
  • 37
  • yes , it is a good suggestion, but can't i pass it in "create_container" function in command parameter ? Is there any way to do it via create_container() function ? – Saad Sep 19 '17 at 13:20
  • @SyedSaadAhmed could you please try the following: command='bash -c "saad.sh && /bin/sleep 180"') – Sergiu Sep 19 '17 at 13:27
  • Thanks @Sergiu , first approach work ! You were right, **"This isn't to do with python, but it is more to do with Docker. The container just does what you have specified, which is: run the script and exit. Therefore, I would suggest that you add the sleep command to the 'saad.sh' script"** It Worked for me ! – Saad Sep 20 '17 at 08:35