Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: multistage build for the base container #166

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
99e813f
fix: move the base container to use a multistage build
Feb 16, 2024
b9066e1
have the first stage build from ubuntu and move sagemaker-training to…
Feb 16, 2024
aed5871
move python deps to start
Feb 16, 2024
6f505ae
test new dockerfile
Feb 20, 2024
ce84b08
Better organize python dependencies for slimming the image
Feb 20, 2024
0435568
Update Dockerfile.cpu
AbeCoull Feb 20, 2024
005c0b4
add compliance check
AbeCoull Mar 11, 2024
a6d4b61
Merge branch 'main' into multi_stage_test
AbeCoull Mar 11, 2024
43af838
Merge branch 'main' into multi_stage_test
AbeCoull Mar 15, 2024
8adee85
Merge branch 'main' into multi_stage_test
AbeCoull Apr 10, 2024
ad462ad
Update Dockerfile.cpu
AbeCoull Apr 10, 2024
2dee634
Update Dockerfile.cpu
AbeCoull Apr 10, 2024
f2663dd
Update Dockerfile.cpu
AbeCoull Apr 10, 2024
35c53a7
Update Dockerfile.cpu
AbeCoull Apr 10, 2024
ec86f5c
Update Dockerfile.cpu
AbeCoull Apr 11, 2024
1309e3d
Update Dockerfile.cpu
AbeCoull Apr 26, 2024
84b69b2
Update Dockerfile.cpu
AbeCoull Apr 26, 2024
9df8406
Merge branch 'main' into multi_stage_test
AbeCoull Apr 26, 2024
56c5016
Merge branch 'main' into multi_stage_test
AbeCoull May 2, 2024
66f5475
Update Dockerfile.cpu
AbeCoull Jun 20, 2024
c10a6f8
Merge branch 'main' into multi_stage_test
AbeCoull Jun 21, 2024
a3ff510
Merge branch 'main' into multi_stage_test
AbeCoull Oct 23, 2024
9afd28f
Update Dockerfile.cpu
AbeCoull Oct 23, 2024
f742cd0
Try building off of the ubuntu image for the OSS compliance to pass
AbeCoull Oct 23, 2024
6f5edb7
add curl
AbeCoull Oct 23, 2024
b4c0ae7
add unzip
AbeCoull Oct 23, 2024
686087d
Merge branch 'main' into multi_stage_test
AbeCoull Oct 24, 2024
9996e55
Merge branch 'main' into multi_stage_test
AbeCoull Dec 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 80 additions & 108 deletions base/jobs/docker/1.0/py3/Dockerfile.cpu
Original file line number Diff line number Diff line change
@@ -1,29 +1,53 @@
# This is based loosely on :
# https://github.com/aws/amazon-sagemaker-examples/blob/master/advanced_functionality/custom-training-containers/script-mode-container/docker/Dockerfile

FROM public.ecr.aws/lts/ubuntu:22.04

FROM public.ecr.aws/lts/ubuntu:22.04 AS builder
LABEL maintainer="Amazon Braket"
LABEL major_version="1"

