docker meets python a look on the docker sdk for python · - docker sdk might be a better choice...

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: [email protected] 1 Docker meets Python – A look on the Docker SDK for Python “pip install docker” Jan Wagner Data Science Consultant

Upload: others

Post on 22-May-2020

50 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 1

Docker meets Python –

A look on the Docker SDK for Python

“pip install docker”

Jan Wagner

Data Science Consultant

Page 2: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 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

Page 3: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 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

Page 4: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 4

About me – Get in touch

[email protected]

https://twitter.com/wgnrjn

https://www.instagram.com/wgnrjn

https://github.com/wgnrjn

Page 5: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 5

Docker

Page 6: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 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

Page 7: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 7

Ways to communicate with it

GUI **

CLI * WebGUI ***

Page 8: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 8

Ways to communicate with it

* Docker CLI ($docker run hello-world)

https://docs.docker.com/engine/reference/commandline/cli/

GUI **

CLI * WebGUI ***

Page 9: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 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 ***

Page 10: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 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 ***

Page 11: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 11

GUI **

CLI * WebGUI ***

Page 12: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 12

GUI **

CLI * WebGUI ***

Docker SDK

Page 13: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 13

Get the Docker SDK for Python

• Officially available for Python and Golang

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

Page 14: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 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

Page 15: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 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 dockerclient = docker.from_env()print (client.containers.run('hello-world‘))

$ docker pull alpine import dockerclient = docker.from_env()image = client.images.pull("alpine")print (image.id)

Page 16: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 16

Hint!

Docker CLI Docker SDK – Python

$ docker image […]

$ docker images

import dockerclient = docker.from_env()client.images.[…]

$ docker container […] import dockerclient = docker.from_env()container = client.containers.[…]

https://docker-py.readthedocs.io/en/stable/client.htmlhttps://docs.docker.com/engine/reference/commandline/container/https://docs.docker.com/engine/reference/commandline/image/https://docs.docker.com/engine/reference/commandline/images/

Page 17: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 17

Ideas for Usecases

Page 18: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 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

Page 19: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 19

Using Python as Container-Starting-Script

import os

os.system ("Do-Stuff -parameterXYZ")

Page 20: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 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)

Page 21: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 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()

[...]

Page 22: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 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()

Page 23: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 23

pyTest with different Python Versions

Page 24: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 24

pyTest with different Python Versions

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

Page 25: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 25

Wrap Up

Page 26: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 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

Page 27: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 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

Page 28: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 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

Page 29: Docker meets Python A look on the Docker SDK for Python · - Docker SDK might be a better choice then „import os“ or equivalents Hope you now have: - An (high level) Idea about

accantec group | Alstertor 17 | 20095 Hamburg | Tel.: +49 (40) 67 59 59 0 | Fax.: +49 (40) 67 59 59 29 | www.accantec.de | Mail: [email protected] 29

Thank you!

---------------------

Questions?