A Hands-on Introduction To Docker

Transcription

SATURN 2017A Hands onIntroduction to DockerLen BassA Handsonon introductionIntroduction totoDockerHandsDockerMay 1–4,2017 2017LenBass 2017 Len Bass1

SATURN 2017Setting expectationsThis is an introduction to Docker intended for those who haveno hands on experience with Docker.If you have used Docker you will likely not get much from thissession.The material (and hands on portion) is taken from the coursethat I teach at CMU called DevOps: Engineering forDeployment and Operations.A Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass2

SATURN 2017LogisticsYou should have installed Docker on your laptop – either innative mode or using Docker Toolbox.Make sure Hello World works (from the installationinstructions).Make sure you have access to the internet since you will bedownloading software.A Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass3

SATURN 2017OutlineIntroduction to DockerHands onWhat’s left?A Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass4

SATURN 2017IsolationProcess Isolate address space No isolation for files ornetworks LightweightVirtual Machine Isolate address space isolate files andnetworks HeavyweightA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass5

SATURN 2017ContainersProcess Isolate address space No isolation for files ornetworks LightweightContainer Isolate address space isolate files andnetworks LightweightVirtual Machine Isolate address space isolate files andnetworks HeavyweightA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass6

SATURN 2017Docker containersA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass7

SATURN 2017Docker ArchitectureDocker daemon Lives on thehost Responds todockercommandsDocker daemon Instantiatesimages andcreatescontainersImage is instantiated to form containerA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass8

SATURN 2017LayersA Docker container image is structured in terms of “layers”.Process for building image Start with base image Load software desired Commit base image software to form new image New image can then be base for more softwareImage is what is transferredA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass9

SATURN 2017Loading of softwareOS is 1GB(yte)Fast network is 1Gb(it) ratedSince there are 8 bits per byte, transferring an OS should take8 seconds.But a 1Gb rated network is 35Mb in practiceThis means loading an OS is 30 secondsConsequently, sharing an OS saves 30 seconds perinstance. Sharing other software saves therne-bandwidth,23213.htmlA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass10

SATURN 2017Exploiting layersWhen an image is updated, only update new layersUnchanged layers do not need to be updatedConsequently, less software is transferred and an update isfaster.A Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass11

SATURN 2017Trade offsVirtual machine gives you all the freedom you have with baremetal Choice of operating system Total control over networking arrangement and filestructuresContainer is constrained in terms of operating systemsavailable Currently just Linux but soon Windows and OSX Provides limited networking options Provides limited file structuring optionsA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass12

SATURN 2017OutlineIntroduction to DockerHands onWhat’s left?A Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass13

SATURN 2017Hands on portionIf you have loaded Docker Toolbox, you have a copy ofVirtualBoxSet port forwarding on “default” so that 8080 on host isforwarded to 8080 on VM.A Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass14

SATURN 2017docker pull ubuntuExecute “docker pull Ubuntu”This loads an image from the docker libraryThe image contains bare copy of ubuntuA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass15

SATURN 2017docker imagesExecute “docker images”This generates a list of images known to Docker on yourmachineYou should see Hello World and ubuntuA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass16

SATURN 2017docker run –i –t ubuntuExecute docker run –i –t UbuntuThis executes an image. An executing image is called a“container”.You are now inside the container.Execute “ls”. A directory structure is set up but only a bare bones OShas been loadedA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass17

SATURN 2017Install software on containerExecuteapt-get updateapt-get install wgetapt-get install nodejsapt-get install npm cntl d This installs the software you will use during thissession and exits the containerA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass18

SATURN 2017docker ps –aExecute “docker ps –a”This generates a list of all of the containers that have beenrunA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass19

SATURN 2017Output from docker ps -aCONTAINER buntu"/bin/bash"agoExited (0) About a minute agosharp mcnulty7 minutes54ae910238b3hello-world"/hello"minutes agoExited (0) 53 minutes agopractical euler53A Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass20

SATURN 2017docker commit sharp mcnulty saturnNote that the ubuntu container has a name of “sharp mcnulty”(on my machine). It will be different on yours.“docker commit sharp mcnulty saturn” creates an image withthe name saturnA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass21

SATURN 2017Execute “docker images”REPOSITORYCREATEDTAGSIZEIMAGE IDsaturnlatestago456 MBa7056797123013 secondsubuntu130 MB0ef2e08ed3fa8 days agolatesthello-worldlatestago1.84 k48b5124b27687 weeksA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass22

