11# Build arguments
2- ARG PYTHON_VERSION=3.9
2+ ARG PYTHON_VERSION=3.10-slim
33
4- # Start from the official Python base image on version: 3.9
5- # First stage, build the application with the dependencies that
6- # managed by Poetry.
7- FROM python:${PYTHON_VERSION} as requirements-stage
8-
9- # Set python virtual environment
10- ENV VIRTUAL_ENV=/opt/venv
11- RUN python -m venv $VIRTUAL_ENV
12- ENV PATH="$VIRTUAL_ENV/bin:$PATH"
13-
14- # Set /tmp as the current working directory. Here's where we will
15- # generate the file requirements.txt.
16- WORKDIR /tmp
17-
18- # Update pip and install Poetry in this Docker stage.
19- RUN pip install --upgrade pip && \
20- pip install poetry
21-
22- # Copy the pyproject.toml and poetry.lock files to the /tmp directory.
23- # Because it uses ./poetry.lock* (ending with a *), it won't crash if
24- # that file is not available yet.
25- ADD ./pyproject.toml ./poetry.lock* /tmp/
26-
27- # Generate the requirements.txt file.
28- RUN poetry export -f requirements.txt --output requirements.txt --without-hashes
29-
30- # This is the final stage, anything here will be preserved in the final
31- # container image.
324FROM python:${PYTHON_VERSION}
335
34- # Create the app user
35- RUN addgroup --system app && adduser --no-create-home --system --group app
36-
37- # Set python virtual environment
38- ENV VIRTUAL_ENV=/opt/venv
39- RUN python -m venv $VIRTUAL_ENV
40- ENV PATH="$VIRTUAL_ENV/bin:$PATH"
41-
42- # Set the current working directory to /code. This is where
43- # we'll put the code for our application.
44- WORKDIR /code
45-
6+ # Install virtualenv and create a virtual environment
7+ RUN pip install --no-cache-dir -U virtualenv>=20.13.1 && virtualenv /env --python=python3.10
8+ ENV PATH /env/bin:$PATH
9+
10+ # Install pip requirements
11+ WORKDIR /app
12+ COPY . .
13+ RUN /env/bin/pip install --no-cache-dir . && \
14+ rm nginx.conf
15+
16+ # Install nginx and copy configuration
17+ RUN apt-get update && apt-get install -y --no-install-recommends nginx curl \
18+ && apt-get clean \
19+ && rm -rf /var/lib/apt/lists/* \
20+ && rm /etc/nginx/sites-enabled/default \
21+ && mkdir -p /var/log/django \
22+ && touch /var/log/django/basedosdados_api.log \
23+ && chown www-data:www-data /var/log/django/basedosdados_api.log
24+ COPY nginx.conf /etc/nginx/nginx.conf
4625# https://docs.python.org/3/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE
4726# Prevents Python from writing .pyc files to disc
4827ENV PYTHONDONTWRITEBYTECODE 1
@@ -52,25 +31,11 @@ ENV PYTHONDONTWRITEBYTECODE 1
5231# in real time. Equivalent to python -u: https://docs.python.org/3/using/cmdline.html#cmdoption-u
5332ENV PYTHONUNBUFFERED 1
5433
55- # Copy the requirements.txt file from the requirements-stage to the
56- # /code directory.
57- COPY --from=requirements-stage /tmp/requirements.txt /code/requirements.txt
58-
59- # Upgrade pip and install the dependencies from the requirements.txt file.
60- RUN pip install --upgrade pip && \
61- pip install --no-cache-dir --upgrade -r /code/requirements.txt
62-
63- # Copy the api directory to the /code directory.
64- ADD /app /code/app
65- ADD run.sh /code/run.sh
66- RUN chmod +x /code/run.sh
67-
68- # chown all the files to the app user
69- RUN chown -R app:app /code
70-
71- # Change to the app user
72- USER app
34+ # Copy app, generate static and set permissions
35+ RUN /env/bin/python manage.py collectstatic --no-input --settings=basedosdados_api.settings.base && \
36+ chown -R www-data:www-data /app
7337
74- # Run the uvicorn command, telling it to use the app object imported
75- # from api.main.
76- CMD ["sh" , "/code/run.sh" ]
38+ # Expose and run app
39+ EXPOSE 80
40+ STOPSIGNAL SIGKILL
41+ CMD ["/app/start-server.sh" ]
0 commit comments