DevOps and Build Automation with Python
4th Course in Python Scripting for DevOps Specialization
Containers for Development
In this module, we look at containers. Containers allow developers
to have a control environment to test and deploy their applications.
2 © LearnQuest 2021
Learning Objectives
Containers for Development
Upon completion of this module, learners will be able to:
• Describe the need for containers
• Install & configure a Docker container
• Script Docker with Python
3 © LearnQuest 2021
Lesson 1 In this lesson we look at the
Containers need for containers
4 © LearnQuest 2021
Virtual Machines
• A virtual machine (VM) is the
virtualization or emulation of a
computer system
• VMs run a complete operating
system–including its own kernel
5 © LearnQuest 2021
Containers
• A container is an isolated,
lightweight silo for running an
application on the host
operating system
• Containers build on top of the
host operating system's kernel
• The host and container OSs
must be the same
6 © LearnQuest 2021
Common VM Providers
VMware
vSphere VirtualBox
Xen Hyper-V KVM
7 © LearnQuest 2021
Common Container Providers
Linux Containers Docker Windows
• LXC Server
• LXD Containers
• CGManager
8 © LearnQuest 2021
Benefits of Containers for DevOps
Less Overhead Reproducibility Immutability
Containers are Containers are much By default Containers
typically faster easier to reproduce are immutable which
is essential for
software testing
9 © LearnQuest 2021
Lesson 1
Review Each VM guest can has its own OS
Containers share an OS
Containers are immutable
by default
10 © LearnQuest 2021
Lesson 2 In this lesson we look at installing
and configuring a Docker
Docker container
11 © LearnQuest 2021
Installing Docker
• Download Docker Desktop for Mac:
https://desktop.docker.com/mac/stable/Docker.dmg
• Download Docker Desktop for Windows:
https://desktop.docker.com/win/stable/Docker%20Desktop%20Installer.exe
• Install Docker Engine on Linux:
https://docs.docker.com/engine/install/
12 © LearnQuest 2021
Dockerfile
• A Dockerfile is simply a text-based script of
instructions that is used to create a container image
• The file is named DockerFile
• A Dockerfile is a step by step set of instructions
• Docker provides a set of standard instructions to be
used in the Dockerfile, like FROM, COPY, RUN, ENV,
EXPOSE, CMD just to name a few basic ones
• Docker will build a Docker image automatically by
reading these instructions from the Dockerfile
13 © LearnQuest 2021
Docker Hub
• Docker Hub is a service provided by Docker for finding and sharing
container images with your team.
• It is the world’s largest repository of container images with an array of
content sources including container community developers, open-
source projects and independent software vendors (ISV) building and
distributing their code in containers.
• Users get access to free public repositories for storing and sharing images
or can choose a subscription plan for private repositories.
14 © LearnQuest 2021
Example Dockerfile
1. FROM ubuntu:18.04
2. RUN apt-get update && apt-get install -y python3
3. RUN mkdir /aspen
4. COPY test.py /aspen/test.py
5. RUN chmod a+rwx -R /aspen/
6. ENTRYPOINT ["aspen/test.py"]
15 © LearnQuest 2021
Docker Command Line
• To build the image: docker build –t [image name]
• To show images: docker Images
• To run an image: docker run [image name]
16 © LearnQuest 2021
Lesson 2
A Dockerfile is simply a text-based
Review script of instructions that is used to
create a container image
The file is named DockerFile
You build a DockerFile
into an Image
17 © LearnQuest 2021
Lesson 3 In this lesson we look at how we
Scripting Docker can script Docker with Python
with Python
18 © LearnQuest 2021
Scripting vs Hosting Python
Scripting Python Hosting Python
Scripting is used to Hosting is what is done
automate the steps in when Python runs in a
managing Docker files, Docker container
images and containers
19 © LearnQuest 2021
Installing Docker SDK for Python
pip install docker
20 © LearnQuest 2021
Simple Example Script
1. import docker
2. client = docker.from_env()
3. containers = client.containers.list()
4. container = client.containers.get(containers[0])
5. logs = container.logs()
6. container.stop()
21 © LearnQuest 2021
Use Cases for Scripting
• Run Images
• Run Commands in Containers
• Measure Resource Usage
• Manage Resource Allocation
• Automate Log Analysis
22 © LearnQuest 2021
Lesson 3
Review Python runs in the container
when hosting Python
Python runs outside the container
when scripting with Python
You can do almost everything in
Scripts that you can do from the
command line
23 © LearnQuest 2021