SATURN 2017Execute “run –i –t Saturn”You are back inside a container. Load cmudevops/ipshow.js/master/initialization ops/ipshow.js/master/ipshow.jsA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass23

SATURN 2017Exit the container - cntl d A Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass24

SATURN 2017List containers docker ps -aCONTAINER aturnExited (0) 8 seconds ago"/bin/bash"2 minutes agoreverent lewin174268c64fbdubuntuExited (0) 24 minutes ago"/bin/bash"30 minutes agosharp mcnulty54ae910238b3hello-world"/hello"ago Exited (0) About an hour agoAbout an hourpractical eulerA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass25

SATURN 2017Make an image called ipshowdocker commit reverent lewin ipshow docker imagesREPOSITORYSIZEipshowsaturnMB none MBubuntuhello-worldTAGlatestlatest none latestlatestIMAGE 48b5124b2768CREATED6 seconds ago456 MB11 minutes ago45621 minutes ago8 days ago456130 MB7 weeks agoA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass1.84 k26

SATURN 2017Execute appdocker run –i –t –p 0.0.0.0:8080:8080 ipshow /bin/bash/initialization scriptIn browser: localhost:8080You should see three ip addresses in the browser:Ip address of local host127.0.0.1 (conventially this is local host)Ip address of containerA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass27

SATURN 2017What have we seenDistinction between docker images and containersCreating a docker image in layersProvisioning the docker image from the internetA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass28

SATURN 2017What is left?ScriptingSharing of imagesScaling of images Swarm AWS container service LambdaA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass29

SATURN 2017ScriptingCreating an image by hand is tedious and error proneYou can create a script to do this (Dockerfile).A Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass30

SATURN 2017Sharing imageMultiple team members may wish to share imagesImages can be in production, under development or undertestDocker Hub is a repository where images can be stored andshared. Each image is tagged to allow versioning Any image can be “pulled” to any host (with appropriatecredentials) Tagging as “latest” allows updates to be propagated. Pull image name :latest gets the last image checked intorepository with that name.A Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass31

SATURN 2017Allocation of images to hostsimagesTo run an image, the image and the host mustbe specifiedhostsWith basic Docker this allocation mustbe done manuallyA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass32

SATURN 2017Docker SwarmimageTo run an image, the image but not the hostmust be specifiedSwarmencapsulateshostsA swarm looks like a single host fromthe point of view of allocation butactually consists of multiple hostsA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass33

SATURN 2017Swarm MasterimageRun request is sentto swarm masterwhich selects hostSwarm Master is aspecific containeron a host not in theswarmSwarmA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass34

SATURN 2017How do containers get to hosts?Three options Containers can be copied at each invocation.- Copying time is overhead- Makes hosts flexible with respect to which containers they run Containers can be preloaded on hosts- No copying time at invocation- When there are multiple different containers, allocator isconstrained to allocate to hosts with appropriate containers. Some layers can be preloaded on hosts- Only copying time for additional layers- Allocator is constrained to allocate to appropriate preloadedsoftwareA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass35

SATURN 2017Multiple swarmsIt is possible to have multiple swarms simultaneously activeSwarm discovery token is used to identify which swarm eachhost belongs toA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass36

SATURN 2017Scaling SwarmsHaving an instance in a swarm be automatically replicateddepending on workload is accomplished by utilizingautoscaling facilities of cloud providerAWS has an EC2 container management facility thatcombines features of Docker Swarm and autoscaling.A Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass37

SATURN 2017AWS EC2 container managementA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass38

SATURN 2017AWS LambdaAWS also has a facility called “Lambda” that consists ofpreloaded OS execution engines. Exists for Java Node.js Python C#AWS maintains pool of partially loaded containers that onlyrequire app specific layer. Load in micro secs. Only one request per Lambda instanceA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass39

SATURN 2017SummaryA container is a lightweight virtual machine that providesaddress space, network, file isolationDocker allows building images in layers and deployment of anew version just requires deploying layers that have changed.Containers can be managed either on VMs throughautoscaling or on preallocated pool for short duration, quickloadingDevelopment workflow is supported through an imagerepository.A Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass40

SATURN 2017Questions and book pitchA Hands on Introduction to DockerMay 1–4, 2017 2017 Len Bass41

Node.js Python C# AWS maintains pool of partially loaded containers that only require app specific layer. Load in micro secs. Only one request per Lambda instance. 40 A Hands on Introduction to