\documentclass{article}
\usepackage[margin=2cm]{geometry}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} % Support for Polish characters
\usepackage{polski} % Polish language settings
\usepackage[english]{babel} % Multilingual support, with Polish as default

\usepackage{listings}
\usepackage{xcolor}

\definecolor{vscodePurple}{rgb}{0.5, 0.0, 0.5} % Function names, keywords
\definecolor{vscodeBlue}{rgb}{0.16, 0.32, 0.75} % Comments
\definecolor{vscodeGreen}{rgb}{0, 0.6, 0} % Strings

% Configuration for the listings package
\lstset{
    basicstyle=\ttfamily,
    columns=fullflexible,
    keywordstyle=\color{vscodePurple},
    stringstyle=\color{vscodeGreen},
    commentstyle=\color{vscodeBlue},
    morecomment=[l][\color{magenta}]{\#},
    frame=single,
    language=Python,
    showstringspaces=false,
    breaklines=true, % Enables line breaking
    postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space}, % Marks where a line has been broken
}

\usepackage{graphicx} % Add the graphicx package for \reflectbox

\usepackage{hyperref}

\usepackage[
    sortcites,
    backend=biber,
    hyperref=true,
    firstinits=true,
    maxbibnames=99,
    ]{biblatex}
\addbibresource{references.bib}


\title{Dockerfile for FastAPI Application using Pyenv and Poetry}
\author{M. Pabiszczak}
\date{\today}

\begin{document}

\maketitle

\section{Description of Dockerfile}

\subsection{Base Image}
\textbf{Dockerfile Instruction:}
\begin{verbatim}
FROM debian:latest
\end{verbatim}
Choose a base image. Debian is a good choice due to Pyenv.

\subsection{Installing Dependencies}
\textbf{Dockerfile Instruction:}
\begin{verbatim}
RUN apt-get update && apt-get install -y \
    aptitude \
    tmux vim-nox nano mc git curl termshark procps \
    sudo iproute2 iputils-ping bridge-utils \
    ifupdown udev make build-essential libssl-dev zlib1g-dev \
    libbz2-dev libreadline-dev libsqlite3-dev wget llvm libncurses5-dev \
    libncursesw5-dev xz-utils libffi-dev liblzma-dev \
    sqlite3 default-libmysqlclient-dev pkg-config

RUN echo 'root:pass' | chpasswd

RUN useradd -m user && echo 'user:pass' | chpasswd && adduser user sudo
\end{verbatim}
Update the package list and install necessary dependencies.

\subsection{Installing Pyenv}
\textbf{Dockerfile Instruction:}
\begin{verbatim}
RUN git clone https://github.com/pyenv/pyenv .pyenv

RUN git clone https://github.com/pyenv/pyenv-virtualenv .pyenv/plugins/pyenv-virtualenv

ENV HOME  /home/user
ENV PYENV_ROOT $HOME/.pyenv
ENV PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH

RUN echo 'export PYENV_ROOT="$HOME/.pyenv"' >> .bashrc
RUN echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> .bashrc
RUN echo 'eval "$(pyenv init -)"' >> .bashrc
RUN echo 'eval "$(pyenv virtualenv-init -)"' >> .bashrc
\end{verbatim}
Install Pyenv and Pyenv-virtualenv.

\subsection{Installing Python 3.11}
\textbf{Dockerfile Instruction:}
\begin{verbatim}
RUN bash -i -c "source ~/.bashrc && env PYTHON_CONFIGURE_OPTS='--enable-shared' pyenv install 3.11"

RUN bash -i -c "source ~/.bashrc &&  pyenv virtualenv 3.11 p3.11"

RUN bash -i -c "source ~/.bashrc &&  pyenv local 3.11"
\end{verbatim}
Install Python 3.11 using Pyenv.

\subsection{Installing Poetry}
\textbf{Dockerfile Instruction:}
\begin{verbatim}
RUN curl -sSL https://install.python-poetry.org | python3 -
\end{verbatim}
Install Poetry.

