Skip to content

Commit

Permalink
Update dockerfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
navhits committed Jun 18, 2023
1 parent 61b3ed5 commit ae03f2a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 45 deletions.
19 changes: 9 additions & 10 deletions awesomeapi/Dockerfile
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"]
2 changes: 1 addition & 1 deletion awesomeapi/README.md
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/)
23 changes: 9 additions & 14 deletions awesomecli/Dockerfile
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"]
4 changes: 2 additions & 2 deletions awesomecli/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ exclude = ["awesomecli/entrypoint.py"]

[tool.poetry.dependencies]
python = "^3.9"
typer = "^0.4.0"
typer = "^0.9.0"

[tool.poetry.dev-dependencies]
pytest = "^5.2"

[tool.poetry.scripts]
trek = "awesomecli.cli:app"
awesomecli = "awesomecli.cli:app"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
15 changes: 9 additions & 6 deletions helloworld/Dockerfile
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"]
19 changes: 7 additions & 12 deletions pydependencies/Dockerfile
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"]

0 comments on commit ae03f2a

Please sign in to comment.