ABCs of Docker

62

description

Introduction to Docker

Transcript of ABCs of Docker

Page 1: ABCs of Docker
Page 2: ABCs of Docker

● Basics of container and Docker● Major Docker components● Starting learning basic docker commands● Hands-on labs and exercises● Quiz

Course outline

Page 3: ABCs of Docker

Prerequisites

● Basic knowledge of how OS works, no need to be expert

● At least 1 virtual machine

Page 4: ABCs of Docker

Traditional application deployment

Page 5: ABCs of Docker

A bit of scale makes a bit of scare

Page 6: ABCs of Docker
Page 7: ABCs of Docker
Page 8: ABCs of Docker
Page 9: ABCs of Docker
Page 10: ABCs of Docker
Page 11: ABCs of Docker
Page 12: ABCs of Docker

Containers under the hood

Page 13: ABCs of Docker
Page 14: ABCs of Docker
Page 15: ABCs of Docker
Page 16: ABCs of Docker
Page 17: ABCs of Docker
Page 18: ABCs of Docker
Page 19: ABCs of Docker
Page 20: ABCs of Docker
Page 21: ABCs of Docker
Page 22: ABCs of Docker
Page 23: ABCs of Docker

cgroups - Wikipedia

cgroups (abbreviated from control groups) is a Linux kernel feature that limits, accounts for and isolates the resource usage (CPU, memory, disk I/O, network, etc.) of a collection of processes.Engineers at Google (primarily Paul Menage and Rohit Seth) started the work on this feature in 2006, under the name "process containers".[1] In late 2007 the nomenclature changed to "control groups" due to the confusion caused by multiple meanings of the term "container" in the Linux kernel context, and control-group functionality merged into kernel version 2.6.24.[2] Since then, developers have added many new features and controllers, such as support for kernfs,[3] firewalling,[4] and unified hierarchy.[5]

Page 24: ABCs of Docker
Page 25: ABCs of Docker
Page 26: ABCs of Docker

Quiz - Containers

What are the benefits of using container instead of VM?● No need for hypervisor● No need for operating system● No physical hardware● No need for priveleged user● No. It’s uselles.

Page 27: ABCs of Docker

Quiz - Containers

What are the benefits of using container instead of VM?● No need for hypervisor● No need for operating system● No physical hardware● No need for priveleged user● No. It’s uselles.

Page 28: ABCs of Docker

I create process with PID=1234 in container. What PID will be on host OS?a. the same as in container: PID=1234b. on host there will be PID=4321 that maps to PID=1234

in containerc. it wont be created in container. Actually container

creates it on host.d. there will be no pid on host. PID=1234 is the child

process in container

Quiz - Containers

Page 29: ABCs of Docker

I create process with PID=1234 in container. What PID will be on host OS?a. the same as in container: PID=1234b. on host there will be PID=4321 that maps to PID=1234

in containerc. it wont be created in container. Actually container

creates it on host.d. there will be no pid on host. PID=1234 is the child

process in container

Quiz - Containers

Page 30: ABCs of Docker

What are the main components of namespaces?a. uts, ipc, pid, network, user, mountb. user, pid, mount, filesystem, application, ioc. pid, os, hardware, user, internet, filesystemd. mount, hardware, network, user, pid, cgroup

Quiz - Containers

Page 31: ABCs of Docker

What are the main components of namespaces?a. uts, ipc, pid, network, user, mountb. user, pid, mount, filesystem, application, ioc. pid, os, hardware, user, internet, filesystemd. mount, hardware, network, user, pid, cgroup

Quiz - Containers

Page 32: ABCs of Docker

Quiz - Containers

What should i do to run application with unprivileged user in container?a. change the owner to current user and run itb. login as sudo and run the applicationc. enter the container and run itd. login as sudo then chown the application then run it

Page 33: ABCs of Docker

Quiz - Containers

What should i do to run application with unprivileged user in container?a. change the owner to current user and run itb. login as sudo and run the applicationc. enter the container and run itd. login as sudo then chown the application then run it

Page 34: ABCs of Docker
Page 35: ABCs of Docker
Page 36: ABCs of Docker
Page 37: ABCs of Docker
Page 38: ABCs of Docker
Page 39: ABCs of Docker

Major Docker components

Page 40: ABCs of Docker
Page 41: ABCs of Docker

In short: write once, really run anywhere

Page 42: ABCs of Docker

Docker images and containers

Page 43: ABCs of Docker

pull the image from docker cloud servers

Page 44: ABCs of Docker

Images repository - Dockerhub

Page 45: ABCs of Docker

Installing Docker engine

1. Open https://docs.docker.com/installation/

2. Find the name of your host operating system from the list

3. Follow the instructions

Page 46: ABCs of Docker

Linux: Add current user to docker group

sudo gpasswd -a <user_name> docker

Page 47: ABCs of Docker

Mac OS: set env variables for boot2docker

boot2docker up

export DOCKER_HOST=tcp://192.168.59.103:2376export DOCKER_CERT_PATH=<cert path>export DOCKER_TLS_VERIFY=1or $(boot2docker shellinit)

Get ip of boot2docker: boot2docker ipCheck version of docker: docker version

Page 48: ABCs of Docker

Windows: maybe like on MacOS...but didnt try

boot2docker up

set DOCKER_HOST=tcp://192.168.59.103:2376set DOCKER_CERT_PATH=<cert path>set DOCKER_TLS_VERIFY=1or boot2docker shellinit

Get ip of boot2docker: boot2docker ipCheck version of docker: docker version