\subsection{Project Configuration}
\textbf{Dockerfile Instruction:}
\begin{verbatim}
COPY --chown=user:user . /home/user/fapi
COPY ./_confs/* ./

WORKDIR /home/user/fapi

ENV PATH="$HOME/.local/bin:$PATH"

RUN bash -i -c "source ~/.bashrc && poetry config virtualenvs.create false && poetry install"
\end{verbatim}
Copy the project into the image, configure the Poetry path, and install dependencies.

\subsection{Running the Application}
\textbf{Dockerfile Instruction:}
\begin{verbatim}
CMD ["/bin/bash", "-c", "python entrypoint.py"]
\end{verbatim}
Run the application.

\section{Running the Container}

\subsection{Running with Default Command}
\textbf{Docker Command:}
\begin{verbatim}
docker run --rm -dit -p 9999:9999 fapi
\end{verbatim}
Run the container with the default command (CMD).

\subsection{Running with Bash Shell}
\textbf{Docker Command:}
\begin{verbatim}
docker run --rm -dit -p 9999:9999 --name cfapi fapi bash
\end{verbatim}
Run the container with a bash shell.

\subsection{Accessing Container Shell}
\textbf{Docker Command:}
\begin{verbatim}
docker exec -it cfapi bash
\end{verbatim}
Allows accessing the shell of the container named cfapi.

\appendix
\section{Complete Dockerfile with Database Creation}

\begin{verbatim}
# Choose a base image. Debian is a good choice due to pyenv
FROM debian:latest

# Update the package list and install necessary dependencies
RUN apt-get update && apt-get install -y \
    aptitude \
    tmux vim-nox nano mc git curl termshark procps \
    sudo iproute2 iputils-ping bridge-utils \
    ifupdown udev make build-essential libssl-dev zlib1g-dev \
    libbz2-dev libreadline-dev libsqlite3-dev wget llvm libncurses5-dev \
    libncursesw5-dev xz-utils libffi-dev liblzma-dev \
    sqlite3 default-libmysqlclient-dev pkg-config

# Set password for root user (optional, for configuration)
RUN echo 'root:rootpass' | chpasswd

# Add a new user `user` with password `pass` and prepare the environment
RUN useradd -m user && echo 'user:pass' | chpasswd && adduser user sudo

# Switch to user `user`
USER user
WORKDIR /home/user

# Install pyenv
RUN git clone https://github.com/pyenv/pyenv .pyenv

# Install pyenv-virtualenv
RUN git clone https://github.com/pyenv/pyenv-virtualenv .pyenv/plugins/pyenv-virtualenv

# Set environment variables
ENV HOME  /home/user
ENV PYENV_ROOT $HOME/.pyenv
ENV PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH

# Add pyenv and pyenv-virtualenv configuration to .bashrc
RUN echo 'export PYENV_ROOT="$HOME/.pyenv"' >> .bashrc
RUN echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> .bashrc
RUN echo 'eval "$(pyenv init -)"' >> .bashrc
RUN echo 'eval "$(pyenv virtualenv-init -)"' >> .bashrc

# Install Python 3.11 with --enable-shared
RUN bash -i -c "source ~/.bashrc && env PYTHON_CONFIGURE_OPTS='--enable-shared' pyenv install 3.11"

# Create a virtual environment p3.11
RUN bash -i -c "source ~/.bashrc &&  pyenv virtualenv 3.11 p3.11"

# Set p3.11 as the default virtual environment, docker doesn't like ;)
RUN bash -i -c "source ~/.bashrc &&  pyenv local 3.11"

# Install Poetry
RUN curl -sSL https://install.python-poetry.org | python3 -

# Copy the project into the image
COPY --chown=user:user . /home/user/fapi
COPY ./_confs/* ./

# Set the working directory for installing dependencies
WORKDIR /home/user/fapi

# Add Poetry path to PATH directly in the Dockerfile
ENV PATH="$HOME/.local/bin:$PATH"

RUN bash -i -c "source ~/.bashrc && poetry config virtualenvs.create false && poetry install"

# Run the application
CMD ["/bin/bash", "-c", "python entrypoint.py"]
\end{verbatim}

\end{document}