# prevent stopping by user interaction
ENV DEBIAN_FRONTEND noninteractive
ENV DEBCONF_NONINTERACTIVE_SEEN true
# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive \
PYTHONUNBUFFERED=1 \
LANG=C.UTF-8 \
LC_ALL=C.UTF-8

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PYTHONIOENCODING=UTF-8
ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8
# Install Python and pip
RUN apt-get update && apt-get install -y --no-install-recommends \
python3-pip \
python3-dev \
build-essential \
curl \
unzip \
&& rm -rf /var/lib/apt/lists/*

ARG PYTHON=python3.10
ARG PYTHON_PIP=python3-pip
ARG PIP=pip3
ARG PYTHON_VERSION=3.10.11
# Copy the dependencies file
COPY requirements.txt /

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
build-essential \
# Install Python dependencies
RUN pip3 install --no-cache-dir --upgrade pip setuptools \
&& pip3 install --no-cache-dir -r requirements.txt

# Install OSS compliance tools
RUN HOME_DIR=/root \
&& curl -o ${HOME_DIR}/oss_compliance.zip https://aws-dlinfra-utilities.s3.amazonaws.com/oss_compliance.zip \
&& unzip ${HOME_DIR}/oss_compliance.zip -d ${HOME_DIR}/ \
&& cp ${HOME_DIR}/oss_compliance/test/testOSSCompliance /usr/local/bin/testOSSCompliance \
&& chmod +x /usr/local/bin/testOSSCompliance \
&& chmod +x ${HOME_DIR}/oss_compliance/generate_oss_compliance.sh \
&& ${HOME_DIR}/oss_compliance/generate_oss_compliance.sh ${HOME_DIR} python3 \
&& rm -rf ${HOME_DIR}/oss_compliance*


# Stage 1: Final Image
FROM public.ecr.aws/lts/ubuntu:22.04
LABEL maintainer="Amazon Braket"
LABEL major_version="1"

# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive \
PYTHONUNBUFFERED=1 \
LANG=C.UTF-8 \
LC_ALL=C.UTF-8

# Install runtime dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
openssh-client \
openssh-server \
ca-certificates \
Expand All @@ -33,93 +57,41 @@ RUN apt-get update \
libssl3 \
openssl \
unzip \
wget \
zlib1g-dev \
# Install dependent library for OpenCV
libgtk2.0-dev \
&& rm -rf /var/lib/apt/lists/*

# Install Open MPI
RUN mkdir /tmp/openmpi \
&& cd /tmp/openmpi \
&& curl -fSsL -O https://download.open-mpi.org/release/open-mpi/v4.0/openmpi-4.0.4.tar.gz \
&& tar zxf openmpi-4.0.4.tar.gz \
&& cd openmpi-4.0.4 \
&& ./configure --enable-orterun-prefix-by-default \
&& make -j $(nproc) all \
&& make install \
&& ldconfig \
&& rm -rf /tmp/openmpi

# Create a wrapper for OpenMPI to allow running as root by default
RUN mv /usr/local/bin/mpirun /usr/local/bin/mpirun.real \
&& echo '#!/bin/bash' > /usr/local/bin/mpirun \
&& echo 'mpirun.real --allow-run-as-root "$@"' >> /usr/local/bin/mpirun \
&& chmod a+x /usr/local/bin/mpirun

RUN echo "hwloc_base_binding_policy = none" >> /usr/local/etc/openmpi-mca-params.conf \
&& echo "rmaps_base_mapping_policy = slot" >> /usr/local/etc/openmpi-mca-params.conf

ENV LD_LIBRARY_PATH=/usr/local/openmpi/lib:/usr/local/lib:$LD_LIBRARY_PATH
ENV PATH /usr/local/openmpi/bin/:$PATH

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

# Create SSH key.
RUN mkdir -p /root/.ssh/ \
&& mkdir -p /var/run/sshd \
&& ssh-keygen -q -t rsa -N '' -f /root/.ssh/id_rsa \
&& cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys \
&& printf "Host *\n StrictHostKeyChecking no\n" >> /root/.ssh/config

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
libbz2-dev \
libc6-dev \
libffi-dev \
libgdbm-dev \
liblzma-dev \
libncursesw5-dev \
libsqlite3-dev \
libssl-dev \
tk-dev \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean

# Installing Python3
RUN wget https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz \
&& tar -xvf Python-$PYTHON_VERSION.tgz \
&& cd Python-$PYTHON_VERSION \
&& ./configure && make && make install \
&& rm -rf ../Python-$PYTHON_VERSION*

# Upgrading pip and creating symbolic link for python3
RUN ${PIP} --no-cache-dir install --upgrade pip setuptools
RUN ln -s $(which ${PYTHON}) /usr/local/bin/python \
&& ln -s $(which ${PIP}) /usr/bin/pip

WORKDIR /

RUN apt-get update && apt-get -y install cmake protobuf-compiler

# Copy the dependencies file
COPY requirements.txt /
# Installing our custom python libraries
RUN ${PIP} install --no-cache --upgrade \
-r requirements.txt

RUN HOME_DIR=/root \
&& curl -o ${HOME_DIR}/oss_compliance.zip https://aws-dlinfra-utilities.s3.amazonaws.com/oss_compliance.zip \
&& unzip ${HOME_DIR}/oss_compliance.zip -d ${HOME_DIR}/ \
&& cp ${HOME_DIR}/oss_compliance/test/testOSSCompliance /usr/local/bin/testOSSCompliance \
&& chmod +x /usr/local/bin/testOSSCompliance \
&& chmod +x ${HOME_DIR}/oss_compliance/generate_oss_compliance.sh \
&& ${HOME_DIR}/oss_compliance/generate_oss_compliance.sh ${HOME_DIR} ${PYTHON} \
&& rm -rf ${HOME_DIR}/oss_compliance*

# Setup our entry point

python3-pip \
libopenmpi-dev \
&& rm -rf /var/lib/apt/lists/*

# SSH login fix and key generation
RUN sed -i 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' /etc/pam.d/sshd \
&& mkdir -p /root/.ssh/ /var/run/sshd \
&& ssh-keygen -q -t rsa -N '' -f /root/.ssh/id_rsa \
&& cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys \
&& printf "Host *\n StrictHostKeyChecking no\n" >> /root/.ssh/config

# Copy Python dependencies from the builder stage
COPY --from=builder /usr/local/lib/python3.10 /usr/local/lib/python3.10
COPY --from=builder /usr/local/bin/pip3 /usr/local/bin/pip3
COPY --from=builder /usr/local/bin/testOSSCompliance /usr/local/bin/testOSSCompliance

# Ensure Python links are correct
RUN ln -s /usr/bin/python3 /usr/local/bin/python \
&& ln -s /usr/bin/python3 /usr/local/bin/python3

# Install Open MPI wrapper to allow running as root
RUN mv /usr/bin/mpirun /usr/bin/mpirun.real \
&& echo '#!/bin/bash' > /usr/bin/mpirun \
&& echo 'mpirun.real --allow-run-as-root "$@"' >> /usr/bin/mpirun \
&& chmod a+x /usr/bin/mpirun

# Set Open MPI configurations
RUN echo "hwloc_base_binding_policy = none" >> /etc/openmpi/openmpi-mca-params.conf \
&& echo "rmaps_base_mapping_policy = slot" >> /etc/openmpi/openmpi-mca-params.conf

# Set up environment variables
ENV LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/openmpi/lib:$LD_LIBRARY_PATH \
PATH=/usr/lib/x86_64-linux-gnu/openmpi/bin:$PATH

# Copy the braket_container.py
COPY braket_container.py /opt/ml/code/braket_container.py

ENV SAGEMAKER_PROGRAM braket_container.py
ENV SAGEMAKER_PROGRAM=braket_container.py
Loading