|
| 1 | +# using ubuntu LTS version |
| 2 | +FROM ubuntu:20.04 AS builder-image |
| 3 | + |
| 4 | +# avoid stuck build due to user prompt |
| 5 | +ARG DEBIAN_FRONTEND=noninteractive |
| 6 | + |
| 7 | +RUN apt-get update \ |
| 8 | + && apt-get install \ |
| 9 | + --no-install-recommends -y \ |
| 10 | + aptitude \ |
| 11 | + autoconf \ |
| 12 | + automake \ |
| 13 | + build-essential \ |
| 14 | + ca-certificates \ |
| 15 | + curl \ |
| 16 | + git \ |
| 17 | + locales \ |
| 18 | + libffi-dev \ |
| 19 | + libncurses-dev \ |
| 20 | + libreadline-dev \ |
| 21 | + libssl-dev \ |
| 22 | + libtool \ |
| 23 | + libxslt-dev \ |
| 24 | + libyaml-dev \ |
| 25 | + python3 \ |
| 26 | + python3-dev \ |
| 27 | + python3-pip \ |
| 28 | + unixodbc-dev \ |
| 29 | + unzip \ |
| 30 | + && rm -rf /var/lib/apt/lists/* |
| 31 | + |
| 32 | +# Set locale |
| 33 | +RUN locale-gen en_US.UTF-8 |
| 34 | +ENV LANG=en_US.UTF-8 |
| 35 | +ENV LANGUAGE=en_US:en |
| 36 | +ENV LC_ALL=en_US.UTF-8 |
| 37 | + |
| 38 | +ARG USERNAME=appuser |
| 39 | +ENV HOME="/home/${USERNAME}" |
| 40 | +ENV PATH="$HOME/.asdf/bin:$HOME/.asdf/shims:$PATH" |
| 41 | + |
| 42 | +RUN useradd --create-home $USERNAME |
| 43 | + |
| 44 | +# install asdf then python latest |
| 45 | +RUN bash -c "git clone --depth 1 https://github.com/asdf-vm/asdf.git $HOME/.asdf \ |
| 46 | + && echo '. $HOME/.asdf/asdf.sh' >> $HOME/.bashrc \ |
| 47 | + && echo '. $HOME/.asdf/asdf.sh' >> $HOME/.profile" |
| 48 | +RUN asdf plugin-add python \ |
| 49 | + && asdf install python 3.10.5 \ |
| 50 | + && asdf global python 3.10.5 |
| 51 | + |
| 52 | +ENV POETRY_HOME="$HOME/.poetry" |
| 53 | +RUN curl -sSL https://install.python-poetry.org | python3.10 - |
| 54 | +ENV PATH "${POETRY_HOME}/bin:$PATH" |
| 55 | + |
| 56 | +WORKDIR /app |
| 57 | +COPY pyproject.toml poetry.lock ./ |
| 58 | +RUN python3.10 -m venv /opt/venv |
| 59 | + |
| 60 | +# Install pip requirements |
| 61 | +RUN . /opt/venv/bin/activate && poetry install |
| 62 | + |
| 63 | +FROM ubuntu:20.04 AS runner-image |
| 64 | + |
| 65 | +ARG USERNAME=appuser |
| 66 | +ENV HOME="/home/${USERNAME}" |
| 67 | +ENV VIRTUAL_ENV="/opt/venv" |
| 68 | +ENV PATH="${VIRTUAL_ENV}/bin:$HOME/.asdf/bin:$HOME/.asdf/shims:$PATH" |
| 69 | + |
| 70 | +RUN useradd --create-home $USERNAME \ |
| 71 | + && mkdir -p /home/${USERNAME}/app |
| 72 | + |
| 73 | +COPY --chown=${USERNAME}:${USERNAME} . $HOME/app |
| 74 | +COPY --from=builder-image --chown=${USERNAME}:${USERNAME} /opt/venv /opt/venv |
| 75 | +COPY --from=builder-image --chown=${USERNAME}:${USERNAME} $HOME/.asdf $HOME/.asdf |
| 76 | + |
| 77 | +# avoid stuck build due to user prompt |
| 78 | +ARG DEBIAN_FRONTEND=noninteractive |
| 79 | + |
| 80 | +RUN apt-get update \ |
| 81 | + && apt-get install \ |
| 82 | + --no-install-recommends -y \ |
| 83 | + apt-transport-https \ |
| 84 | + ca-certificates \ |
| 85 | + curl \ |
| 86 | + git \ |
| 87 | + gnupg \ |
| 88 | + software-properties-common \ |
| 89 | + wget \ |
| 90 | + && wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | apt-key add - \ |
| 91 | + && echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-5.0.list \ |
| 92 | + && apt-get update \ |
| 93 | + && apt-get install \ |
| 94 | + --no-install-recommends -y \ |
| 95 | + mongodb-org \ |
| 96 | + && rm -rf /var/lib/apt/lists/* |
| 97 | + |
| 98 | +# Keeps Python from generating .pyc files in the container |
| 99 | +ENV PYTHONDONTWRITEBYTECODE=1 |
| 100 | + |
| 101 | +# Turns off buffering for easier container logging |
| 102 | +ENV PYTHONUNBUFFERED=1 |
| 103 | + |
| 104 | +# activate virtual environment |
| 105 | +RUN python -m venv $VIRTUAL_ENV |
| 106 | + |
| 107 | +USER appuser |
| 108 | + |
| 109 | +WORKDIR $HOME/app |
| 110 | + |
| 111 | +# ENTRYPOINT ["python", "hello.py"] |
| 112 | +CMD ["/bin/bash"] |
0 commit comments