FROM ubuntu:18.04

LABEL maintainer="Amazon AI"

# prevent stopping by user interaction
ENV DEBIAN_FRONTEND noninteractive
ENV DEBCONF_NONINTERACTIVE_SEEN true
ENV SAGEMAKER_TRAINING_MODULE sagemaker_tensorflow_container.training:main

# Set environment variables for MKL
# For more about MKL with TensorFlow see:
# https://www.tensorflow.org/performance/performance_guide#tensorflow_with_intel%C2%AE_mkl_dnn
ENV KMP_AFFINITY=granularity=fine,compact,1,0 
ENV KMP_BLOCKTIME=1 
ENV KMP_SETTINGS=0

ENV PYTHONDONTWRITEBYTECODE=1 
ENV PYTHONUNBUFFERED=1 
ENV PYTHONIOENCODING=UTF-8 
ENV LANG=C.UTF-8 
ENV LC_ALL=C.UTF-8

ARG PYTHON=python3
ARG PYTHON_PIP=python3-pip
ARG PIP=pip3

# Use TENSORFLOW_WHL instead of TF_URL before releasing
# ARG TF_URL="https://tensorflow-aws.s3-us-west-2.amazonaws.com/1.14/AmazonLinux/cpu/final/tensorflow-1.14.0-cp36-cp36m-linux_x86_64.whl"

ARG FRAMEWORK_SUPPORT_INSTALLABLE=sagemaker_tensorflow_training.tar.gz
ARG TENSORFLOW_WHL=tensorflow-2.0.0-cp36-cp36m-manylinux2010_x86_64.whl
ARG TF_URL=https://tensorflow-aws.s3-us-west-2.amazonaws.com/2.0/AmazonLinux/cpu/final/$TENSORFLOW_WHL

RUN apt-get update && apt-get install -y --no-install-recommends \
    python3-dev \
    python3-pip \
    python3-setuptools \
    software-properties-common \
    build-essential \
    openssh-client \
    openssh-server \
    ca-certificates \
    curl \
    git \
    wget \
    vim \
    zlib1g-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.1.tar.gz \
 && tar zxf openmpi-4.0.1.tar.gz \
 && cd openmpi-4.0.1 \
 && ./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:$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

WORKDIR /

COPY $FRAMEWORK_SUPPORT_INSTALLABLE .

RUN ${PIP} --no-cache-dir install --upgrade \
    pip \
    setuptools

# Some TF tools expect a "python" binary
RUN ln -s $(which ${PYTHON}) /usr/local/bin/python \
 && ln -s $(which ${PIP}) /usr/bin/pip

# Setup TF Wheel
RUN wget $TF_URL -O /tmp/$TENSORFLOW_WHL

# install PyYAML==5.1.2 to avoid conflict with latest awscli
# # python-dateutil==2.8.0 to satisfy botocore associated with latest awscli
RUN ${PIP} install --no-cache-dir -U \
    numpy==1.17.4 \
    scipy==1.2.2 \
    scikit-learn==0.22 \
    pandas==0.25.3 \
    Pillow==6.2.1 \
    h5py==2.10.0 \
    keras_applications==1.0.8 \
    keras_preprocessing==1.1.0 \
    keras==2.3.1 \
    python-dateutil==2.8.0 \
    PyYAML==5.1.2 \
    requests==2.22.0 \
    awscli==1.16.303 \
    mpi4py==3.0.3 \
    "sagemaker-tensorflow>=2.0,<2.1" \
    # Let's install TensorFlow separately in the end to avoid
    # the library version to be overwritten
    # ${PIP} install --no-cache-dir -U ${TF_URL} \
 && ${PIP} install --no-cache-dir -U \
    /tmp/$TENSORFLOW_WHL \
    horovod==0.18.2 \
 && rm -f /tmp/$TENSORFLOW_WHL \
 && ${PIP} install --no-cache-dir -U \
    $FRAMEWORK_SUPPORT_INSTALLABLE \
 && rm -f $FRAMEWORK_SUPPORT_INSTALLABLE 

COPY dockerd-entrypoint.py /usr/local/bin/dockerd-entrypoint.py
COPY deep_learning_container.py /usr/local/bin/deep_learning_container.py

RUN chmod +x /usr/local/bin/dockerd-entrypoint.py \
 && chmod +x /usr/local/bin/deep_learning_container.py

RUN curl https://aws-dlc-licenses.s3.amazonaws.com/tensorflow-2.0/license.txt -o /license.txt

ENTRYPOINT ["python", "/usr/local/bin/dockerd-entrypoint.py"]
CMD ["bin/bash"]