Skip to content

Commit

Permalink
Decouple AIP from Conda (#42)
Browse files Browse the repository at this point in the history
* File no longer needed

* File not updated and not needed

* Add dockerignore

* Remove unused production requirements

* Update to use venv

* New multi-stage slim dockerfile

* Not needed anymore
  • Loading branch information
verovaleros authored Oct 27, 2024
1 parent 31fb739 commit 067dcca
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 118 deletions.
33 changes: 33 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
venv/
env/
*.pyc
*.pyo
*.pyd
__pycache__/
*.so
*.egg
*.egg-info/
.eggs/
.git/
.gitignore
.DS_Store
Thumbs.db
*.log
*.swp
*.tmp
build/
dist/
*.egg-info/
*.tar.gz
*.zip
node_modules/
.idea/
.vscode/
*.sublime-project
*.sublime-workspace
.dockerignore
Dockerfile
Dockerfile_MacM1
images/
.github
data/
7 changes: 0 additions & 7 deletions environment.yml

This file was deleted.

75 changes: 42 additions & 33 deletions etc/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,59 +1,68 @@
FROM python:3.12-slim
LABEL org.opencontainers.image.title="AIP" \
org.opencontainers.image.description="This image runs the AIP framework for blocklist generation." \
org.opencontainers.image.version="0.1.0" \
org.opencontainers.image.created="2023-08-01" \
org.opencontainers.image.source="https://github.com/stratosphereips/AIP" \
org.opencontainers.image.source="Joaquin Bogado <[email protected]>" \
org.opencontainers.image.authors="Veronica Valeros <[email protected]>"

FROM python:3.12-slim AS builder

# Define arguments for username, UID, and GID
# Create a non-root user
ARG username=aip
ARG uid=1000
ARG gid=1000

# Set environment variables based on these arguments
ENV USER=$username
ENV UID=$uid
ENV GID=$gid
ENV HOME=/home/$USER

# Create a group and user based on the UID and GID
RUN apt-get update && \
apt-get install -y python3-venv && \
rm -rf /var/lib/apt/lists/*

RUN groupadd -g $GID $USER && \
useradd -m -u $UID -g $GID -s /bin/bash $USER

COPY etc/docker/entrypoint.sh /usr/local/bin/
RUN chmod u+x /usr/local/bin/entrypoint.sh

# Switch to the non-root user
USER $USER
ENV PATH="$HOME/miniconda3/bin:$PATH"
ENV ENV_PREFIX=$HOME/env

WORKDIR $HOME/AIP

COPY requirements.txt .

# Conda installation and setup
RUN python -c "import urllib.request; urllib.request.urlretrieve('https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh', '$HOME/miniconda.sh')" && \
bash ~/miniconda.sh -b -p $HOME/miniconda3 && \
rm ~/miniconda.sh
RUN python -m venv venv && \
./venv/bin/pip install --no-cache-dir -r requirements.txt

RUN conda init bash
# Remove unnecessary files
RUN find venv/ -type d -name '__pycache__' -exec rm -rf {} +
RUN find venv/ -type d -name 'tests' -exec rm -rf {} + && \
find venv/ -type d -name '*.dist-info' -exec rm -rf {} +

# Set the working directory
WORKDIR $HOME/AIP
# Stage 2: Final stage
FROM python:3.12-slim

# Create a non-root user
ARG username=aip
ARG uid=1000
ARG gid=1000

COPY environment.yml requirements.txt $HOME/AIP/
ENV USER=$username
ENV UID=$uid
ENV GID=$gid
ENV HOME=/home/$USER

RUN groupadd -g $GID $USER && \
useradd -m -u $UID -g $GID -s /bin/bash $USER

RUN conda update --name base conda
RUN conda env create --file environment.yml && \
conda clean --all --yes
# Copy the entrypoint script
COPY etc/docker/entrypoint.sh /usr/local/bin/
RUN chmod u+x /usr/local/bin/entrypoint.sh

# Switch to the non-root user
USER $USER

WORKDIR $HOME/AIP

# Copy application
COPY . .
# Copy venv from the builder stage
COPY --from=builder $HOME/AIP/venv $HOME/AIP/venv

# Dynamically link aip to the correct site-packages folder
RUN ln -s $HOME/AIP/lib/aip $(conda run -n aip python -c "import site; print(site.getsitepackages()[0])")/aip
# Copy aip files
COPY --chown=$USER:$USER . .

RUN echo 'conda activate aip' >> $HOME/.bashrc
ENV PATH="$HOME/AIP/venv/bin:$PATH"

ENTRYPOINT [ "/usr/local/bin/entrypoint.sh" ]
59 changes: 0 additions & 59 deletions etc/docker/Dockerfile_MacM1

This file was deleted.

9 changes: 4 additions & 5 deletions etc/docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ set -e
# Force the command prompt to display colors
export force_color_prompt=yes

# Initialize Conda in the current shell session
source $HOME/miniconda3/etc/profile.d/conda.sh
# Activate the virtual environment
source "$HOME/AIP/venv/bin/activate"

# Activate the conda environment already created in the docker
conda activate aip

# Execute any command passed to the container when run
exec "$@"
PYTHONPATH="$HOME/AIP/lib:$PYTHONPATH" python "$@"

4 changes: 0 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
netaddr==0.8.0
maxminddb==2.2.0
zeeklog2pandas
ipython
scikit-learn
pathlib
joblib
python-dotenv
matplotlib
pandas
10 changes: 0 additions & 10 deletions version.py

This file was deleted.

0 comments on commit 067dcca

Please sign in to comment.