Tut2 Python and Docker
Tut2 Python and Docker
6. Examples
7. Python Code Snippets
8. Conclusion
Recap: Python
• Dynamically-typed scripting language
• Very easy to learn
• Interactive front-end for C/C++ code
• Object-oriented
• Lots of libraries
− Including tools for data analysis
• Powerful, scalable
− Supporting tools that handle very large datasets
Introducing Docker
Introducing Docker
• Docker is a platform for developers and sysadmins to develop, deploy, and run applications
with containers. The use of Linux containers to deploy applications is called containerization.
Containers are not new, but their use for easily deploying applications is.
− Containerization is increasingly popular because containers are:
− Flexible: Even the most complex applications can be containerized.
− Lightweight: Containers leverage and share the host kernel.
− Interchangeable: You can deploy updates and upgrades on-the-fly.
− Portable: You can build locally, deploy to the cloud, and run anywhere.
− Scalable: You can increase and automatically distribute container replicas.
− Stackable: You can stack services vertically and on-the-fly.
Docker Basics
Images and Containers Containers vs Virtual Machines
• A container is launched by running an image. An • A container runs natively on Linux and shares
image is an executable package that includes the kernel of the host machine with other
everything needed to run an application--the containers. It runs a discrete process, taking no
code, a runtime, libraries, environment variables, more memory than any other executable,
and configuration files. making it lightweight.
• A container is a runtime instance of an image-- • By contrast, a virtual machine (VM) runs a full-
what the image becomes in memory when blown “guest” operating system with virtual
executed (that is, an image with state, or a user access to host resources through a hypervisor.
process). You can see a list of your running In general, VMs provide an environment with
containers with the command, docker ps, just more resources than most applications need.
as you would in Linux.
Docker Basics: Images + Containers
• A Docker Image is the template (application plus required
binaries and libraries) needed to build a running Docker
Container (the running instance of that image). As
templates, images are what can be used to share a
containerized applications. Collections of images are
stored/shared in registries like Docker Hub.
Why Docker?
• A lightweight approach that allows us to simulate an environment that has
parallels to how one might interact with a cloud-based VM or container, without
having the overhead and cost of setting up AWS or Azure instances.
export DOCKER_COMPOSE_VERSION=1.22.0
sudo curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-$(uname –s)-$(uname –m) -o /usr/local/bin/docker-compose
sudo chmod a+rx /usr/local/bin/docker-compose
# Uninstall:
# sudo apt-get remove docker-ce
# sudo apt autoremove
# sudo rm /usr/local/bin/docker-compose
# sudo rm /etc/bash_completion.d/docker
# sudo rm /etc/bash_completion.d/docker-compose
Alternative for Windows (WSL):
• For Windows Subsystem for Linux, install Docker CE with instructions for
Alternative for Linux (Ubuntu), but requires the below setting be enabled in
the Docker Settings. There are more secure, but very involved workarounds.
https://medium.com/@sebagomez/installing-the-docker-client-on-ubuntus-windows-subsystem-for-linux-612b392a44c4
https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly
https://blogs.msdn.microsoft.com/commandline/2017/12/08/cross-post-wsl-interoperability-with-docker/
https://raesene.github.io/blog/2018/03/29/WSL-And-Docker/
https://docs.docker.com/engine/security/https/#create-a-ca-server-and-client-keys-with-openssl
https://blogs.technet.microsoft.com/stefan_stranger/2018/04/02/access-my-docker-for-windows-kubernetes-cluster-from-debian-wsl/
Additional Notes
• Recommend installing and using Git
• For Windows, Git comes with a Bash terminal
− The Git-Bash / MinGW terminal works with Docker with
some caveats requiring some workarounds.
− PowerShell and CMD should work will Docker
− Windows Subsystem for Linux can install Docker CE, but
can only be used as a client not the engine. See here.
Using Docker
Docker commands
Using Docker: The Basic Commands
• docker images List images
• docker pull Pull an image or a repository from a registry
• docker ps List containers
• docker run Run a command in a new container
• docker rm Remove one or more containers
• docker help Help about the command
https://docs.docker.com/get-started/
https://docs.docker.com/engine/reference/commandline/docker/
docker images
• The default docker images will show all top level images, their repository and tags, and their
size. The docker images command takes an optional [REPOSITORY[:TAG]] argument that
restricts the list to images that match the argument. If you specify REPOSITORY but no TAG,
the docker images command lists all images in the given repository.
docker pull
• Most of your images will be created on top of a base
image from the Docker Hub registry. Docker Hub contains
many pre-built images that you can pull and try without
needing to define and configure your own. To download a
particular image, or set of images (i.e., a repository), use
docker pull.
See: https://docs.docker.com/engine/reference/commandline/run/
docker run
docker run -it -v $PWD:/app -w /app python:3.7 bash
− -w /app lets the command being executed inside directory given, here /app. If the
path does not exist it is created inside the container.
− -it instructs Docker to allocate a pseudo-TTY connected to the container’s stdin;
creating an interactive bash shell in the container.
▪ -t, --tty Allocate a pseudo-TTY
▪ -i, --interactive Keep STDIN open even if not attached
− --rm automatically remove the container when it exits
See: https://docs.docker.com/engine/reference/commandline/run/
docker rm
docker rm $(docker ps -a -q)
− This command will delete all stopped containers. The
command docker ps -a -q will return all existing container
IDs and pass them to the rm command which will delete
them. Any running containers will not be deleted.
Other Common Commands
• docker start Start one or more stopped containers
• docker build Build an image from a Dockerfile
• docker cp Copy files/folders between a container and the local filesystem
• docker exec Run a command in a running container
• docker kill Kill one or more running containers
• docker pause Pause all processes within one or more containers
• docker stop Stop one or more running containers
Usage Notes
• For Windows Users using Git-Bash or MinGW
− You may need to prefix docker run with winpty
− When setting volumes or working directories absolute paths must be
prefixed with an extra /.
− For instance: docker run -it -v $PWD:/app –w /app ubuntu bash
− Becomes: winpty docker run -it -v /$PWD:/app –w //app ubuntu bash
Our Docker Environment
eecs4415 Image
Our class’s Docker image is now available at: eecsyorku/eecs4415
See docs: https://hub.docker.com/r/eecsyorku/eecs4415/
Demos
0. Basic Docker
$ docker pull hello-world
$ docker images hello-world
$ docker run hello-world
0. Basic Docker
$ docker run -it ubuntu bash
$ uname -a
Linux VC003 4.4.0-17134-Microsoft #285-Microsoft Thu Aug 30 17:31:00 PST 2018 x86_64
x86_64 x86_64 GNU/Linux
arguments = sys.argv[1:]
print(sys.stdin)
sys.stdout.write('Text to print.\n')
Classes:
class MyNameClass: Note:
"""Documentation for my MyNameClass class"""
• Methods prefixed by ‘__’ are
def __init__(self, name): usually reserved for system
""" defined methods like __init__
Initializes the object. (constructor) or __str__.
The keyword 'self', refers object (like 'this' in Java) • Methods prefixed by
""" underscore ‘_’ are private
methods within class.
self.name = name
• All other methods (not prefixed
by underscore ‘_’) are public
def __str__(self):
methods and accessible to calls
"""
outside of the class.
Returns a string representation of the object
Equivalent to ‘toString()' in Java
"""
return self.name
Reading a File / STDIN
import sys
def iterate_words(textfile):
with open(textfile, 'r') as f:
for line in iter(f):
line = line.strip()
words = filter(None, re.split('\W+', line))
for word in words:
# Use the yield keyword to specify the next iteration item
yield word.lower()
def process_words(textfile):
for word in iterate_words(textfile):
print(word)
Reading & Parsing CSV
import csv
See: https://docs.python.org/3/library/csv.html
Writing Files
with open(output, 'w') as f:
f.write('Text to write.\n')
Writing CSV
import csv
See: https://docs.python.org/3/library/csv.html
Find CSVs in a Directories
import os
import mimetypes
from os import path