-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Support working with env files (#737)
Fix: #687 Users should not be required to load each env var manually if they have an env file. Added support for loading a dot-env file. Usage: ```python with DockerContainer("nginx:latest").with_env_file("my_env_file"): # We now have a container with the relevant env vars from the file ``` This is an implementation of ```docker run --env-file <file> ...```
- Loading branch information
1 parent
f1d8d35
commit 932ee30
Showing
4 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import tempfile | ||
from pathlib import Path | ||
|
||
from testcontainers.core.container import DockerContainer | ||
|
||
|
||
|
@@ -17,3 +20,29 @@ def test_get_logs(): | |
assert isinstance(stdout, bytes) | ||
assert isinstance(stderr, bytes) | ||
assert "Hello from Docker".encode() in stdout, "There should be something on stdout" | ||
|
||
|
||
def test_docker_container_with_env_file(): | ||
"""Test that environment variables can be loaded from a file""" | ||
with tempfile.TemporaryDirectory() as temp_directory: | ||
env_file_path = Path(temp_directory) / "env_file" | ||
with open(env_file_path, "w") as f: | ||
f.write( | ||
""" | ||
TEST_ENV_VAR=hello | ||
NUMBER=123 | ||
DOMAIN=example.org | ||
ADMIN_EMAIL=admin@${DOMAIN} | ||
ROOT_URL=${DOMAIN}/app | ||
""" | ||
) | ||
container = DockerContainer("alpine").with_command("tail -f /dev/null") # Keep the container running | ||
container.with_env_file(env_file_path) # Load the environment variables from the file | ||
with container: | ||
output = container.exec("env").output.decode("utf-8").strip() | ||
assert "TEST_ENV_VAR=hello" in output | ||
assert "NUMBER=123" in output | ||
assert "DOMAIN=example.org" in output | ||
assert "[email protected]" in output | ||
assert "ROOT_URL=example.org/app" in output | ||
print(output) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters