slides:
thisisreal.net/talks/20190709_boulder_python/
¶notebook:
https://github.com/whlteXbread/talk_notebooks/
¶andrew catellier
docker is a way to build infrastructure using code
imagine setting up your dev/prod environment with one command.
imagine a shell script to set up your dev/prod environment but without the pain.
imagine not worrying about dependencies.
then take about one step back from there.
there are lots of good python/docker tutorials out there! definitely check them out.
but here's a crash course that covers some of the subtleties that aren't always covered in the tutorials.
speak up if you see something that doesn't make sense!
$> docker run -t your_app
----------------------
generating profit...
done!!!1
----------------------
baby's first dockerfile
# Dockerfile---image name: dd
FROM alpine:3.10
RUN apk add sl
CMD /bin/sh
a devops poem in three layers
(it's a haiku if you say it right!)
layer 1: import a "base image"—in this case a tiny linux
FROM alpine:3.10
layer 2: install a system-level package in our little container
RUN apk add sl
layer 3: run a command after everything else is set up, a.k.a. entrypoint
CMD /bin/sh
build the docker "image" and tag it with the name dd
docker build -t dd .
run the docker "image" by specifying the tag name—launches a shell!
docker run -it dd
specifying -i
tells docker to let you interact via the terminal
check it out.
baby's second dockerfile, hello cruel world
# Dockerfile---image name: py_hello_world
FROM python:3.7.3-alpine3.10
CMD python -c "print('Hello World')"
import a "base image"
FROM python:3.7.3-alpine3.10
run a command after everything else is set up
CMD python -c "print('Hello World')"
build the docker "image" and tag it with the name py_hello_world
docker build -t py_hello_world .
run the docker "image" by specifying the tag name. python prints Hello World
to the terminal
docker run -t py_hello_world
check it out.
i thought you'd never ask! here's a dockerfile:
# Dockerfile---image name: py_ezpz
FROM python:3-alpine3.10
WORKDIR /app
COPY app /app
RUN pip install -r requirements.txt
COPY content /content
CMD ["python", "/app/analyze.py"]
you can pick from many different images (debian, alpine, windows) with python already installed
FROM python:3-alpine3.10
specify a directory to start in and then copy our code into the image
WORKDIR /app
COPY app /app
oh snap
RUN pip install -r requirements.txt
copy some more things into the image
COPY content /content
set our entrypoint.
CMD ["python", "/app/analyze.py"]
/app/requirements.txt
:
parse
# /app/analyze.py
from parse import parse
def is_vogon(poem):
"""determines whether or not a poem is vogon"""
for line in poem:
parse_result = parse('On a {} bee,', line)
if parse_result:
if parse_result[0] == 'lurgid':
return True
return False
if __name__ == "__main__":
# print('VERSION TWO POINT OH!!!!!!')
print('analyzing poem readability')
# open the poem
with open('/content/poem.txt') as fp:
poem = fp.readlines()
# should we read it?????????
if is_vogon(poem):
print('third worst poetry in the galaxy, pls avoid')
else:
print('definitely worth reading')
/content/poem.txt
:
Oh freddled gruntbuggly,
Thy micturations are to me, (with big yawning)
As plurdled gabbleblotchits, in midsummer morning
On a lurgid bee,
That mordiously hath blurted out,
Its earted jurtles, grumbling
Into a rancid festering confectious organ squealer. [drowned out by moaning and screaming]
Now the jurpling slayjid agrocrustles,
Are slurping hagrilly up the axlegrurts,
And living glupules frart and stipulate,
Like jowling meated liverslime,
Groop, I implore thee, my foonting turlingdromes,
And hooptiously drangle me,
With crinkly bindlewurdles,mashurbitries.
Or else I shall rend thee in the gobberwarts with my blurglecruncheon,
See if I don't!
you know the drill:
docker build -t py_ezpz .
and run it
docker run -t py_ezpz
check it out.
we can fix this.
# Dockerfile---image name: py_ezpz
FROM python:3-alpine3.10
WORKDIR /app
COPY app /app <-------- CACHE BUSTER
RUN pip install -r requirements.txt
COPY content /content
CMD ["python", "/app/analyze.py"]
# Dockerfile---image name: py_ezpz
FROM python:3-alpine3.10
copy requirements.txt requirements.txt
RUN pip install -r requirements.txt
WORKDIR /app
COPY app /app
COPY content /content
CMD ["python", "/app/analyze.py"]
docker run -v ~/gitrepos/pydocker_talk/py_ezpz/content/:/content -t py_ezpz
check it out.
we can make this even more reusable.
imagine the case where everybody on a team uses the same tools (dependencies) but has different tasks.
why?
# Dockerfile---image name: analyze_base
FROM python:3-alpine3.10
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
and build it:
docker build -t analyze_base -f Dockerfile.base .
# Dockerfile---image name: analyze
FROM analyze_base
WORKDIR /app
COPY app /app
CMD ["python", "/app/analyze.py"]
and build it:
docker build --no-cache -t analyze .
docker run -v ~/gitrepos/pydocker_talk/py_inherit/content/:/content -t analyze
check it out.
shudder
no but really, conda can be very useful because in many cases it will handle system level dependencies as well.
it is definitely quirky though.
you can inherit from a miniconda3 container and start building your environment from there.
pro tips:
conda-forge
channel as much as possible and remove defaults
from your .condarc
.this starts to get a little more tricky.
RUN
commands that install your packagesthen you could build the image locally and distribute using dockerhub
are there any other suggestions for how to securely/privately do this over the internet?
slides:
thisisreal.net/talks/20190709_boulder_python/
¶notebook:
https://github.com/whlteXbread/talk_notebooks/
¶andrew catellier