Save 37% off PRO during our Black Friday Sale! »

Writing Dockerfile for a Python project the right way

Writing Dockerfile for a Python project the right way

A short talk on best practices for Dockerfiles in terms of Python projects

200442040b9d038626d92d408f499642?s=128

Serge Matveenko

November 27, 2018
Tweet

Transcript

  1. Writing Dockerfile for a Python project the right way Serge

    Matveenko Twitter: @lig1 Github: @lig assaia.aero
  2. None
  3. Straightforward (good) FROM python:3.7 WORKDIR /usr/src/app COPY requirements.txt ./ RUN

    pip install --no-cache-dir -r requirements.txt COPY . . CMD [ "python", "./your-daemon-or-script.py" ]
  4. Straightforward (bad) FROM python:3 COPY . /usr/src/app/ RUN pip install

    --no-cache-dir -r requirements.txt WORKDIR /usr/src/app CMD python ./your-daemon-or-script.py
  5. Caching (obvious) FROM python:3.7 WORKDIR /usr/src/app COPY requirements.txt ./ RUN

    pip install --no-cache-dir -r requirements.txt COPY . . CMD [ "python", "./your-daemon-or-script.py" ] FROM python:3 COPY . /usr/src/app/ RUN pip install --no-cache-dir -r requirements.txt WORKDIR /usr/src/app CMD python ./your-daemon-or-script.py
  6. Caching (non-obvious) FROM python:3.7 WORKDIR /usr/src/app COPY requirements.txt ./ RUN

    pip install --no-cache-dir -r requirements.txt COPY . . CMD [ "python", "./your-daemon-or-script.py" ] FROM python:3 COPY . /usr/src/app/ RUN pip install --no-cache-dir -r requirements.txt WORKDIR /usr/src/app CMD python ./your-daemon-or-script.py
  7. Python version FROM python:3.7 WORKDIR /usr/src/app COPY requirements.txt ./ RUN

    pip install --no-cache-dir -r requirements.txt COPY . . CMD [ "python", "./your-daemon-or-script.py" ] FROM python:3 COPY . /usr/src/app/ RUN pip install --no-cache-dir -r requirements.txt WORKDIR /usr/src/app CMD python ./your-daemon-or-script.py
  8. Python version (even better) FROM python:3.7-stretch WORKDIR /usr/src/app COPY requirements.txt

    ./ RUN pip install --no-cache-dir -r requirements.txt COPY . . CMD [ "python", "./your-daemon-or-script.py" ] FROM python:3 COPY . /usr/src/app/ RUN pip install --no-cache-dir -r requirements.txt WORKDIR /usr/src/app CMD python ./your-daemon-or-script.py
  9. CMD FROM python:3.7 WORKDIR /usr/src/app COPY requirements.txt ./ RUN pip

    install --no-cache-dir -r requirements.txt COPY . . CMD [ "python", "./your-daemon-or-script.py" ] FROM python:3 COPY . /usr/src/app/ RUN pip install --no-cache-dir -r requirements.txt WORKDIR /usr/src/app CMD python ./your-daemon-or-script.py
  10. CMD (now you see me…) FROM python:3.7 WORKDIR /usr/src/app COPY

    requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY . . ENTRYPOINT [ "python", "./your-daemon-or-script.py" ] CMD [ "run" ] FROM python:3 COPY . /usr/src/app/ RUN pip install --no-cache-dir -r requirements.txt WORKDIR /usr/src/app ENTRYPOINT python ./your-daemon-or-script.py CMD run
  11. That was too easy

  12. What if I want to build from …?

  13. FROM nvidia/cuda:9.0-cudnn7-runtime-ubuntu16.04 # :) FROM nvidia/cuda:9.0-cudnn7-runtime-ubuntu16.04 RUN apt-get update &&

    apt-get install -y lib… lib… ENV PYTHON_VERSION=3.6.6 RUN curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash && \ pyenv install ${PYTHON_VERSION} && \ pyenv global ${PYTHON_VERSION} && \ curl -L https://raw.githubusercontent.com/kennethreitz/pipenv/master/get-pipenv.py | python && \ pipenv --python python COPY ./Pipfile ./Pipfile.lock ./ RUN pipenv install --deploy --dev
  14. Two different Tensorflows??? FROM nvidia/cuda:9.0-cudnn7-runtime-ubuntu16.04 AS base COPY ./Pipfile ./Pipfile.lock

    ./ RUN pipenv install --deploy --dev CMD ["pipenv", "run", "python", "-m", "my.worker"] FROM base AS base-gpu RUN pipenv run pip install tensorflow-gpu==$(pipenv run pip show tensorflow | grep '^Version' | awk '{print $2}') FROM base as app COPY . . FROM base-gpu as gpu COPY . .
  15. Questions? Serge Matveenko Twitter: @lig1 Github: @lig Telegram: @SergeMatveenko assaia.aero