Docker by Example Using A Visual Approach
Docker by Example Using A Visual Approach
Docker by Example Using A Visual Approach
Docker by Example
Using a Visual Approach
Ganesh & Hari
[email protected]
[email protected]
Why Docker?
Why Docker?
What is Docker?
Source: https://en.wikipedia.org/wiki/Docker_(software)
Pop quiz
Needs a hypervisor
Docker accesses virtualisation features of Linux
Native Docker support on Windows
Source: https://i2.wp.com/blog.docker.com/wp-content/uploads/windows.png?w=975&ssl=1
https://i2.wp.com/blog.docker.com/wp-content/uploads/windows.png?w=975&ssl=1
Docker for DevOps
Docker becoming popular over time
$ docker -v
Docker version 1.12.0-rc4, build e4a0dbc, experimental
Finding details of a Docker installation
Can I install Docker from commandline?
Yes! from get.docker.com
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker Hub account:
https://hub.docker.com
$ docker -h
Usage: docker [OPTIONS] COMMAND [arg...]
docker [ --help | -v | --version ]
Options:
Commands:
attach Attach to a running container
Docker commands look like Linux commands - so
familiarity with Linux commands can really help to
get up to speed quickly with Docker.
Docker Images
How to get list of images?
How to search for an image?
How to get an image?
Use “docker pull <image_name>” command
docker images -q
lists all image ids
Avoid “image sprawl”
Command
Image name argument
Command name
How to run a container interactively?
$ docker run -t -i fedora /bin/bash
[root@00eef5289c91 /]# pwd
/
[root@00eef5289c91 /]# whoami
root
[root@00eef5289c91 /]# ls
bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv
sys tmp usr var
[root@00eef5289c91 /]# cc
bash: cc: command not found
[root@00eef5289c91 /]# gcc
bash: gcc: command not found
[root@00eef5289c91 /]# java
bash: java: command not found
[root@00eef5289c91 /]# tar
bash: tar: command not found
[root@00eef5289c91 /]# exit
exit
$
Create a terminal
to interact with
$ hostname
ganesh
$ docker run -it alpine /bin/sh
/ # hostname
b4ebae46b156
/ # ps -a
PID USER TIME COMMAND
1 root 0:00 /bin/sh
6 root 0:00 ps -a
/ # exit
$ ps -a
PID TTY TIME CMD
15327 ttys001 0:00.02 login -pf gsamarthyam
15328 ttys001 0:00.27 -bash
How to run a container in the background?
$ docker run -d ubuntu /bin/sh -c "while true; do echo current date and time is: $(date); sleep
10; done"
9128bf57e03c3b32f0bf784a92332953996236d7e358a77c62c10bdec95fd5b9
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
9128bf57e03c ubuntu "/bin/sh -c 'while tr" About a minute ago Up About a
minute lonely_einstein
$ docker logs 9128bf57e03c3b32f0bf784a92332953996236d7e358a77c62c10bdec95fd5b9
current date and time is: Fri Jul 22 15:42:49 IST 2016
current date and time is: Fri Jul 22 15:42:49 IST 2016
current date and time is: Fri Jul 22 15:42:49 IST 2016
current date and time is: Fri Jul 22 15:42:49 IST 2016
// output elided
How to expose a port?
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "80"
}
]
},
Using Nginx
Type http://localhost:80 in your browser window
How to expose a port?
"Ports": {
"443/tcp": null,
"80/tcp": [
{
"HostIp": "0.0.0.0", randomly assigned and mapped
"HostPort": "32770" port number (by docker)
}
]
},
How to expose all exposed ports?
$ docker run -d ubuntu /bin/sh -c "while true; do echo current date and time is: $
(date); sleep 10; done"
acc349675098a0133366076f2082db6171ee4a0cd2e1e45ada9a485684ea4c01
$ docker attach acc349675098a0133366076f2082db6171ee4a0cd2e1e45ada9a485684ea4c01
current date and time is: Mon Aug 1 10:30:13 IST 2016
current date and time is: Mon Aug 1 10:30:13 IST 2016
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3651758ff308 wordpress:latest "/entrypoint.sh apach" 2 days ago Up 2 days 0.0.0.0:8000->80/tcp mywordpress_wordpress_1
b95388054539 mysql:5.7 "docker-entrypoint.sh" 2 days ago Up 2 days 3306/tcp mywordpress_db_1
How do I see all the containers?
Use “docker ps -a” command
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c378c6b84b1 fedora "/bin/echo 'Hello wor" 4 minutes ago Exited (0) 4 minutes ago grave_thompson
c4b2db95f268 hello-world "/hello" 5 minutes ago Exited (0) 5 minutes ago amazing_jones
2dcd9d0caf6f 777f9424d24d "/bin/bash" 42 minutes ago Exited (0) 42 minutes ago prickly_khorana
3651758ff308 wordpress:latest "/entrypoint.sh apach" 2 days ago Up 2 days 0.0.0.0:8000->80/tcp mywordpress_wordpress_1
b95388054539 mysql:5.7 "docker-entrypoint.sh" 2 days ago Up 2 days 3306/tcp mywordpress_db_1
4b984664f9aa golang:latest "go run myapp.go" 2 days ago Exited (1) 2 days ago mydocker_app_1
63cd7661a8ad hello-world "/hello" 2 days ago Exited (0) 2 days ago adoring_sammet
c191fbeae884 ubuntu "/bin/bash" 2 days ago Exited (0) 2 days ago clever_mcclintock
08e173332d46 docker/whalesay "cowsay Hello world" 2 days ago Exited (0) 2 days ago tender_joliot
6322b8204a5d 0f192147631d "/bin/bash" 9 days ago Exited (0) 9 days ago desperate_aryabhata
...
Explicitly remove exited containers
COPY . /usr/src/mycapp
WORKDIR /usr/src/mycapp
$ cat first.c
#include <stdio.h>
$ docker ps -a
CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS NAMES
4c12998fd392 debian "/bin/bash"
6 seconds ago Exited (0) 5 seconds ago sick_panini
Pop quiz
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
a53779a74904 debian "/bin/bash" 2 minutes ago
Up 2 minutes agitated_aryabhata
Building images using Dockerfile
Different ways to create images
ADD Copies files or directories from the host to the container in the given path
$ curl localhost:8080/hi
hello
Dockerfile for running a Java program
$ cat Dockerfile
FROM java:latest
COPY HiHello.class /
EXPOSE 8080
ENTRYPOINT ["java"]
CMD ["HiHello"]
$ docker build .
Sending build context to Docker daemon 6.656 kB
Step 1 : FROM java:latest
---> 264282a59a95
// ... Successfully built 60a14f519720
$ docker run -d -p 8080:8080 60a14f519720
16f6d7eca560c96b995be9f0c6d68167930ab7501451a452818e04ce29ec177f
$ curl localhost:8080/hi
hello
Pop quiz
There is NO way to recreate the Dockerfile that was used to build that
image from a given image id/tag using Docker CLI.
Think about Makefile: can you recreate the Makefile that was used to
build that executable file? No.
However, you can see the commands used to create the layers in the
image. Pass “—no-trunc” option to “docker history” command.
Example: “docker history --no-trunc google/cadvisor"
Try it now!
Docker Volumes
Docker volume commands
Command Description
❖ You can “clean up” the volumes if you aren't using them.
Use the command “docker volume rm $(docker volume
ls -q)“ to remove all the volumes.
Use Flocker (data volume manager)
See: https://clusterhq.com/flocker/
source: https://clusterhq.com/assets/images/diagrams/diagram-1.jpg
Docker Machine
Docker Machine
Create and manage machines running Docker (cloud or on your computer)
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
myhost - virtualbox Running tcp://192.168.99.100:2376 v1.12.2
Docker Compose
docker-compose commands
Command Description
Command Description
root@856aed6a92f1:/# cat /etc/hosts There are many ways to get the IP address of a
// ...
172.17.0.6 856aed6a92f1 container:
root@856aed6a92f1:/# 1. Use the docker inspect command
2. Use ip addr command from the container’s shell
3. Use “cat /etc/hosts” and check the entry for the
container
How to get port mappings of a container?
$ docker run -d -p5000:5000 registry
c51b984b4d64a05e924c7677f20e8c5c386e8bb53f5de0369337d31f73a7cf7e
$ docker port
c51b984b4d64a05e924c7677f20e8c5c386e8bb53f5de0369337d31f73a7cf7e
5000/tcp -> 0.0.0.0:5000
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
de6e17ededc8 nginx "nginx -g 'daemon off" 21 seconds
ago Up 20 seconds 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp
reverent_wright
Three kinds of networks
$ docker network ls
Default, single-host driver
NETWORK ID NAME DRIVER SCOPE
a3bb9a40c8e3 bridge bridge local
399711fd0635 host host local
790ae8b43d9b none null local
Docker network commands
Command Description
docker run -d --name myubuntu ubuntu /bin/sh -c "while true; do echo current date and
time is: $(date); sleep 10; done”
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the
instructions.
Clustering for Docker
“batteries included but swappable”
Docker Security
Docker workbench for security
docker run -it --net host --pid host --cap-add audit_control \
-v /var/lib:/var/lib \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/lib/systemd:/usr/lib/systemd \
-v /etc:/etc --label docker_bench_security \
docker/docker-bench-security
OR
OR
Source: https://github.com/docker/docker-bench-security
Docker workbench for security
$ sh docker-bench-security.sh
# ------------------------------------------------------------------------------
# Docker Bench for Security v1.1.0
#
# Docker, Inc. (c) 2015-
#
# Checks for dozens of common best-practices around deploying Docker containers in production.
# Inspired by the CIS Docker 1.11 Benchmark:
# https://benchmarks.cisecurity.org/downloads/show-single/index.cfm?file=docker16.110
# ------------------------------------------------------------------------------
$ docker stats
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
sleepy_wescoff 0.00% 1.031 MiB / 4 MiB 25.78% 648 B / 648 B 0 B / 0 B 1
Printing containers names in stat
localhost:8080
Monitoring Docker
❖ datadog (https://www.datadoghq.com/)
❖ sysdig (http://www.sysdig.org/)
❖ prometheus (https://prometheus.io/)
Other topics
How do debug on a running container?
Use “docker exec” command
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
9128bf57e03c ubuntu "/bin/sh -c 'while tr" 24 minutes ago Up
24 minutes lonely_einstein
$ docker exec -ti lonely_einstein /bin/bash
root@9128bf57e03c:/#
Can I use GUI instead of command-line?
Use “kitematic” (https://github.com/docker/kitematic)
Different ways to access Docker
Graphical User
Interface (Kitematic)
See: https://github.com/jpetazzo/dind
Myths and Misconceptions
Docker *completely* replaces
VMs
Containers AND VMs
Docker is *completely* portable
Build once, run anywhere - but conditions apply!
http://amzn.com/1491917571
“Docker: Up & Running”, Karl Matthias, Sean P. Kane, O'Reilly Media; 1 edition (July 3, 2015)
THE DOCKER BOOK
➤ Interesting sub-title:
“Containerization is the new
virtualization”.
➤ From James Turnbull (CTO at
Kickstarter and Advisor at
Docker)
➤ Useful to get comfortable with
core concepts of Docker
➤ Useful for developers,
operations staff (and DevOps),
and SysAdmins
➤ Supporting website: http://
http://www.amazon.in/dp/B00LRROTI4
dockerbook.com/
The Docker Book, James Turnbull, Amazon Digital South Asia Services, July 2014
DOCKER COOKBOOK
➤ Contents written in recipe
format (Problem, Solution,
Discussion)
➤ Useful because we can look for
solutions to the problems that
we face when using Docker
http://amzn.com/149191971X
“Docker Cookbook”, Sébastien Goasguen, O'Reilly Media, 2015
DOCKER IN ACTION
➤ Wide coverage from basics to
advanced topics like managing
massive clusters
➤ Book organised into three parts:
➤ Keeping a tidy computer
AngularJS (22nd Oct)
Modern Software Architecture (5th Nov)
SOLID Principles (19th Nov)
Meetups
h"p://www.meetup.com/JavaScript-Meetup-Bangalore/
h"p://www.meetup.com/Container-Developers-Meetup-Bangalore/
h"p://www.meetup.com/So>ware-Cra>smanship-Bangalore-Meetup/
h"p://www.meetup.com/Core-Java-Meetup-Bangalore/
h"p://www.meetup.com/Technical-Writers-Meetup-Bangalore/
h"p://www.meetup.com/CloudOps-Meetup-Bangalore/
h"p://www.meetup.com/Bangalore-SDN-IoT-NetworkVirtualizaHon-Enthusiasts/
h"p://www.meetup.com/So>wareArchitectsBangalore/
[email protected] @GSamarthyam
www.codeops.tech slideshare.net/sgganesh
+91 98801 64463 bit.ly/ganeshsg
Image credits
❖ https://pbs.twimg.com/media/CH-ISJGUwAAt8hQ.png
❖ http://patg.net/assets/container_vs_vm.jpg
❖ http://static1.businessinsider.com/image/525e9c7669bedd9c3015dc60-1190-625/the-10-funniest-dilbert-comic-strips-about-idiot-bosses.jpg
❖ https://blog.docker.com/wp-content/uploads/2014/03/docker-execdriver-diagram.png
❖ https://docs.docker.com/engine/article-img/architecture.svg
❖ https://en.wikipedia.org/wiki/File:Docker-linux-interfaces.svg
❖ http://lohmander.me/content/images/2015/10/d2f.jpg
❖ https://camo.githubusercontent.com/
ec87adde4b3711198fb90ff112eb4361d313e067/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f7765622d6172746566616374732f6361727
46f6f6e2d7768616c652d382e6769662b28343030254333253937323235292e706e67
❖ http://blog.gutcheckit.com/hubfs/Headers/Blogs/Q215-Blog-KernelSeasonsRecap-Header-060415.jpg
❖ http://core0.staticworld.net/images/article/2014/11/docker_linux-100530817-primary.idge.jpg
❖ http://cdn.hrpayrollsystems.net/wp-content/uploads/2015/02/best-practices-hris.jpg
❖ https://blog.docker.com/media/2015/07/moby_art.png
❖ https://blog.docker.com/media/2015/04/sticker-02-15-2-1024x711.png
❖ http://blogs-images.forbes.com/janakirammsv/files/2016/06/docker1.jpg?width=960
❖ http://blogs-images.forbes.com/janakirammsv/files/2016/06/Docker_CI_CD.jpg?width=960
Image credits
❖ http://cormachogan.com/wp-content/uploads/2016/07/docker-volumes.jpg
❖ http://image.slidesharecdn.com/swarmonlinemeetup-150507153718-lva1-app6891/95/docker-
swarm-020-5-638.jpg?cb=1431013147
❖ http://image.slidesharecdn.com/swarmonlinemeetup-151111212937-lva1-app6892/95/docker-online-
meetup-28-productionready-docker-swarm-11-638.jpg?cb=1447459032
❖ https://blog.docker.com/media/2015/04/docker-turtles-communication.jpg
❖ https://pbs.twimg.com/media/CtSCE2FUEAA94Pd.jpg
❖ https://i0.wp.com/blog.docker.com/wp-content/uploads/3-1.png?w=560&ssl=1
❖ https://blog.docker.com/media/2015/11/logo-title-final-swarm-2d.png
❖ http://image.slidesharecdn.com/docker-swarm-mike-goelzer-mv-meetup-45min-
workshop022420161-160228024416/95/docker-swarm-docker-native-clustering-5-638.jpg?cb=1456856097
❖ http://54.71.194.30:4110/engine/reference/api/images/event_state.png/
❖ http://edge.alluremedia.com.au/m/l/2015/05/DockerExploration.jpg
❖ https://www.docker.com/sites/default/files/home-1-solutions-2_0.jpg
Image credits
❖ https://i2.wp.com/blog.docker.com/wp-content/uploads/windows.png?resize=975%2C546&ssl=1
❖ http://taylorholmes.com/wp-content/uploads/2010/08/totem11-1024x364.jpg
❖ https://cdn-images-2.medium.com/max/2000/1*k8n7Jx9UaLRAxum9HMp8nQ.png
❖ https://pbs.twimg.com/media/CpA4RzoXEAAf83a.png
❖ http://thenewstack.io/wp-content/uploads/2016/02/Docker.png
❖ https://lh3.googleusercontent.com/-4Cpex5VrtFM/Vl4mKLq5FbI/AAAAAAAAAxE/FJRVex2O6tE/w485-h370/
docker_monstro.png
❖ https://www.cloudbees.com/sites/default/files/jenkins-docker-cd-express.jpg
❖ http://jbu.io/wp-content/uploads/2015/10/docker.jpg
❖ https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRiV1MXhBG39oQPuyVyAF5ZMaYzi3pOYvm6pHeJA71x8PrfVD7p
❖ http://2.bp.blogspot.com/-0qweEK2XCg8/VhQS0dffOTI/AAAAAAAAArw/gpOGuJELCP4/s1600/docker.png
❖ http://momentumtelecom.com/wp-content/uploads/2014/10/training-icons-qrg.png
❖ https://learning-continuous-deployment.github.io/assets/images/compose.jpg
❖ http://www.showroomworkstation.org.uk/pictures/Logos/~jDKjyjDDDDKjyjU6/Try_This_sm.png