Skip to content

Instantly share code, notes, and snippets.

@praveenweb
Created February 26, 2018 10:02
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save praveenweb/9cabe480c1811b85337de988e43b8633 to your computer and use it in GitHub Desktop.
Save praveenweb/9cabe480c1811b85337de988e43b8633 to your computer and use it in GitHub Desktop.
# ---- Base python ----
FROM python:3.6 AS base
# Create app directory
WORKDIR /app
# ---- Dependencies ----
FROM base AS dependencies
COPY gunicorn_app/requirements.txt ./
# install app dependencies
RUN pip install -r requirements.txt
# ---- Copy Files/Build ----
FROM dependencies AS build
WORKDIR /app
COPY . /app
# Build / Compile if required
# --- Release with Alpine ----
FROM python:3.6-alpine3.7 AS release
# Create app directory
WORKDIR /app
COPY --from=dependencies /app/requirements.txt ./
COPY --from=dependencies /root/.cache /root/.cache
# Install app dependencies
RUN pip install -r requirements.txt
COPY --from=build /app/ ./
CMD ["gunicorn", "--config", "./gunicorn_app/conf/gunicorn_config.py", "gunicorn_app:app"]
@rohangeorge91
Copy link

rohangeorge91 commented May 2, 2021

Brilliant. You can make the build even lighter by pip the dependencies to a target location and then including them in the pythonpath.

FROM python:3.8.9-alpine3.13 as pythonBuilder
WORKDIR /home/root/server
# any dependencies in python which requires a compiled c/c++ code (if any)
RUN apk update && apk add --update gcc libc-dev linux-headers libusb-dev
COPY ./local-project-folder .
RUN pip3 install --target=/home/root/server/dependencies -r requirements.txt

FROM python:3.8.9-alpine3.13
WORKDIR /home/root/server
# include runtime libraries (if any)
RUN apk update && apk add libusb-dev
COPY --from=pythonBuilder	/home/root/server .
ENV PYTHONPATH="${PYTHONPATH}:/home/root/server/dependencies"
CMD "./server.py"

@vishalgoel98
Copy link

Hi @rohangeorge91 not able to run server.py in next layer of docker file. everytime i need to run requirment.txt in second layer also.

@rohangeorge91
Copy link

that's was the reason I gave the pip3 install steps we give a specific path to copy the dependencies and also include them in the python path so that it will scan for them from that directory. Can you share your dockerfile as a Git gist? also to avoid bothering praveen.

@PaulisMatrix
Copy link

@praveenweb, I have one question. What's the use of dependencies layer since you are installing the packages in the release itself?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment