-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
37 additions
and
45 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
FROM python:3.9-slim as build | ||
RUN apt-get update && apt-get install build-essential patchelf locales curl -y | ||
RUN curl -sSL https://install.python-poetry.org | python - | ||
RUN apt-get update && apt-get install build-essential patchelf locales scons curl -y | ||
WORKDIR /build | ||
COPY . /build | ||
ENV PATH="/root/.local/bin:$PATH" | ||
RUN poetry export -f requirements.txt -o requirements.txt | ||
|
||
# No difference at this stage | ||
FROM build as build-env | ||
RUN pip install -r requirements.txt | ||
RUN pip install pyinstaller staticx | ||
RUN pyinstaller -F awesomeapi/entrypoint.py --name awesomeapi --clean | ||
RUN pip install . | ||
RUN pip install pyinstaller | ||
RUN pip install staticx | ||
RUN pyinstaller -F awesomeapi/entrypoint.py --name awesomeapi --clean --strip | ||
RUN staticx dist/awesomeapi app --strip | ||
RUN mkdir tmp | ||
|
||
FROM scratch | ||
COPY --from=build-env /build/app /awesomeapi | ||
COPY --from=build-env /tmp /tmp | ||
# COPY --from=build-env /usr/lib/locale /usr/lib/locale | ||
# pyinstaller needs a /tmp to exist | ||
COPY --from=build-env /build/tmp /tmp | ||
ENV LANG=C.UTF-8 | ||
ENV LC_ALL=C.UTF-8 | ||
EXPOSE 8080 | ||
ENTRYPOINT ["./awesomeapi"] | ||
ENTRYPOINT ["./awesomeapi"] |
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,3 @@ | ||
# AwesomeAPI | ||
|
||
An awesome CLI built with [FastAPI](https://fastapi.tiangolo.com/) | ||
An awesome API built with [FastAPI](https://fastapi.tiangolo.com/) |
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,27 +1,22 @@ | ||
FROM python:3.9-slim as build | ||
# This time we install locales package as our CLI depends on Click that needs locales to check for UTF-8 support | ||
RUN apt-get update && apt-get install build-essential patchelf locales curl -y | ||
RUN curl -sSL https://install.python-poetry.org | python - | ||
RUN apt-get update && apt-get install build-essential patchelf locales scons curl -y | ||
WORKDIR /build | ||
COPY . /build | ||
ENV PATH="/root/.local/bin:$PATH" | ||
RUN poetry export -f requirements.txt -o requirements.txt | ||
|
||
# No difference at this stage | ||
FROM build as build-env | ||
RUN pip install -r requirements.txt | ||
RUN pip install pyinstaller staticx | ||
RUN pyinstaller -F awesomecli/entrypoint.py --name awesomecli --clean | ||
RUN pip install . | ||
RUN pip install pyinstaller | ||
RUN pip install staticx | ||
RUN pyinstaller -F awesomecli/entrypoint.py --name awesomecli --clean --strip | ||
RUN staticx dist/awesomecli app --strip | ||
RUN mkdir tmp | ||
|
||
FROM scratch | ||
COPY --from=build-env /build/app /awesomecli | ||
COPY --from=build-env /tmp /tmp | ||
# This time we copy few linux dependencies that Python dependency will need. | ||
# In our context it is `click` which is used by Typer in the backend | ||
# This is a hacky approach and not recommneded. But until I or someone figures out, we'll keep it this way. | ||
COPY --from=build-env /usr/lib/locale /usr/lib/locale | ||
# We also need to set few environment variables so Click can verify that locale is set for LC_ALL and LANG | ||
# pyinstaller needs a /tmp to exist | ||
COPY --from=build-env /build/tmp /tmp | ||
ENV LANG=C.UTF-8 | ||
ENV LC_ALL=C.UTF-8 | ||
ENTRYPOINT ["./awesomecli"] | ||
ENTRYPOINT ["./awesomecli"] |
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,21 +1,24 @@ | ||
# First we use a build environment to perform all our necessary actions | ||
FROM python:3.9-slim as build-env | ||
# To build the binaries we will need some packages | ||
RUN apt-get update && apt-get install build-essential patchelf -y | ||
RUN apt-get update && apt-get install build-essential patchelf locales scons curl -y | ||
# Although we don't need a work dir, we create one to keep our build environment clean so copying becomes easy later | ||
WORKDIR /build | ||
COPY . /build | ||
RUN pip install staticx pyinstaller | ||
RUN pip install pyinstaller | ||
RUN pip install staticx | ||
# Since there are no requirements for this project, we can just build the binaries | ||
RUN pyinstaller -F helloworld/main.py -n binary --clean | ||
RUN pyinstaller -F helloworld/main.py -n binary --clean --strip | ||
RUN staticx dist/binary dist/app --strip | ||
RUN mkdir tmp | ||
|
||
# Actual image instructions starts from here. | ||
FROM scratch | ||
# We copy the necessary files from the previous context to the actual image | ||
# We copy the necessary files from the previous stage to the actual image | ||
COPY --from=build-env /build/dist/app /app | ||
# Scratch image will not have the ability to create folders. Hence addin /tmp folder This is a workaround. | ||
COPY --from=build-env /tmp /tmp | ||
# Executables built with pyinstaller needs a /tmp to exist | ||
# Given that you cannot run any commands on a scratch container, we copy a /tmp from the previous stage | ||
COPY --from=build-env /build/tmp /tmp | ||
# Specifying an entrypoint means nothing except this command will be executed when the image is run | ||
# Since this is also a scratch image, we cannot enter the CLI when run as a container | ||
ENTRYPOINT ["./app"] |
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,27 +1,22 @@ | ||
# This time we have 3 stages for our build. | ||
# The base image will install necessary libraries | ||
FROM python:3.9-slim as build | ||
RUN apt-get update && apt-get install build-essential patchelf curl -y | ||
# In this step we install `poetry` - a depenedency managment tool for python | ||
RUN curl -sSL https://install.python-poetry.org | python - | ||
RUN apt-get update && apt-get install build-essential patchelf locales scons curl -y | ||
# Setting the workdir although we don't need it here. But this will help add a file to the workdir that will needed in the next step. | ||
WORKDIR /build | ||
COPY . /build | ||
ENV PATH="/root/.local/bin:$PATH" | ||
# Since pip cannot read and install just the dependencies from pyproject.toml, we use poetry to export the requirements alone. | ||
# --without-hashes is used primarily when there are dependencies from a VCS. Although here it doesn't apply, keeping it here. | ||
# But hash verification is recommended. | ||
RUN poetry export -f requirements.txt -o requirements.txt --without-hashes | ||
|
||
# In this tage we pack and build the binnary | ||
FROM build as build-env | ||
RUN pip install staticx pyinstaller | ||
RUN pip install -r requirements.txt | ||
RUN pyinstaller -F pydependencies/pydependencies.py -n binary --clean | ||
RUN pip install . | ||
RUN pip install pyinstaller | ||
RUN pip install staticx | ||
RUN pyinstaller -F pydependencies/pydependencies.py -n binary --clean --strip | ||
RUN staticx dist/binary dist/app --strip | ||
RUN mkdir tmp | ||
|
||
# And our magical stage that will run the binary | ||
FROM scratch | ||
COPY --from=build-env /build/dist/app /app | ||
COPY --from=build-env /tmp /tmp | ||
COPY --from=build-env /build/tmp /tmp | ||
ENTRYPOINT ["./app"] |