Skip to content

Commit

Permalink
Alternative way to check if running in Docker (#1204)
Browse files Browse the repository at this point in the history
* Update setup.py

* Update __init__.py

* 🐛 fix FileNotFoundError in running_in_docker (#1201)

* Remove version constraints

* Changed the way Docker is determined

* Reference to ticket 1140

* Added documentation for ZAPPA_RUNNING_IN_DOCKER flag

* Fixed indentation

* Removed log file

* Added strtobool and re-added MINIMUM_SUPPORTED_MIINOR_VERSION

* updated version

* Added #1201 to CHANGELOG.md, reformatted code

* Reformatted by black due to line length

* Removed unnecessary comment

Co-authored-by: monkut <shane.cousins@gmail.com>
  • Loading branch information
gaborschulz and monkut committed Dec 3, 2022
1 parent ea55be9 commit 73ee393
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Zappa Changelog

## 0.56.1

* Fix FileNotFoundError in running_in_docker (#1201)
* Use ZAPPA_RUNNING_IN_DOCKER environment variable to use any Python version inside Docker container (#1140)

## 0.56.0
* Not recognizing virtaulenv created with pyenv (#1132)
* Remove six from zappa dependencies (#1164)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ requirements:
pipenv lock
pipenv sync --dev

build: clean requirements-install
build: clean requirements
python setup.py sdist
python setup.py bdist_wheel

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@ Update Example:

Refer to [the blog post](https://ianwhitestone.work/zappa-serverless-docker/) for more details about how to leverage this functionality, and when you may want to.

If you are using a custom Docker image for your Lambda runtime (e.g. if you want to use a newer version of Python that is not yet supported by Lambda out of the box) and you would like to bypass the Python version check, you can set an environment variable to do so:

$ export ZAPPA_RUNNING_IN_DOCKER=True

You can also add this to your Dockerfile like this:

```
ENV ZAPPA_RUNNING_IN_DOCKER=True
```

### Rollback

You can also `rollback` the deployed code to a previous version by supplying the number of revisions to return to. For instance, to rollback to the version deployed 3 versions ago:
Expand Down
4 changes: 2 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2604,7 +2604,7 @@ def test_unsupported_version_error(self, *_):

reload(zappa)

@mock.patch("pathlib.Path.read_text", return_value="/docker/")
@mock.patch("os.getenv", return_value="True")
@mock.patch("sys.version_info", new_callable=partial(get_sys_versioninfo, 6))
def test_minor_version_only_check_when_in_docker(self, *_):
from importlib import reload
Expand All @@ -2614,7 +2614,7 @@ def test_minor_version_only_check_when_in_docker(self, *_):

reload(zappa)

@mock.patch("pathlib.Path.read_text", return_value="/docker/")
@mock.patch("os.getenv", return_value="True")
@mock.patch("sys.version_info", new_callable=partial(get_sys_versioninfo, 7))
def test_no_runtimeerror_when_in_docker(self, *_):
from importlib import reload
Expand Down
19 changes: 8 additions & 11 deletions zappa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import os
import sys
from pathlib import Path


def running_in_docker() -> bool:
"""
Determine if zappa is running in docker.
- When docker is used allow usage of any python version
"""
# https://stackoverflow.com/a/20012536/24718
cgroup_content = Path("/proc/1/cgroup").read_text()
in_docker = "/docker/" in cgroup_content or "/lxc/" in cgroup_content
return in_docker
# https://stackoverflow.com/questions/63116419
running_in_docker_flag = os.getenv("ZAPPA_RUNNING_IN_DOCKER", "False").lower() in {"y", "yes", "t", "true", "1"}
return running_in_docker_flag


SUPPORTED_VERSIONS = [(3, 7), (3, 8), (3, 9)]
Expand All @@ -19,10 +18,9 @@ def running_in_docker() -> bool:
if not running_in_docker() and sys.version_info[:2] not in SUPPORTED_VERSIONS:
print(running_in_docker())
formatted_supported_versions = ["{}.{}".format(*version) for version in SUPPORTED_VERSIONS]
err_msg = (
f"This version of Python ({sys.version_info.major}.{sys.version_info.minor}) is not supported!\n"
f"Zappa (and AWS Lambda) support the following versions of Python: {formatted_supported_versions}"
)
err_msg = "This version of Python ({}.{}) is not supported!\n".format(
*sys.version_info
) + "Zappa (and AWS Lambda) support the following versions of Python: {}".format(formatted_supported_versions)
raise RuntimeError(err_msg)
elif running_in_docker() and sys.version_info.minor < MINIMUM_SUPPORTED_MINOR_VERSION:
# when running in docker enforce minimum version only
Expand All @@ -32,5 +30,4 @@ def running_in_docker() -> bool:
)
raise RuntimeError(err_msg)


__version__ = "0.56.0"
__version__ = "0.56.1"

0 comments on commit 73ee393

Please sign in to comment.