Docker Compose Support¶
Allows to spin up services configured via docker-compose.yml
.
- class testcontainers.compose.DockerCompose(filepath, compose_file_name='docker-compose.yml', pull=False, build=False, env_file=None)¶
Manage docker compose environments.
- Parameters:
filepath (str) – The relative directory containing the docker compose configuration file
compose_file_name (str) – The file name of the docker compose configuration file
pull (bool) – Attempts to pull images before launching environment
build (bool) – Whether to build images referenced in the configuration file
env_file (str) – Path to an env file containing environment variables to pass to docker compose
Example
with DockerCompose("/home/project", compose_file_name=["docker-compose-1.yml", "docker-compose-2.yml"], pull=True) as compose: host = compose.get_service_host("hub", 4444) port = compose.get_service_port("hub", 4444) driver = webdriver.Remote( command_executor=("http://{}:{}/wd/hub".format(host,port)), desired_capabilities=CHROME, ) driver.get("http://automation-remarks.com") stdout, stderr = compose.get_logs() if stderr: print("Errors\n:{}".format(stderr))
hub: image: selenium/hub ports: - "4444:4444" firefox: image: selenium/node-firefox links: - hub expose: - "5555" chrome: image: selenium/node-chrome links: - hub expose: - "5555"
- docker_compose_command()¶
Returns command parts used for the docker compose commands
- Returns:
The docker compose command parts
- Return type:
list[str]
- exec_in_container(service_name, command)¶
Executes a command in the container of one of the services.
- Parameters:
service_name (str) – Name of the docker compose service to run the command in
command (list[str]) – The command to execute
- Returns:
stdout, stderr, return code
- Return type:
tuple[str, str, int]
- get_logs()¶
Returns all log output from stdout and stderr
- Returns:
stdout, stderr
- Return type:
tuple[bytes, bytes]
- get_service_host(service_name, port)¶
Returns the host for one of the services.
- Parameters:
service_name (str) – Name of the docker compose service
port (int) – The internal port to get the host for
- Returns:
The hostname for the service
- Return type:
str
- get_service_port(service_name, port)¶
Returns the mapped port for one of the services.
- Parameters:
service_name (str) – Name of the docker compose service
port (int) – The internal port to get the mapping for
- Returns:
The mapped port on the host
- Return type:
str
- start()¶
Starts the docker compose environment.
- stop()¶
Stops the docker compose environment.
- wait_for(url)¶
Waits for a response from a given URL. This is typically used to block until a service in the environment has started and is responding. Note that it does not assert any sort of return code, only check that the connection was successful.
- Parameters:
url (str) – URL from one of the services in the environment to use to wait on