-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
153 lines (107 loc) · 3.69 KB
/
Dockerfile
File metadata and controls
153 lines (107 loc) · 3.69 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# See
# Document docker poetry best practices
# https://github.com/python-poetry/poetry/discussions/1879
# `python-base` sets up all our shared environment variables
FROM python:3.11.1-slim as python-base
# slim => sh and no bash
#
# python
#
# python output is sent straight to terminal without being first buffered
ENV PYTHONUNBUFFERED=1
# prevents python creating .pyc files
ENV PYTHONDONTWRITEBYTECODE=1
#
# pip
#
# keep docker image as small as possible
ENV PIP_NO_CACHE_DIR=off
# suppress pip upgrade warning
ENV PIP_DISABLE_PIP_VERSION_CHECK=on
# mitigate ReadTimeoutError
ENV PIP_DEFAULT_TIMEOUT=100
#
# poetry
# see:
# https://python-poetry.org/docs/configuration/
#
ENV POETRY_VERSION=1.3.0
ENV POETRY_HOME="/opt/poetry"
# the virtualenv will be created and expected in a folder named .venv
# within the root directory of the project.
ENV POETRY_VIRTUALENVS_IN_PROJECT=true
# do not ask any interactive question
ENV POETRY_NO_INTERACTION=1
#
# bespoke
#
# this is where our requirements + virtual environment will live
ENV PYSETUP_PATH="/opt/pysetup"
ENV VENV_PATH="/opt/pysetup/.venv"
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"
################################################################################
################################################################################
# `builder-base` stage is used to build deps + create our virtual environment
FROM python-base as builder-base
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
# deps for installing poetry
curl \
# deps for building python deps
build-essential
# install poetry - respects $POETRY_VERSION & $POETRY_HOME
RUN curl -sSL https://install.python-poetry.org | python3 -
# copy project requirement files here to ensure they will be cached.
WORKDIR $PYSETUP_PATH
COPY poetry.lock pyproject.toml ./
# install runtime deps - uses $POETRY_VIRTUALENVS_IN_PROJECT internally
RUN poetry install --no-root --no-dev
################################################################################
################################################################################
# docker build --target development --tag image:tag .
# `development` image is used during test
FROM python-base as test
ENV FASTAPI_ENV=development
WORKDIR $PYSETUP_PATH
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
# vi for dev editing
vim \
tree
# copy in our built poetry + venv
COPY --from=builder-base $POETRY_HOME $POETRY_HOME
COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH
# quicker install as runtime deps are already installed
RUN poetry install --no-root
COPY . /opt/src
WORKDIR /opt/src
ENV PYTHONPATH="/opt/src"
RUN echo 'alias ls="ls -aFC --color"' >> ~/.bashrc
CMD sleep 86400
################################################################################
################################################################################
# `development` image is used during development
FROM python-base as development
ENV FASTAPI_ENV=development
WORKDIR $PYSETUP_PATH
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
# vi for dev editing
vim \
tree \
# ping, nc, ifconfig for network sleuthing
iputils-ping \
netcat \
net-tools
# copy in our built poetry + venv
COPY --from=builder-base $POETRY_HOME $POETRY_HOME
COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH
# quicker install as runtime deps are already installed
RUN poetry install --no-root
COPY . /opt/src
WORKDIR /opt/src
ENV PYTHONPATH="/opt/src"
RUN echo 'alias ls="ls -aFC --color"' >> ~/.bashrc
EXPOSE 8000
# CMD sleep 86400
CMD uvicorn --host 0.0.0.0 --reload --log-level debug main:app