docker meets python a look on the docker sdk for python
play

Docker meets Python A look on the Docker SDK for Python pip - PowerPoint PPT Presentation

Docker meets Python A look on the Docker SDK for Python pip install docker Jan Wagner Data Science Consultant accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de |


  1. Docker meets Python – A look on the Docker SDK for Python “pip install docker” Jan Wagner Data Science Consultant accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 1

  2. Agenda 1.Who am I? 2.The Docker Daemon/Service and Ways to communicate with it 3.Docker SDK for Python! a. Where and how to get it b. Code Examples 4.Ideas for Usecases a. Using Python as Container-Starting-Script … end extend it b. Get Container-Logs for further processing c. pyTest with different Python Versions 5.Wrap Up 6.Q&A accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 2

  3. About me – Hi, my name is Jan! • 32 Years old, Not married yet, but engaged • Data Science Consultant @ accantec consulting AG • We focus on Data – BI, Data Engineering & Data Science • https://accantec.de accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 3

  4. About me – Get in touch j.wagner@accantec.com https://twitter.com/wgnrjn https://www.instagram.com/wgnrjn https://github.com/wgnrjn accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 4

  5. Docker accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 5

  6. Your friendly house guest – The Whale! The Docker Daemon/Service: Reachable per default from within your System (localhost) ▪ Reachable per configuration from the outside ▪ accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 6

  7. Ways to communicate with it GUI ** CLI * WebGUI *** accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 7

  8. Ways to communicate with it * Docker CLI ($docker run hello-world) https://docs.docker.com/engine/reference/commandline/cli/ GUI ** CLI * WebGUI *** accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 8

  9. Ways to communicate with it * Docker CLI ($docker run hello-world) https://docs.docker.com/engine/reference/commandline/cli/ GUI ** ** Kitematic https://kitematic.com/ ** Docker Extension for VS Code CLI * WebGUI *** https://github.com/microsoft/vscode-docker accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 9

  10. Ways to communicate with it * Docker CLI ($docker run hello-world) https://docs.docker.com/engine/reference/commandline/cli/ GUI ** ** Kitematic https://kitematic.com/ ** Docker Extension for VS Code CLI * WebGUI *** https://github.com/microsoft/vscode-docker *** Portainer https://www.portainer.io/ accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 10

  11. GUI ** CLI * WebGUI *** accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 11

  12. GUI ** CLI * WebGUI *** Docker SDK accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 12

  13. Get the Docker SDK for Python Officially available for Python and Golang • https://docs.docker.com/develop/sdk/ accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 13

  14. Get the Docker SDK for Python Officially available for Python and Golang • https://docs.docker.com/develop/sdk/ pip/pip3 install docker • Package on conda-forge available too • https://anaconda.org/conda-forge/docker-py accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 14

  15. Code Examples Taken from https://docs.docker.com/develop/sdk/examples/ * Docker CLI Docker SDK – Python $ docker run hello-world import docker client = docker.from_env() print (client.containers.run('hello-world ‘ )) $ docker pull alpine import docker client = docker.from_env() image = client.images.pull("alpine") print (image.id) * Code Examples on Website are in Python 2 Syntax … I don ´ t know why ☺ accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 15

  16. Hint! Docker CLI Docker SDK – Python $ docker image […] import docker $ docker images client = docker.from_env() client.images .[…] $ docker container […] import docker client = docker.from_env() container = client.containers .[…] https://docker-py.readthedocs.io/en/stable/client.html https://docs.docker.com/engine/reference/commandline/container/ https://docs.docker.com/engine/reference/commandline/image/ https://docs.docker.com/engine/reference/commandline/images/ accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 16

  17. Ideas for Usecases accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 17

  18. Using Python as Container-Starting-Script https://hub.docker.com/_/postgres $ docker run – name test-db -e POSTGRES_PASSWORD=EuroPython -d postgres $ docker run --name test-db – e POSTGRES_PASSWORD=EuroPython -v /some/where/data:/var/lib/postgresql /data […….] postgres accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 18

  19. Using Python as Container-Starting-Script import os os.system ("Do-Stuff -parameterXYZ") accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 19

  20. Using Python as Container-Starting-Script import docker var1 = "..." var2 = ["...", "123"] var3 = "..." client = docker.from_env() client.containers.run('postgres', name=var1, environment=var2, mounts=var3, detach=True) accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 20

  21. Extend it to … [...] client = docker.from_env() myContainer = client.containers.get('myTestContainer') myContainer.stop() client.containers.prune() newImage = client.images.build('path/to/Dockerfile') newImageID = newImage.id() [...] accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 21

  22. Get Container-Logs import docker client = docker.from_env() container = client.containers.run('ubuntu', detach=True) f = open("myContainerLog", "w") for line in container.logs(stream=True): f.write(line) f.close() accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 22

  23. pyTest with different Python Versions accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 23

  24. pyTest with different Python Versions …. maybe just build a CI/CD Pipeline in this case …. accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 24

  25. Wrap Up accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 25

  26. Wrap Up Depending on your Task at Hand: - Python might be a better choice then e.g. bash Scripts - Docker SDK might be a better choice then „ import os “ or equivalents accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 26

  27. Wrap Up Depending on your Task at Hand: - Python might be a better choice then e.g. bash Scripts - Docker SDK might be a better choice then „ import os “ or equivalents Hope you now have: - An (high level) Idea about the Docker SDK for Python - Some inspiration for your Day-to-Day work with Docker and Python accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 27

  28. Wrap Up Depending on your Task at Hand: - Python might be a better choice then e.g. bash Scripts - Docker SDK might be a better choice then „ import os “ or equivalents Hope you now have: - An (high level) Idea about the Docker SDK for Python - Some inspiration for your Day-to-Day work with Docker and Python Last Hint: - Read the Docker Docs accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: info@accantec.de 28

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend