-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
100 lines (79 loc) · 3.75 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# syntax=docker/dockerfile:1
ARG PYTHON_VERSION=3.8
FROM python:3.8.13-slim AS base
# Install Poetry.
ENV POETRY_VERSION 1.2.0
RUN --mount=type=cache,target=/root/.cache/ \
pip install poetry~=$POETRY_VERSION
# Create and activate a virtual environment.
RUN python -m venv /opt/app-env
ENV PATH /opt/app-env/bin:$PATH
ENV VIRTUAL_ENV /opt/app-env
# Install compilers that may be required for certain packages or platforms.
RUN rm /etc/apt/apt.conf.d/docker-clean
RUN --mount=type=cache,target=/var/cache/apt/ \
--mount=type=cache,target=/var/lib/apt/ \
apt-get update && \
apt-get install --no-install-recommends --yes build-essential
# Set the working directory.
WORKDIR /app/
# Install the run time Python environment.
COPY poetry.lock* pyproject.toml /app/
RUN --mount=type=cache,target=/root/.cache/ \
mkdir -p src/mlops_demo/ && touch src/mlops_demo/__init__.py && touch README.md && \
poetry install --only main --no-interaction
# Create a non-root user.
ARG UID=1000
ARG GID=$UID
RUN groupadd --gid $GID app && \
useradd --create-home --gid $GID --uid $UID app
FROM base as ci
# Install git so we can run pre-commit.
RUN --mount=type=cache,target=/var/cache/apt/ \
--mount=type=cache,target=/var/lib/apt/ \
apt-get update && \
apt-get install --no-install-recommends --yes git
# Install the CI Python environment.
RUN --mount=type=cache,target=/root/.cache/ \
poetry install --only main,test --no-interaction
# Give the non-root user ownership and switch to the non-root user.
RUN chown --recursive app /app/ /opt/
USER app
FROM base as dev
# Install development tools: compilers, curl, git, gpg, ssh, starship, sudo, vim, and zsh.
RUN --mount=type=cache,target=/var/cache/apt/ \
--mount=type=cache,target=/var/lib/apt/ \
apt-get update && \
apt-get install --no-install-recommends --yes build-essential curl git gnupg ssh sudo vim zsh zsh-antigen && \
sh -c "$(curl -fsSL https://starship.rs/install.sh)" -- "--yes" && \
usermod --shell /usr/bin/zsh app
# Install the development Python environment.
RUN --mount=type=cache,target=/root/.cache/ \
poetry install --no-interaction
# Persist output generated during docker build so that we can restore it in the dev container.
COPY .pre-commit-config.yaml /app/
RUN mkdir -p /opt/build/poetry/ && cp poetry.lock /opt/build/poetry/ && \
git init && pre-commit install --install-hooks && \
mkdir -p /opt/build/git/ && cp .git/hooks/commit-msg .git/hooks/pre-commit /opt/build/git/
# Give the non-root user ownership and switch to the non-root user.
RUN chown --recursive app /app/ /opt/ && \
echo 'app ALL=(root) NOPASSWD:ALL' > /etc/sudoers.d/app && \
chmod 0440 /etc/sudoers.d/app
USER app
# Configure the non-root user's shell.
RUN echo 'source /usr/share/zsh-antigen/antigen.zsh' >> ~/.zshrc && \
echo 'antigen bundle zsh-users/zsh-syntax-highlighting' >> ~/.zshrc && \
echo 'antigen bundle zsh-users/zsh-autosuggestions' >> ~/.zshrc && \
echo 'antigen apply' >> ~/.zshrc && \
echo 'eval "$(starship init zsh)"' >> ~/.zshrc && \
echo 'HISTFILE=~/.zsh_history' >> ~/.zshrc && \
echo 'HISTSIZE=1000' >> ~/.zshrc && \
echo 'SAVEHIST=1000' >> ~/.zshrc && \
zsh -c 'source ~/.zshrc'
# Install Docker CE CLI
RUN sudo apt-get update \
&& sudo apt-get install -y apt-transport-https ca-certificates curl gnupg2 lsb-release \
&& curl -fsSL https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/gpg | sudo apt-key add - 2>/dev/null \
&& echo "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]') $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list \
&& sudo apt-get update \
&& sudo apt-get install -y docker-ce-cli