dockerfile.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. DOCKERFILE_TEMPLATE = """
  2. # ----- stage 1: build -----
  3. FROM {py_build_image} as build
  4. # requirements section depends on pip vs conda, and presence of buildx
  5. ENV PIP_PROGRESS_BAR off
  6. {requirements_section}
  7. # ----- stage 2: base -----
  8. {base_setup}
  9. COPY --from=build /env /env
  10. ENV PATH="/env/bin:$PATH"
  11. ENV SHELL /bin/bash
  12. # some resources (eg sagemaker) must run on root
  13. {user_setup}
  14. WORKDIR {workdir}
  15. RUN chown -R {uid} {workdir}
  16. # make artifacts cache dir unrelated to build
  17. RUN mkdir -p {workdir}/.cache && chown -R {uid} {workdir}/.cache
  18. # copy code/etc
  19. COPY --chown={uid} src/ {workdir}
  20. ENV PYTHONUNBUFFERED=1
  21. {entrypoint_section}
  22. """
  23. # this goes into base_setup in TEMPLATE
  24. PYTHON_SETUP_TEMPLATE = """
  25. FROM {py_base_image} as base
  26. """
  27. # this goes into base_setup in TEMPLATE
  28. ACCELERATOR_SETUP_TEMPLATE = """
  29. FROM {accelerator_base_image} as base
  30. # make non-interactive so build doesn't block on questions
  31. ENV DEBIAN_FRONTEND=noninteractive
  32. # install python
  33. RUN apt-get update -qq && apt-get install --no-install-recommends -y \
  34. {python_packages} \
  35. && apt-get -qq purge && apt-get -qq clean \
  36. && rm -rf /var/lib/apt/lists/*
  37. # make sure `python` points at the right version
  38. RUN update-alternatives --install /usr/bin/python python /usr/bin/python{py_version} 1 \
  39. && update-alternatives --install /usr/local/bin/python python /usr/bin/python{py_version} 1
  40. """
  41. # this goes into requirements_section in TEMPLATE
  42. PIP_TEMPLATE = """
  43. RUN python -m venv /env
  44. # make sure we install into the env
  45. ENV PATH="/env/bin:$PATH"
  46. COPY {requirements_files} ./
  47. {buildx_optional_prefix} {pip_install}
  48. """
  49. # this goes into requirements_section in TEMPLATE
  50. CONDA_TEMPLATE = """
  51. COPY src/environment.yml .
  52. {buildx_optional_prefix} conda env create -f environment.yml -n env
  53. # pack the environment so that we can transfer to the base image
  54. RUN conda install -c conda-forge conda-pack
  55. RUN conda pack -n env -o /tmp/env.tar && \
  56. mkdir /env && cd /env && tar xf /tmp/env.tar && \
  57. rm /tmp/env.tar
  58. RUN /env/bin/conda-unpack
  59. """
  60. USER_CREATE_TEMPLATE = """
  61. RUN useradd \
  62. --create-home \
  63. --no-log-init \
  64. --shell /bin/bash \
  65. --gid 0 \
  66. --uid {uid} \
  67. {user} || echo ""
  68. """
  69. ENTRYPOINT_TEMPLATE = """
  70. ENTRYPOINT {entrypoint}
  71. """