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

Getting started with docker + python

Getting started with docker + python

Introduction to Docker and showing how well it works with Python app deployments. Docker examples in https://github.com/a-y-khan/python_docker_examples.

Ayla Khan

May 31, 2018
Tweet

More Decks by Ayla Khan

Other Decks in Technology

Transcript

  1. Docker vs VMs Host OS Hypervisor VM Guest OS ...

    VM Guest OS Host OS Base OS ... Base OS
  2. Docker Run • docker run -i -t ubuntu:16.04 ◦ interactive

    example ▪ STDIN open (-i) ▪ pseudo-tty (-t) ◦ Dockerfile command: CMD [“/bin/bash”]
  3. Docker Run • docker run -it python:3.6 ◦ pulls image

    from docker repository if needed ◦ run interactively ◦ can also use image ID
  4. Docker State • Docker images, containers stateless ◦ unless configured

    otherwise ◦ ephemeral storage • Volume or bind mounting from host OS ◦ persistent storage ◦ separate stateless code from stateful
  5. Docker Run With Script • docker run --volume $PWD:/usr/app \

    --workdir /usr/app python:3.6 \ python3 hello_world.py ◦ hello_world.py in current working directory
  6. Docker Build • docker build --tag name:tag PATH | URL

    ◦ Dockerfile to image ◦ Build context (PATH | URL) ▪ build source ▪ get files from context ▪ size matters! ◦ .dockerignore file
  7. Docker CLI Cleanup Commands • docker stop • docker kill

    • docker rmi ◦ remove images • docker container prune ◦ remove stopped containers
  8. Other Docker CLI Commands • docker ps • docker exec

    • docker history ◦ intermediate images (layers) • docker inspect ◦ container or image metadata https://docs.docker.com/engine/reference/commandline/docker/
  9. simple_python_docker: python 3 • Build image with Python 3.6 base

    • docker build -t python3 . ◦ builds python3:latest • docker run python3:latest
  10. simple_python_docker: python 2 • Build image with Python 2.7 base

    • docker build -f Dockerfile.python_27 \ -t python2:demo . ◦ builds python2:demo • docker run python2:demo
  11. flask_docker • docker build -t app:1.0 . • docker run

    \ --publish 5000:5000 \ --name my_app \ --rm \ app:1.0 ◦ Detached mode: --detach
  12. flask_docker_logs • docker build -t app:2.0 . • docker run

    \ --publish 5000:5000 \ --name my_app --rm \ --volume $PWD/logs:/usr/app/logs \ app:2.0
  13. ENTRYPOINT vs CMD • ENTRYPOINT ◦ Package specific executable •

    CMD ◦ Set default parameters, flags ◦ More flexible for executable setting
  14. flask_multistage_docker-compose • Group image builds in single Dockerfile • Copy

    artifacts between stages https://docs.docker.com/develop/develop-images/multistage-build