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
SMART_READER_LITE
LIVE PREVIEW

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 |


slide-1
SLIDE 1

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

Docker meets Python – A look on the Docker SDK for Python

“pip install docker”

Jan Wagner Data Science Consultant

slide-2
SLIDE 2

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

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

slide-3
SLIDE 3

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

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
slide-4
SLIDE 4

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

About me – Get in touch

j.wagner@accantec.com https://twitter.com/wgnrjn https://www.instagram.com/wgnrjn https://github.com/wgnrjn

slide-5
SLIDE 5

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

Docker

slide-6
SLIDE 6

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

Your friendly house guest – The Whale!

The Docker Daemon/Service: ▪ Reachable per default from within your System (localhost) ▪ Reachable per configuration from the outside

slide-7
SLIDE 7

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

Ways to communicate with it

GUI ** CLI * WebGUI ***

slide-8
SLIDE 8

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

Ways to communicate with it

* Docker CLI ($docker run hello-world) https://docs.docker.com/engine/reference/commandline/cli/

GUI ** CLI * WebGUI ***

slide-9
SLIDE 9

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

Ways to communicate with it

* Docker CLI ($docker run hello-world) https://docs.docker.com/engine/reference/commandline/cli/ ** Kitematic https://kitematic.com/ ** Docker Extension for VS Code https://github.com/microsoft/vscode-docker

GUI ** CLI * WebGUI ***

slide-10
SLIDE 10

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

Ways to communicate with it

* Docker CLI ($docker run hello-world) https://docs.docker.com/engine/reference/commandline/cli/ ** Kitematic https://kitematic.com/ ** Docker Extension for VS Code https://github.com/microsoft/vscode-docker *** Portainer https://www.portainer.io/

GUI ** CLI * WebGUI ***

slide-11
SLIDE 11

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

GUI ** CLI * WebGUI ***

slide-12
SLIDE 12

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

GUI ** CLI * WebGUI *** Docker SDK

slide-13
SLIDE 13

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

Get the Docker SDK for Python

  • Officially available for Python and Golang

https://docs.docker.com/develop/sdk/

slide-14
SLIDE 14

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

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

slide-15
SLIDE 15

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

Code Examples

Taken from https://docs.docker.com/develop/sdk/examples/ * * Code Examples on Website are in Python 2 Syntax … I don´t know why ☺

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)

slide-16
SLIDE 16

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

Hint!

Docker CLI Docker SDK – Python $ docker image […] $ docker images

import docker 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/

slide-17
SLIDE 17

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

Ideas for Usecases

slide-18
SLIDE 18

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

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

slide-19
SLIDE 19

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

Using Python as Container-Starting-Script

import os

  • s.system ("Do-Stuff -parameterXYZ")
slide-20
SLIDE 20

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

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)

slide-21
SLIDE 21

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

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()

[...]

slide-22
SLIDE 22

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

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()

slide-23
SLIDE 23

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

pyTest with different Python Versions

slide-24
SLIDE 24

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

pyTest with different Python Versions

…. maybe just build a CI/CD Pipeline in this case ….

slide-25
SLIDE 25

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

Wrap Up

slide-26
SLIDE 26

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

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
slide-27
SLIDE 27

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

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
slide-28
SLIDE 28

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

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
slide-29
SLIDE 29

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 29

Thank you!

  • Questions?