# DockerPythonEnvironment

## `autogen.environments.docker_python_environment.DockerPythonEnvironment`

```python
DockerPythonEnvironment(image='python:3.11-slim', container_name_prefix='ag2_docker_env_', volumes=None, environment=None, network=None, pip_packages=None, requirements_file=None, dockerfile=None, build_args=None, cleanup_container=True, keep_container_running=False, container_startup_timeout=30)
```

Bases: `PythonEnvironment`

A Python environment using Docker containers for isolated execution.

Initialize a Docker Python environment.

| PARAMETER                  | DESCRIPTION |
| -------------------------- | ----------- |
| `image`                   | Docker image to use (ignored if dockerfile is provided)  <br>**TYPE:**`str`**DEFAULT:**`'python:3.11-slim'` |
| `container_name_prefix`   | Prefix for container names<br>**TYPE:**`str`**DEFAULT:**`'ag2_docker_env_'` |
| `volumes`                 | Dictionary mapping host paths to container paths for mounting<br>**TYPE:**`dict[str, str] | None`**DEFAULT:**`None` |
| `environment`             | Dictionary of environment variables to set in the container<br>**TYPE:**`dict[str, str] | None`**DEFAULT:**`None` |
| `network`                 | Docker network to attach the container to<br>**TYPE:**`str | None`**DEFAULT:**`None` |
| `pip_packages`            | List of pip packages to install in the container<br>**TYPE:**`list[str] | None`**DEFAULT:**`None` |
| `requirements_file`       | Path to requirements.txt file to install in the container<br>**TYPE:**`str | None`**DEFAULT:**`None` |
| `dockerfile`              | Optional path to a Dockerfile to build and use instead of pulling an image<br>**TYPE:**`str | None`**DEFAULT:**`None` |
| `build_args`              | Optional build arguments for the Dockerfile<br>**TYPE:**`dict[str, str] | None`**DEFAULT:**`None` |
| `cleanup_container`       | Whether to remove the container after use<br>**TYPE:**`bool`**DEFAULT:**`True` |
| `keep_container_running`   | Whether to keep the container running after execution<br>**TYPE:**`bool`**DEFAULT:**`False` |
| `container_startup_timeout`| Timeout in seconds for container startup<br>**TYPE:**`int`**DEFAULT:**`30` |

Source code in `autogen/environments/docker_python_environment.py`

|     |     |
| --- | --- |
| ``` | ```
| def __init__( | def __init__(  
| self, | self,  
| image: str = "python:3.11-slim", | image: str = "python:3.11-slim",  
| container_name_prefix: str = "ag2_docker_env_", | container_name_prefix: str = "ag2_docker_env_",  
| volumes: dict[str, str] | None = None, | volumes: dict[str, str] | None = None,  
| environment: dict[str, str] | None = None, | environment: dict[str, str] | None = None,  
| network: str | None = None, | network: str | None = None,  
| pip_packages: list[str] | None = None, | pip_packages: list[str] | None = None,  
| requirements_file: str | None = None, | requirements_file: str | None = None,  
| dockerfile: str | None = None, | dockerfile: str | None = None,  
| build_args: dict[str, str] | None = None, | build_args: dict[str, str] | None = None,  
| cleanup_container: bool = True, | cleanup_container: bool = True,  
| keep_container_running: bool = False, | keep_container_running: bool = False,  
| container_startup_timeout: int = 30, | container_startup_timeout: int = 30,  
| ): | ):  
| """Initialize a Docker Python environment. | """Initialize a Docker Python environment.  
| Args: | Args:  
| image: Docker image to use (ignored if dockerfile is provided) | image: Docker image to use (ignored if dockerfile is provided)  
| container_name_prefix: Prefix for container names | container_name_prefix: Prefix for container names  
| volumes: Dictionary mapping host paths to container paths for mounting | volumes: Dictionary mapping host paths to container paths for mounting  
| environment: Dictionary of environment variables to set in the container | environment: Dictionary of environment variables to set in the container  
| network: Docker network to attach the container to | network: Docker network to attach the container to  
| pip_packages: List of pip packages to install in the container | pip_packages: List of pip packages to install in the container  
| requirements_file: Path to requirements.txt file to install in the container | requirements_file: Path to requirements.txt file to install in the container  
| dockerfile: Optional path to a Dockerfile to build and use instead of pulling an image | dockerfile: Optional path to a Dockerfile to build and use instead of pulling an image  
| build_args: Optional build arguments for the Dockerfile | build_args: Optional build arguments for the Dockerfile  
| cleanup_container: Whether to remove the container after use | cleanup_container: Whether to remove the container after use  
| keep_container_running: Whether to keep the container running after execution | keep_container_running: Whether to keep the container running after execution  
| container_startup_timeout: Timeout in seconds for container startup | container_startup_timeout: Timeout in seconds for container startup  
| """ | """  
| self.image = image | self.image = image  
| self.container_name_prefix = container_name_prefix | self.container_name_prefix = container_name_prefix  
| self.volumes = volumes or {} | self.volumes = volumes or {}  
| self.environment = environment or {} | self.environment = environment or {}  
| self.network = network | self.network = network  
| self.pip_packages = pip_packages or [] | self.pip_packages = pip_packages or []  
| self.requirements_file = requirements_file | self.requirements_file = requirements_file  
| self.dockerfile = dockerfile | self.dockerfile = dockerfile  
| self.build_args = build_args or {} | self.build_args = build_args or {}  
| self.cleanup_container = cleanup_container | self.cleanup_container = cleanup_container  
| self.keep_container_running = keep_container_running | self.keep_container_running = keep_container_running  
| self.container_startup_timeout = container_startup_timeout | self.container_startup_timeout = container_startup_timeout  
| # Internal state | # Internal state  
| self._container_id = None | self._container_id = None  
| self._container_name = None | self._container_name = None  
| self._custom_image_name = None | self._custom_image_name = None  
| self._temp_dir = None | self._temp_dir = None  
| super().__init__() | super().__init__()  
| ``` | ```

### `image` instance-attribute
```python
image = image
```

### `container_name_prefix` instance-attribute
```python
container_name_prefix = container_name_prefix
```

### `volumes` instance-attribute
```python
volumes = volumes or {}
```

### `environment` instance-attribute
```python
environment = environment or {}
```

### `network` instance-attribute
```python
network = network
```

### `pip_packages` instance-attribute
```python
pip_packages = pip_packages or []
```

### `requirements_file` instance-attribute
```python
requirements_file = requirements_file
```

### `dockerfile` instance-attribute
```python
dockerfile = dockerfile
```

### `build_args` instance-attribute
```python
build_args = build_args or {}
```

### `cleanup_container` instance-attribute
```python
cleanup_container = cleanup_container
```

### `keep_container_running` instance-attribute
```python
keep_container_running = keep_container_running
```

### `container_startup_timeout` instance-attribute
```python
container_startup_timeout = container_startup_timeout
```

### `get_executable`
```python
get_executable()
```

Get the path to the Python executable in the Docker container.

### `execute_code` async
```python
execute_code(code, script_path, timeout=30)
```

Execute code in the Docker container.

### `get_current_python_environment` classmethod
```python
get_current_python_environment(python_environment=None)
```

Get the current Python environment or the specified one if provided.

| PARAMETER                  | DESCRIPTION |
| -------------------------- | ----------- |
| `python_environment`       | Optional environment to return if specified. <br>**TYPE:**`Optional[PythonEnvironment]`**DEFAULT:**`None` |

| RETURNS                   | DESCRIPTION |
| -------------------------- | ----------- |
| `Optional[PythonEnvironment]` | The current Python environment or None if none is active. |