Page 49: ABCs of Docker

Let docker say: Hello World!

docker run hello-world

Page 50: ABCs of Docker

Now let’s get into the container

1) docker pull centos – fetch centos image from repository2) docker run centos – run fetched centos image within container3) docker ps - list the running containers4) docker ps -a - list all the containers (running + not running)5) docker exec - to execute the command inside container6) docker attach - get into the container7) exit from container quits the container8) Ctrl+P and Ctrl+Q leaves the running container9) docker inspect <container_id> - show info about running container

Page 51: ABCs of Docker

Lab: Run required container

IOS/Android developers:1) docker run -d –p 8080:8080 jenkins2) Open http://<docker_host>:8080 in your browser and create build plan in

jenkins

Service developers:3) docker run -it --rm williamyeh/scala4) Write hello world in scala

TIP: docker --help is you cheat sheet

Page 52: ABCs of Docker

Quiz - Docker

What is the difference between images and containers?a. containers consist of binary files, images consist of user app filesb. containers run only once, whereas images run multiple timesc. containers pulled from dockerhub, whereas images stores locallyd. images consist of instructions and user files, whereas containers

only runtime environment for user process

Page 53: ABCs of Docker

Quiz - Docker

What is the difference between images and containers?a. containers consist of binary files, images consist of user app filesb. containers run only once, whereas images run multiple timesc. containers pulled from dockerhub, whereas images stores locallyd. images consist of instructions and user files, whereas containers

only runtime environment for user process

Page 54: ABCs of Docker

Quiz - DockerWhat will be the result of the following instructions?docker run -it my-image /bin/bash # let’s say it returns ID 123...echo “Sample text” | cat > SampleText.txtexitdocker cp 123:/root/SampleText.txt ./

a. will copy SampleText.txt to current directoryb. will copy data from the current directory to /root/SampleText.txt inside containerc. prints to screen Sample text and copies SampleText.txt to current directory on docker hostd. does nothing. Terminates with error

Page 55: ABCs of Docker

Quiz - DockerWhat will be the result of the following instructions?docker run -it my-image /bin/bash # let’s say it returns ID 123...echo “Sample text” | cat > SampleText.txtexitdocker cp 123:/root/SampleText.txt ./

a. will copy SampleText.txt to current directoryb. will copy data from the current directory to /root/SampleText.txt inside containerc. prints to screen Sample text and copies SampleText.txt to current directory on docker hostd. does nothing. Terminates with error

Page 56: ABCs of Docker

Quiz - DockerWhat will be the result of the following instructions?docker run -d my-image /bin/bash echo “Sample text” | cat > SampleText.txt # let’s say returns id 123...docker run -d my-image /bin/bash “ping 8.8.8.8” docker cp 123:/root/SampleText.txt ./

a. will copy SampleText.txt to current directoryb. will copy data from the current directory to /root/SampleText.txt inside containerc. prints to screen Sample text and copies SampleText.txt to current directory on docker hostd. does nothing. Terminates with error

Page 57: ABCs of Docker

Quiz - DockerWhat will be the result of the following instructions?docker run -d my-image /bin/bash echo “Sample text” | cat > SampleText.txt # let’s say returns id 123...docker run -d my-image /bin/bash “ping 8.8.8.8” docker cp 123:/root/SampleText.txt ./

a. will copy SampleText.txt to current directoryb. will copy data from the current directory to /root/SampleText.txt inside containerc. prints to screen Sample text and copies SampleText.txt to current directory on docker hostd. does nothing. Terminates with error

Page 58: ABCs of Docker

Writing own Dockerfile

FROM ubuntu:latestMAINTAINTER <name surname>FROM ubuntu:latestRUN apt-get updateRUN apt-get install -y nginxCMD ["nginx", "-g", "daemon off;"]

Page 59: ABCs of Docker

Docker Workflow 1

1) Download file from curl -L -O http://github.com/atbaker/flask-example/archive/master.zip2) Unzip master.zip3) run python flask-example.py

Build as docker:

FROM python:2-onbuildEXPOSE 8000CMD [“gunicorn”, “-c”, “gunicorn_config.py”, “flask-example:app”]

Page 60: ABCs of Docker

Docker Workflow 2

Download file from curl -L -O http://github.com/atbaker/django-example/archive/master.zip

Build as docker:

FROM python:2.7-onbuildEXPOSE 8000CMD [“gunicorn”, “-c”, “gunicorn_config.py”, “--chdr”, “django-example”, “wsgi:application”]

docker run --name postgres -d postgres:9.3docker run --name memcached -d atbaker/memcached-verbosedocker run --name django -d -p 8000:8000 --link postgres:db --link memcached:cache django-exampledocker run --name django --link postgres:db --link memcached:cache django-example python django/example/manage.py migrate

Page 61: ABCs of Docker

Docker workflow 2 with Docker-Compose

django: build: . links:

- postgres:db- memcached:cache

ports:- “8000:80”

postgres: image:postgres:9.3

memcached: image:atbaker/memcached-verbose

Page 62: ABCs of Docker

Useful resources

https://docs.docker.com/ - official docs from Dockerhttps://docs.docker.com/compose/ - official docs about Docker Composehttps://docs.docker.com/docker/introduction/understanding-docker/ - Docker architecturehttps://linuxcontainers.org/ - WiKi about Linux Containers (LXC)https://lwn.net/Articles/531114/ - Linux namespaces overviewhttps://lwn.net/Articles/532748/ - Linux PID namespaces