-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
104 lines (84 loc) · 4.09 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
101
102
103
104
# syntax=docker/dockerfile:1
ARG PYTHON_VERSION=3.8
FROM python:$PYTHON_VERSION-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.3.1
RUN --mount=type=cache,target=/root/.cache/pip/ \
pip install poetry~=$POETRY_VERSION
# 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
# Create a non-root user and switch to it [1].
# [1] https://code.visualstudio.com/remote/advancedcontainers/add-nonroot-user
ARG UID=1000
ARG GID=$UID
RUN groupadd --gid $GID user && \
useradd --create-home --gid $GID --uid $UID user && \
chown user /opt/
USER user
# Create and activate a virtual environment.
RUN python -m venv /opt/ksef-env
ENV PATH /opt/ksef-env/bin:$PATH
ENV VIRTUAL_ENV /opt/ksef-env
# Set the working directory.
WORKDIR /workspaces/ksef/
# Install the run time Python dependencies in the virtual environment.
COPY --chown=user:user poetry.lock* pyproject.toml /workspaces/ksef/
RUN mkdir -p /home/user/.cache/pypoetry/ && mkdir -p /home/user/.config/pypoetry/ && \
mkdir -p src/ksef/ && touch src/ksef/__init__.py && touch README.md
RUN --mount=type=cache,uid=$UID,gid=$GID,target=/home/user/.cache/pypoetry/ \
poetry install --only main --no-interaction
FROM base as ci
# Allow CI to run as root.
USER root
# 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/CD Python dependencies in the virtual environment.
RUN --mount=type=cache,target=/root/.cache/pypoetry/ \
poetry install --only main,test --no-interaction
FROM base as dev
# Install development tools: compilers, curl, git, gpg, ssh, starship, sudo, vim, and zsh.
USER root
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 && \
sh -c "$(curl -fsSL https://starship.rs/install.sh)" -- "--yes" && \
usermod --shell /usr/bin/zsh user && \
echo 'user ALL=(root) NOPASSWD:ALL' > /etc/sudoers.d/user && chmod 0440 /etc/sudoers.d/user
USER user
# Install the development Python dependencies in the virtual environment.
RUN --mount=type=cache,uid=$UID,gid=$GID,target=/home/user/.cache/pypoetry/ \
poetry install --no-interaction
# Persist output generated during docker build so that we can restore it in the dev container.
COPY --chown=user:user .pre-commit-config.yaml /workspaces/ksef/
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/
# Configure the non-root user's shell.
ENV ANTIDOTE_VERSION 1.8.1
RUN git clone --branch v$ANTIDOTE_VERSION --depth=1 https://github.com/mattmc3/antidote.git ~/.antidote/ && \
echo 'zsh-users/zsh-syntax-highlighting' >> ~/.zsh_plugins.txt && \
echo 'zsh-users/zsh-autosuggestions' >> ~/.zsh_plugins.txt && \
echo 'source ~/.antidote/antidote.zsh' >> ~/.zshrc && \
echo 'antidote load' >> ~/.zshrc && \
echo 'eval "$(starship init zsh)"' >> ~/.zshrc && \
echo 'HISTFILE=~/.history/.zsh_history' >> ~/.zshrc && \
echo 'HISTSIZE=1000' >> ~/.zshrc && \
echo 'SAVEHIST=1000' >> ~/.zshrc && \
echo 'setopt share_history' >> ~/.zshrc && \
echo 'bindkey "^[[A" history-beginning-search-backward' >> ~/.zshrc && \
echo 'bindkey "^[[B" history-beginning-search-forward' >> ~/.zshrc && \
mkdir ~/.history/ && \
zsh -c 'source ~/.zshrc'