Skip to content

Commit

Permalink
Support workdir, entrypoint and command in agibuild.yaml (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
spillai committed Oct 9, 2023
1 parent cd37294 commit 0d042ad
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 27 deletions.
16 changes: 14 additions & 2 deletions agipack/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,17 @@ class ImageConfig:
add: Optional[List[str]] = field(default_factory=list)
"""List of files to copy into the image."""

workdir: Optional[str] = field(default=None)
"""Working directory for the image (defaults to /app/${AGIPACK_ENV} if not set)."""

run: Optional[List[str]] = field(default_factory=list)
"""List of commands to run in the image."""
"""List of commands to run in the image under the workdir."""

entrypoint: Optional[List[str]] = field(default_factory=list)
"""Entrypoint for the image."""

command: Optional[List[str]] = field(default_factory=lambda: ["bash"])
"""Command to run in the image."""

def additional_kwargs(self):
"""Additional kwargs to pass to the Jinja2 Dockerfile template."""
Expand Down Expand Up @@ -199,9 +208,12 @@ def save_yaml(self, filename: str) -> None:
# Pre-process the config to remove empty lists, etc.
data = asdict(self)
for _, config in data["images"].items():
for key in ["env", "system", "pip", "requirements", "add", "run"]:
for key in ["env", "system", "pip", "requirements", "add", "run", "entrypoint", "command"]:
if not len(config[key]):
del config[key]
for key in ["workdir"]:
if config.get(key) is None:
del config[key]
# Save the YAML file
with open(filename, "w") as f:
yaml.safe_dump(data, f, sort_keys=False)
Expand Down
17 changes: 16 additions & 1 deletion agipack/templates/Dockerfile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,17 @@ RUN echo "export CONDA_PATH=/opt/conda/envs/${AGIPACK_PYENV}" >> ~/.bashrc
RUN echo "export PATH=/opt/conda/envs/${AGIPACK_PYENV}/bin:$PATH" >> ~/.bashrc
RUN echo "export CONDA_DEFAULT_ENV=${AGIPACK_PYENV}" >> ~/.bashrc
RUN echo "mamba activate ${AGIPACK_PYENV}" > ~/.bashrc
ENTRYPOINT ["/bin/bash", "-c"]

{%- endif %}


# Setup working directory
{%- if workdir %}
WORKDIR {{ workdir }}
{%- else %}
WORKDIR /app/$AGIPACK_PYENV
{%- endif %}


{%- if run|length > 0 %}

Expand All @@ -142,3 +147,13 @@ RUN \
&& echo "run commands complete"

{%- endif %}


{%- if entrypoint|length > 0 %}
ENTRYPOINT [{%- for cmd in entrypoint %}"{{ cmd }}"{% if not loop.last %}, {% endif %}{%- endfor %}]
{%- endif %}


{%- if command %}
CMD [{{ command }}]
{%- endif %}
1 change: 1 addition & 0 deletions agipack/templates/agibuild.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ images:
- ls -la
env:
MY_ENV_VAR: value
command: ["bash"]
28 changes: 16 additions & 12 deletions examples/generated/Dockerfile-base-cpu
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
FROM debian:buster-slim AS base-cpu

# Setup environment variables
ENV PROJECT agi
ENV PYENV agi-py38
ENV AGIPACK_PROJECT agi
ENV AGIPACK_PYENV agi-py38
ENV AGIPACK_VERSION 0.1.4
ENV AGIPACK_PATH /opt/agi-pack

ENV PYTHON_VERSION 3.8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV PYTHONWARNINGS ignore

# Setup conda paths
ENV CONDA_PATH=/opt/conda/envs/${PYENV}
ENV CONDA_PATH=/opt/conda/envs/${AGIPACK_PYENV}
ENV CONDA_PREFIX=${CONDA_PATH}
ENV CONDA_EXE=${CONDA_PATH}/bin/conda
ENV PATH=${CONDA_PATH}/bin:/opt/conda/bin:$PATH
ENV CONDA_DEFAULT_ENV ${PYENV}
ENV CONDA_DEFAULT_ENV ${AGIPACK_PYENV}

# Setup environment variables
ENV MY_ENV_VAR=value
Expand All @@ -35,7 +38,7 @@ RUN curl -sLo ~/mambaforge.sh "https://github.com/conda-forge/miniforge/releases
&& ~/mambaforge.sh -b -p /opt/conda \
&& /opt/conda/bin/mamba init bash \
&& /opt/conda/bin/mamba config --set pip_interop_enabled True \
&& /opt/conda/bin/mamba create -n ${PYENV} python=${PYTHON_VERSION} -y \
&& /opt/conda/bin/mamba create -n ${AGIPACK_PYENV} python=${PYTHON_VERSION} -y \
&& rm ~/mambaforge.sh

# Install pip packages, with cache mounting /opt/conda/pkgs for faster builds
Expand All @@ -49,17 +52,18 @@ RUN --mount=type=cache,target=/opt/conda/pkgs/ \
&& echo "pip install complete"

# Export conda environment on login
RUN echo "export CONDA_PATH=/opt/conda/envs/${PYENV}" >> ~/.bashrc
RUN echo "export PATH=/opt/conda/envs/${PYENV}/bin:$PATH" >> ~/.bashrc
RUN echo "export CONDA_DEFAULT_ENV=${PYENV}" >> ~/.bashrc
RUN echo "mamba activate ${PYENV}" > ~/.bashrc
ENTRYPOINT ["/bin/bash", "-c"]
RUN echo "export CONDA_PATH=/opt/conda/envs/${AGIPACK_PYENV}" >> ~/.bashrc
RUN echo "export PATH=/opt/conda/envs/${AGIPACK_PYENV}/bin:$PATH" >> ~/.bashrc
RUN echo "export CONDA_DEFAULT_ENV=${AGIPACK_PYENV}" >> ~/.bashrc
RUN echo "mamba activate ${AGIPACK_PYENV}" > ~/.bashrc


# Setup working directory
WORKDIR /app/$PYENV
WORKDIR /app/$AGIPACK_PYENV

# Run commands
RUN \
python -c 'import cv2; print(cv2.__version__)' \
python -c 'import torch; print(torch.__version__)' \
&& echo "run commands complete"
&& echo "run commands complete"
CMD [['bash']]
28 changes: 16 additions & 12 deletions examples/generated/Dockerfile-base-cu118
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
FROM debian:buster-slim AS base-gpu

# Setup environment variables
ENV PROJECT agi
ENV PYENV agi-py38
ENV AGIPACK_PROJECT agi
ENV AGIPACK_PYENV agi-py38
ENV AGIPACK_VERSION 0.1.4
ENV AGIPACK_PATH /opt/agi-pack

ENV PYTHON_VERSION 3.8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV PYTHONWARNINGS ignore

# Setup conda paths
ENV CONDA_PATH=/opt/conda/envs/${PYENV}
ENV CONDA_PATH=/opt/conda/envs/${AGIPACK_PYENV}
ENV CONDA_PREFIX=${CONDA_PATH}
ENV CONDA_EXE=${CONDA_PATH}/bin/conda
ENV PATH=${CONDA_PATH}/bin:/opt/conda/bin:$PATH
ENV CONDA_DEFAULT_ENV ${PYENV}
ENV CONDA_DEFAULT_ENV ${AGIPACK_PYENV}

# Setup environment variables
ENV MY_ENV_VAR=value
Expand All @@ -35,7 +38,7 @@ RUN curl -sLo ~/mambaforge.sh "https://github.com/conda-forge/miniforge/releases
&& ~/mambaforge.sh -b -p /opt/conda \
&& /opt/conda/bin/mamba init bash \
&& /opt/conda/bin/mamba config --set pip_interop_enabled True \
&& /opt/conda/bin/mamba create -n ${PYENV} python=${PYTHON_VERSION} -y \
&& /opt/conda/bin/mamba create -n ${AGIPACK_PYENV} python=${PYTHON_VERSION} -y \
&& rm ~/mambaforge.sh

# Install pip packages, with cache mounting /opt/conda/pkgs for faster builds
Expand All @@ -53,19 +56,20 @@ RUN --mount=type=cache,target=/opt/conda/pkgs/ \
&& echo "pip install complete"

# Export conda environment on login
RUN echo "export CONDA_PATH=/opt/conda/envs/${PYENV}" >> ~/.bashrc
RUN echo "export PATH=/opt/conda/envs/${PYENV}/bin:$PATH" >> ~/.bashrc
RUN echo "export CONDA_DEFAULT_ENV=${PYENV}" >> ~/.bashrc
RUN echo "mamba activate ${PYENV}" > ~/.bashrc
ENTRYPOINT ["/bin/bash", "-c"]
RUN echo "export CONDA_PATH=/opt/conda/envs/${AGIPACK_PYENV}" >> ~/.bashrc
RUN echo "export PATH=/opt/conda/envs/${AGIPACK_PYENV}/bin:$PATH" >> ~/.bashrc
RUN echo "export CONDA_DEFAULT_ENV=${AGIPACK_PYENV}" >> ~/.bashrc
RUN echo "mamba activate ${AGIPACK_PYENV}" > ~/.bashrc


# Setup working directory
WORKDIR /app/$PYENV
WORKDIR /app/$AGIPACK_PYENV

# Run commands
RUN \
echo 'pytorch: ' && python -c 'import torch; print(torch.__version__)' \
echo 'cuda: ' && python -c 'import torch; print(torch.version.cuda)' \
echo 'cudnn: ' && python -c 'import torch; print(torch.backends.cudnn.version())' \
echo 'opencv:' && python -c 'import cv2; print(cv2.__version__)' \
&& echo "run commands complete"
&& echo "run commands complete"
CMD [['bash']]

0 comments on commit 0d042ad

Please sign in to comment.