Skip to content

Commit f9304d6

Browse files
committedMar 6, 2022
feat: allow passing CLI args to Python app
1 parent deae581 commit f9304d6

File tree

6 files changed

+33
-39
lines changed

6 files changed

+33
-39
lines changed
 

‎Dockerfile

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ USER ${USERNAME}
1818
WORKDIR /home/${USERNAME}
1919

2020
# Copy scripts
21-
COPY scripts/* ./scripts/
21+
COPY --chown=${USERNAME}:${USERNAME} scripts/* ./scripts/
22+
RUN chmod +x ./scripts/entrypoint.sh
2223

2324
# Run entrypoint as default command
24-
CMD ["sh", "scripts/entrypoint.sh"]
25+
ENTRYPOINT ["./scripts/entrypoint.sh"]

‎README.md

+7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ docker run -it --rm -e GIT_REPOSITORY="https://github.com/David-Lor/Python-Hello
5050
Only required variable is (ENV) `GIT_REPOSITORY`.
5151
The variables marked with (ARG) are [build-args](https://docs.docker.com/engine/reference/commandline/build/#set-build-time-variables---build-arg), used on image build.
5252

53+
If your Python script/app is CLI-based and requires to be called with arguments (for example `python . worker --instances=10`), you can provide them as Docker command arguments, like following:
54+
55+
```bash
56+
docker run -it --rm -e GIT_REPOSITORY="https://github.com/David-Lor/Python-HelloWorld.git" -e GIT_BRANCH="args" davidlor/python-git-app worker --instances=10
57+
# This command will clone an existing branch on the example repository, and print out the custom commands provided
58+
```
59+
5360
## Available tags
5461

5562
- The tags available for the image are a limited selection of tags used in base images, such as the [official Python images](https://hub.docker.com/_/python/).

‎scripts/entrypoint.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
set -ex
44

55
python -u ~/scripts/setup_app.py
6-
python -u ${APP_NAME}
6+
python -u "$APP_NAME" "$@"

‎scripts/setup_app.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ def __init__(self):
2525
self.app_dir = self.join_home(self.app_name)
2626
self.requirements_file = self.join_app(self.REQUIREMENTS_FILENAME)
2727
self.git_branch = os.getenv("GIT_BRANCH")
28-
28+
2929
def join_home(self, path):
3030
return os.path.join(self.base_dir, path)
31-
31+
3232
def join_app(self, path):
3333
return os.path.join(self.app_dir, path)
3434

‎tools/tests/test_build.py

+9-24
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,44 @@
33

44

55
class TestBuild(BaseTest):
6-
"""Test building the image
7-
"""
6+
"""Test building the image"""
87

98
def test_build_default(self):
10-
"""Test building the image with default parameters, then running the container, thus running the app
11-
"""
9+
"""Test building the image with default parameters, then running the container, thus running the app"""
1210
image, output = self.build_image()
1311
assert image in output
1412

1513
output = self.run_container(image=image)
1614
assert OUTPUT_SUCCESS in output
1715

1816
def test_build_username(self):
19-
"""Test building the image changing the username
20-
"""
17+
"""Test building the image changing the username"""
2118
image, output = self.build_image(user=USERNAME)
2219
assert image in output
2320

24-
output = self.run_container(image=image, final_args=["pwd"])
21+
output = self.run_container(image=image, args=["--entrypoint", "pwd"])
2522
assert "/home/{}".format(USERNAME) in output
2623

2724
def test_build_from_alpine(self):
28-
"""Test building the image using the 'python:alpine' base image
29-
"""
25+
"""Test building the image using the 'python:alpine' base image"""
3026
image, output = self.build_image(from_image="python:alpine")
3127
assert image in output
3228

3329
output = self.run_container(image=image, args=["--entrypoint", "which"], final_args=["apk"])
3430
assert "/sbin/apk" in output
3531

36-
def test_build_from_python27(self):
37-
"""Test building the image using the 'python:2.7-alpine' base image
38-
"""
39-
image, output = self.build_image(from_image="python:2.7-alpine")
40-
assert image in output
41-
42-
output = self.run_container(image=image, final_args=["python", "-V"])
43-
assert output.startswith("Python 2.7")
44-
4532
def test_build_from_pyston(self):
46-
"""Test building the image using the 'pyston/pyston' base image
47-
"""
33+
"""Test building the image using the 'pyston/pyston' base image"""
4834
image, output = self.build_image(from_image="pyston/pyston")
4935
assert image in output
5036

51-
output = self.run_container(image=image, final_args=["python", "-V"])
37+
output = self.run_container(image=image, args=["--entrypoint", "python"], final_args=["-V"])
5238
assert "[Pyston " in output
5339

5440
def test_build_from_pypy(self):
55-
"""Test building the image using the 'pypy' base image
56-
"""
41+
"""Test building the image using the 'pypy' base image"""
5742
image, output = self.build_image(from_image="pypy")
5843
assert image in output
5944

60-
output = self.run_container(image=image, final_args=["python", "-V"])
45+
output = self.run_container(image=image, args=["--entrypoint", "python"], final_args=["-V"])
6146
assert "[PyPy " in output

‎tools/tests/test_clone.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,32 @@
33

44

55
class TestClone(BaseTest):
6-
"""Test the repository cloning, thus running the app
7-
"""
6+
"""Test the repository cloning, thus running the app"""
87

98
def test_clone_existing_repository(self):
10-
"""Test cloning from an existing repository
11-
"""
9+
"""Test cloning from an existing repository"""
1210
output = self.run_container()
1311
assert OUTPUT_SUCCESS in output
1412

1513
def test_clone_unexisting_repository(self):
16-
"""Test cloning from a repository that does not exist
17-
"""
14+
"""Test cloning from a repository that does not exist"""
1815
output = self.run_container(repository=UNEXISTING_REPOSITORY)
1916
assert GIT_CLONE_FAILED_ERROR in output
2017

2118
def test_clone_existing_branch_fail_pip_install(self):
2219
"""Test cloning from an existing repository, an existing branch
23-
(the requirements-fail branch, that will fail pip install)
24-
"""
20+
(the requirements-fail branch, that will fail pip install)"""
2521
# TODO Create another branch on Python-HelloWorld repository that won't fail but output something different
2622
output = self.run_container(branch=REQUIREMENTS_FAIL_BRANCH)
2723
assert PIP_INSTALL_FAILED_ERROR in output
2824

2925
def test_clone_unexisting_branch(self):
30-
"""Test cloning from an existing repository, a branch that does not exist
31-
"""
26+
"""Test cloning from an existing repository, a branch that does not exist"""
3227
output = self.run_container(branch="non-existing-branch")
3328
assert GIT_CLONE_FAILED_ERROR in output
29+
30+
def test_clone_run_args(self):
31+
"""Test cloning from an existing repository, using custom args when running the Python app"""
32+
args = ["--foo", "bar", "-baz", "0"]
33+
output = self.run_container(branch="args", final_args=args)
34+
assert f"Called with args: {str(args)}" in output

0 commit comments

Comments
 (0)
Please sign in to comment.