0
votes

My attempt to automatically launch (using python script) an instance of docker container instance of selenium/standalone-chrome failed.

With SSH command line I can launch selenium/standalone-chrome with the right configuration: docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome

However, how do I pass the port settings (4444) and volume settings (/dev/shm:/dev/shm) via python script?

Here is my python script:

import docker
def toggle_selenium():

    client = docker.from_env()
    
    #Check if there are Selenium containers
    if client.containers.list(filters={'ancestor':'selenium/standalone-chrome'})==[]:
        # Then run a selenium container
        container = client.containers.run('selenium/standalone-chrome',detach=True, auto_remove=True)
        if client.containers.get(container.id).status == 'running':
            print('{:<47s} : {}'.format('Container '+ container.name ,'Running'))
            return()
    else:
        # Stop all running Selenium containers
        for i in client.containers.list(filters={'ancestor':'selenium/standalone-chrome'}):
            i.stop()
            print('{:<47s} : {}'.format('Container '+ i.name ,'Stopped'))
        client.containers.prune()
        print('{:<47s} : {}'.format('Container status','All stopped'))
        return()
        
if __name__ == '__main__':
    toggle_selenium()
1

1 Answers

0
votes

I managed to resolve it by referring to Docker SDK documentation.

        container = client.containers.run('selenium/standalone-chrome',
                                          detach=True,
                                          auto_remove=True,
                                          ports={'4444/tcp':('127.0.0.1',4444)},
                                          volumes = {'/dev/shm':{'bind':'/dev/shm','mode':'rw'}})