Skip to content

Commit

Permalink
Merge pull request #181 from jupyter-naas/package-abi-into-container
Browse files Browse the repository at this point in the history
Package abi into container
  • Loading branch information
Dr0p42 authored Jul 24, 2024
2 parents 256563e + 80b2d84 commit 245414c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.abi-conda
.logs
Dockerfile*
62 changes: 62 additions & 0 deletions Dockerfile.linux.x86_64
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Use the official Python 3.12 slim image based on Debian Bookworm as the base image
FROM python:3.12-slim-bookworm

# Set environment variables for build and target platforms
ENV BUILDPLATFORM=Linux
ENV TARGETPLATFORM=x86_64

# Update the package list, install wget and make, download and install Miniconda, and clean up
RUN apt update \
&& apt install --yes wget make \
&& cd /tmp \
&& wget https://repo.anaconda.com/miniconda/Miniconda3-latest-$BUILDPLATFORM-$TARGETPLATFORM.sh \
&& bash Miniconda3-latest-$BUILDPLATFORM-$TARGETPLATFORM.sh -b -u -p /opt/conda \
&& /opt/conda/bin/conda init bash \
&& apt clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Update PATH to include Miniconda binaries
ENV PATH="/opt/conda/bin:${PATH}"

# Create a new user 'abi' with a home directory and bash shell
RUN useradd -m abi -s /bin/bash \
&& mkdir /app && chown abi /app

# Set the working directory to /app
WORKDIR /app

# Add conda.yml and Makefile to /app with ownership set to 'abi'
ADD --chown=abi:abi conda.yml Makefile /app/

# Switch to user 'abi'
USER abi

# Initialize conda for bash shell
RUN conda init bash

# Add conda environment activation command to .bashrc for user 'abi'
RUN echo "conda activate /app/.abi-conda" >> /home/abi/.bashrc

# Switch back to root user
USER root

# Run a complex command to install conda packages and clean up to reduce image size
RUN su - abi -c "export PATH=\"/opt/conda/bin:${PATH}\" && cd /app && make conda-install-kernel && conda clean --all -y" && \
conda clean --all -y && \
find /opt/conda/ -type f -name '*.a' -delete && \
find /opt/conda/ -type f -name '*.pyc' -delete && \
find /opt/conda/ -type f -name '*.pyo' -delete && \
find /opt/conda/ -type f -name '*.js.map' -delete && \
rm -rf /opt/conda/pkgs && \
rm -rf /opt/conda/conda-meta

# Add conda environment activation command to .bashrc for root user
RUN echo "conda activate /app/.abi-conda" >> /root/.bashrc

# Add all remaining files to /app with ownership set to 'abi'
ADD --chown=abi:abi . .

# Switch back to user 'abi'
USER abi

# Set the entrypoint to the docker-entrypoint.sh script
ENTRYPOINT ["/app/docker-entrypoint.sh"]
24 changes: 18 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Usage:
make conda-export: Export the conda environment to conda.yml
make windows-install-conda: Download and install Miniconda for Windows
make usage: Show this message
make build: Build the Docker image to package ABI.


endef
Expand Down Expand Up @@ -79,26 +80,31 @@ CONDA_ENV_HASH = .abi-conda/$(shell cat conda.yml | $(MD5) | sed 's/ .*//g').has
$(CONDA_ENV_HASH): .abi-conda
@ echo "\n📦 conda.yml drift detected. Updating conda environment ...\n\n"
@ make conda-env-update && \
(rm .abi-conda/*.hash || true) && \
touch $(CONDA_ENV_HASH) && \
make conda-update-hash && \
echo "\n\n✅ conda environment updated.\n\n"

conda-update-hash:
@ (rm .abi-conda/*.hash || true) && \
touch $(CONDA_ENV_HASH)

.abi-conda:
conda env create -f conda.yml --prefix .abi-conda
make conda-update-hash

dependencies: $(CONDA_ENV_HASH)
dep: dependencies
dependencies: .abi-conda $(CONDA_ENV_HASH)

conda-env-add:
conda run -p .abi-conda pip install $(package)

conda-env-update:
conda env update --file conda.yml --prune -p .abi-conda

conda-install-kernel: $(CONDA_ENV_HASH)
conda-install-kernel: dep
conda run -p .abi-conda python -m ipykernel install --user --name abi --display-name "abi"
conda run -p .abi-conda jupyter kernelspec install --user .abi-conda/share/jupyter/kernels/python3/

conda-export: $(CONDA_ENV_HASH)
conda-export: dep
@ rm .naas_drivers_packages > /dev/null 2>&1; cat conda.yml | grep '\- naas-drivers' | sed 's/^.*\[//g; s/\].*//g' > .naas_drivers_packages
@ echo "name: .abi-conda" > conda.yml
@ conda run -p .abi-conda conda env export --no-builds | grep -v "^prefix: " | grep -v "^name: " >> conda.yml
Expand All @@ -108,4 +114,10 @@ conda-export: $(CONDA_ENV_HASH)
windows-install-conda:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod +x Miniconda3-latest-Linux-x86_64.sh
./Miniconda3-latest-Linux-x86_64.sh
./Miniconda3-latest-Linux-x86_64.sh

# Docker
build: build.linux.x86_64

build.linux.x86_64:
docker build . -t abi -f Dockerfile.linux.x86_64 --platform linux/amd64
10 changes: 10 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

# Enable conda in this script.
source /opt/conda/etc/profile.d/conda.sh

# Activate the conda environment.
conda activate /app/.abi-conda

# Executing user command.
exec "$@"

0 comments on commit 245414c

Please sign in to comment.