pythondockerdevopsselenium-gridselenium-docker

Failed to create multiple containers using docker python SDk


I am using python docker SDK to create multiple chrome containers. Below is my script Here first I have pull docker image then try to create 2 containers out it. But it is failing with an error message that ports are already in use but I am incrementing the ports value as per container count.

import docker, sys

class CreateContainer:
    def __init__(self):
        self.client  = CreateContainer.create_client()

    @staticmethod
    def create_client():
        client = docker.from_env()
        return client

    def pull_image(self, image_name):
        image = client.images.pull(image_name)
        print(image.name)

    def create_containers(self, image, container_name, expose_port, countainer_count=1):
    
        container = self.client.containers.run(
            image, 
            name=container_name, 
            hostname=container_name, 
            ports=expose_port, 
            detach=True
        )   
        
        for line in container.logs():
            print(line)
        return container


if __name__ == '__main__':
    threads = int(sys.argv[1])
    c_obj = CreateContainer()
    for i in range(1, threads+1):
        c_obj.create_containers("selenium/standalone-chrome", "Chrome_{0}".format(i), expose_port={5550+i:4444})

------Run----------

python test.py 2

------error-----

Traceback (most recent call last):
  File "C:\Program Files\Python39\lib\site-packages\docker\api\client.py", line 268, in _raise_for_status
    response.raise_for_status()
  File "C:\Program Files\Python39\lib\site-packages\requests\models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: http+docker://localnpipe/v1.40/containers/260894dbaec6946e5f31fdbfb5307182d2f621c12a38f328f6efac58df58854d/start

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Desktop\FreeLance\Utility\src\docker_engine\create_container.py", line 53, in <module>
    c_obj.create_containers("selenium/standalone-chrome", "Chrome_{0}".format(i), expose_port={5550+i:4444})
  File "C:\Users\Desktop\FreeLance\Utility\src\docker_engine\create_container.py", line 20, in create_containers
    container = self.client.containers.run(
  File "C:\Program Files\Python39\lib\site-packages\docker\models\containers.py", line 818, in run
    container.start()
  File "C:\Program Files\Python39\lib\site-packages\docker\models\containers.py", line 404, in start
    return self.client.api.start(self.id, **kwargs)
  File "C:\Program Files\Python39\lib\site-packages\docker\utils\decorators.py", line 19, in wrapped
    return f(self, resource_id, *args, **kwargs)
  File "C:\Program Files\Python39\lib\site-packages\docker\api\container.py", line 1111, in start
    self._raise_for_status(res)
  File "C:\Program Files\Python39\lib\site-packages\docker\api\client.py", line 270, in _raise_for_status
    raise create_api_error_from_http_exception(e)
  File "C:\Program Files\Python39\lib\site-packages\docker\errors.py", line 31, in create_api_error_from_http_exception
    raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 500 Server Error for http+docker://localnpipe/v1.40/containers/260894dbaec6946e5f31fdbfb5307182d2f621c12a38f328f6efac58df58854d/start: Internal Server Error ("driver failed programming external connectivity on endpoint Chrome_2 (c0c77528743a1e3153201565b2cb520243b66adbb903bb69e91a00c4399aca62): Bind for 0.0.0.0:4444 failed: port is already allocated")

Solution

  • While proving port the syntax is container_port:host_port

    c_obj.create_containers("selenium/standalone-chrome", "Chrome_{0}".format(i), expose_port={4444:5550+i})
    

    documentation : https://docker-py.readthedocs.io/en/stable/containers.html