Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django dockerfile multistage

# Base off the official python image
# Define a common stage for dev and prod images called base
FROM python:3.10 as base
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Create a user to avoid running containers as root in production
RUN addgroup --system web 
    && adduser --system --ingroup web web
# Install os-level dependencies (as root)
RUN apt-get update && apt-get install -y -q --no-install-recommends 
  # dependencies for building Python packages
  build-essential 
  # postgress client (psycopg2) dependencies
  libpq-dev 
  # cleaning up unused files to reduce the image size
  && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false 
  && rm -rf /var/lib/apt/lists/*
# Switch to the non-root user
USER web
# Create a directory for the source code and use it as base path
WORKDIR /home/web/code/
# Copy the python depencencies list for pip
COPY --chown=web:web ./requirements/base.txt requirements/base.txt
# Switch to the root user temporary, to grant execution permissions.
USER root
# Install python packages at system level
RUN pip install --no-cache-dir -r requirements/base.txt
# Copy entrypoint script which waits for the db to be ready
COPY --chown=web:web ./docker/app/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
USER web
# This script will run before every command executed in the container
ENTRYPOINT ["entrypoint.sh"]


# Define an image for local development. Inherits common packages from the base stage.
FROM base as dev
# Copy the scripts that starts the development application server (runserver)
COPY --chown=web:web ./docker/app/start-dev-server.sh /usr/local/bin/start-dev-server.sh
USER root
RUN chmod +x /usr/local/bin/start-dev-server.sh
USER web
# The development server starts by default when the container starts
CMD ["start-dev-server.sh"]


# Define an image for production. Inherits common packages from the base stage.
FROM base as prod
# Install extra packages required in production
USER root
COPY --chown=web:web ./requirements/prod.txt requirements/prod.txt
RUN pip install --no-cache-dir -r requirements/prod.txt
# Copy the script that starts the production application server (gunicorn)
COPY --chown=web:web ./docker/app/start-prod-server.sh /usr/local/bin/start-prod-server.sh
RUN chmod +x /usr/local/bin/start-prod-server.sh
USER web
# Copy the source code of our django app to the working directoy
COPY --chown=web:web . ./
# The production server starts by default when the container starts
CMD ["start-prod-server.sh"]
Comment

PREVIOUS NEXT
Code Example
Python :: is python platform independent 
Python :: split string by spaces python 
Python :: filter query objects by date range in Django? 
Python :: crear una clase en python 
Python :: create a blank image 
Python :: instance variable in python 
Python :: pandas add quantile columns 
Python :: load json py 
Python :: how to find if the numpy array contains negative values 
Python :: Converting Hex to RGB value in Python 
Python :: how to sort a dictionary using pprint module in python 
Python :: python - remove floating in a dataframe 
Python :: arrange array in ascending order python 
Python :: version python 
Python :: time.time() 
Python :: python multiaxis slicing 
Python :: scaling data 
Python :: python split word into letter pairs 
Python :: append dataframe pandas 
Python :: how do i turn a tensor into a numpy array 
Python :: remove tuple from list python 
Python :: pyqt open file dialog 
Python :: python slice a dict 
Python :: kivymd window size 
Python :: python round to two decimals 
Python :: get the current date and time in python 
Python :: fork function in python 
Python :: python order by date 
Python :: How to wait a page is loaded in Python Selenium 
Python :: sqlite3 python 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =