【问题标题】:Trying to launch docker container for selenium/standalone-chrome with port and volume configurations尝试使用端口和卷配置为 selenium/standalone-chrome 启动 docker 容器
【发布时间】:2021-06-06 23:27:50
【问题描述】:

我尝试自动启动(使用 python 脚本)selenium/standalone-chrome 的 docker 容器实例失败。

使用 SSH 命令行,我可以使用正确的配置启动 selenium/standalone-chrome: docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome

但是,如何通过 python 脚本传递端口设置(4444)和音量设置(/dev/shm:/dev/shm)?

这是我的 python 脚本:

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()

【问题讨论】:

    标签: python python-3.x docker selenium


    【解决方案1】:

    我通过参考 Docker SDK 文档设法解决了这个问题。

            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'}})
    
    

    【讨论】:

      猜你喜欢
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      相关资源
      最近更新 更多