-
Notifications
You must be signed in to change notification settings - Fork 13
/
Dockerfile
91 lines (74 loc) · 3.36 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
# syntax=docker/dockerfile:1
FROM python:3.8-slim AS base
# Configure Python to print tracebacks on crash [1], and to not buffer stdout and stderr [2].
# [1] https://docs.python.org/3/using/cmdline.html#envvar-PYTHONFAULTHANDLER
# [2] https://docs.python.org/3/using/cmdline.html#envvar-PYTHONUNBUFFERED
ENV PYTHONFAULTHANDLER 1
ENV PYTHONUNBUFFERED 1
# Install Poetry.
ENV POETRY_VERSION 1.1.13
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/graphchain/ && touch src/graphchain/__init__.py && touch README.md && \
poetry install --no-dev --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 development Python environment.
RUN --mount=type=cache,target=/root/.cache/ \
poetry install --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 && \
zsh -c 'source ~/.zshrc'