Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Serge Matveenko

November 27, 2018
Tweet

More Decks by Serge Matveenko

Other Decks in Technology

Transcript

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

    Matveenko Twitter: @lig1 Github: @lig assaia.aero
  2. 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" ]
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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 . .