Problem Based Learning

Docker 1

I am Juan Cabrera

In [2]:
print ('Name: ', Juan['name'])
print ('Email: ', Juan['email'])
print ('Work: ', Juan['work'])
Name:  Juan A. Cabrera
Email:  juan.cabrera@tu-dresden.de
Work:  Deutsche Telekom Chair of Communication Networks

Slides: http://comnets.bitbucket.org/docker-pbl-2016/lecture-1/
Press esc for a menu of the slides

Slides inspired and adapted from:
Docker by Example. By:
Ganesh & Hari {ganesh|hari}(at)codeops.tech

Installing docker

The easiest way is to use the script available here

To install it, run one of the following commands:

wget -qO- https://get.docker.com/ | sh

or

curl -sSL https://get.docker.com/ | sh

They will use a CLI tool to download the script ("wget" or "curl"), and then "| sh" will pipe it to shell

If none of those commands work, ask me for help

Docker version

In [3]:
%%bash
docker -v
Docker version 1.12.2, build bb80604

Docker info

In [4]:
%%bash
docker info
Containers: 8
 Running: 4
 Paused: 0
 Stopped: 4
Images: 11
Server Version: 1.12.2
Storage Driver: devicemapper
 Pool Name: docker-8:2-7864838-pool
 Pool Blocksize: 65.54 kB
 Base Device Size: 10.74 GB
 Backing Filesystem: xfs
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data Space Used: 2.541 GB
 Data Space Total: 107.4 GB
 Data Space Available: 104.8 GB
 Metadata Space Used: 3.482 MB
 Metadata Space Total: 2.147 GB
 Metadata Space Available: 2.144 GB
 Thin Pool Minimum Free Space: 10.74 GB
 Udev Sync Supported: true
 Deferred Removal Enabled: false
 Deferred Deletion Enabled: false
 Deferred Deleted Device Count: 0
 Data loop file: /home/juarch/.docker/devicemapper/devicemapper/data
 Metadata loop file: /home/juarch/.docker/devicemapper/devicemapper/metadata
 Library Version: 1.02.135 (2016-09-26)
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: null bridge host overlay
Swarm: active
 NodeID: 2gmf7ny80vdpdxyycfsy1h8ov
 Is Manager: true
 ClusterID: 37i93mguaohbuhz2wi2braua0
 Managers: 1
 Nodes: 2
 Orchestration:
  Task History Retention Limit: 5
 Raft:
  Snapshot Interval: 10000
  Heartbeat Tick: 1
  Election Tick: 3
 Dispatcher:
  Heartbeat Period: 5 seconds
 CA Configuration:
  Expiry Duration: 3 months
 Node Address: 192.168.1.55
Runtimes: runc
Default Runtime: runc
Security Options: seccomp
Kernel Version: 4.8.4-1-ARCH
Operating System: Arch Linux
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.513 GiB
Name: the_shire
ID: AKUY:UC66:T5NP:ZAFR:YS6C:YW5O:N473:3U62:DUGN:LLDK:6NE5:D5OC
Docker Root Dir: /home/juarch/.docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Insecure Registries:
 127.0.0.0/8
 WARNING: Usage of loopback devices is strongly discouraged for production use. Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.

Running a Container

Hello World

In [5]:
%%bash
docker run docker/whalesay cowsay Hello World
 _____________ 
< Hello World >
 ------------- 
    \
     \
      \     
                    ##        .            
              ## ## ##       ==            
           ## ## ## ##      ===            
       /""""""""""""""""___/ ===        
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \______ o          __/            
        \    \        __/             
          \____\______/   

Hello World 2

In [6]:
%%bash
docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

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

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

Getting help

In [7]:
%%bash
docker -h
Usage: docker [OPTIONS] COMMAND [arg...]
       docker [ --help | -v | --version ]

A self-sufficient runtime for containers.

Options:

  --config=~/.docker              Location of client config files
  -D, --debug                     Enable debug mode
  -H, --host=[]                   Daemon socket(s) to connect to
  -h, --help                      Print usage
  -l, --log-level=info            Set the logging level
  --tls                           Use TLS; implied by --tlsverify
  --tlscacert=~/.docker/ca.pem    Trust certs signed only by this CA
  --tlscert=~/.docker/cert.pem    Path to TLS certificate file
  --tlskey=~/.docker/key.pem      Path to TLS key file
  --tlsverify                     Use TLS and verify the remote
  -v, --version                   Print version information and quit

Commands:
    attach    Attach to a running container
    build     Build an image from a Dockerfile
    commit    Create a new image from a container's changes
    cp        Copy files/folders between a container and the local filesystem
    create    Create a new container
    diff      Inspect changes on a container's filesystem
    events    Get real time events from the server
    exec      Run a command in a running container
    export    Export a container's filesystem as a tar archive
    history   Show the history of an image
    images    List images
    import    Import the contents from a tarball to create a filesystem image
    info      Display system-wide information
    inspect   Return low-level information on a container, image or task
    kill      Kill one or more running containers
    load      Load an image from a tar archive or STDIN
    login     Log in to a Docker registry.
    logout    Log out from a Docker registry.
    logs      Fetch the logs of a container
    network   Manage Docker networks
    node      Manage Docker Swarm nodes
    pause     Pause all processes within one or more containers
    port      List port mappings or a specific mapping for the container
    ps        List containers
    pull      Pull an image or a repository from a registry
    push      Push an image or a repository to a registry
    rename    Rename a container
    restart   Restart a container
    rm        Remove one or more containers
    rmi       Remove one or more images
    run       Run a command in a new container
    save      Save one or more images to a tar archive (streamed to STDOUT by default)
    search    Search the Docker Hub for images
    service   Manage Docker services
    start     Start one or more stopped containers
    stats     Display a live stream of container(s) resource usage statistics
    stop      Stop one or more running containers
    swarm     Manage Docker Swarm
    tag       Tag an image into a repository
    top       Display the running processes of a container
    unpause   Unpause all processes within one or more containers
    update    Update configuration of one or more containers
    version   Show the Docker version information
    volume    Manage Docker volumes
    wait      Block until a container stops, then print its exit code

Run 'docker COMMAND --help' for more information on a command.

What are the images that I have?

In [8]:
%%bash
docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
nginx                     latest              e43d811ce2f4        39 hours ago        181.4 MB
dock0/arch                latest              9b9558aeec5c        4 days ago          573.6 MB
alpine                    latest              baa5d63471ea        4 days ago          4.799 MB
nginx                     <none>              a5311a310510        10 days ago         181.4 MB
datadog/docker-dd-agent   latest              f385e05e1883        6 weeks ago         301.8 MB
node                      5-slim              c7a1798865e7        7 weeks ago         207.5 MB
ubuntu                    latest              bd3d4369aebc        8 weeks ago         126.6 MB
hello-world               latest              c54a2cc56cbb        3 months ago        1.848 kB
gophernet/mz              latest              71fa98ad2324        6 months ago        126.3 MB
networkstatic/iperf3      latest              6ea158fee1a7        7 months ago        125.5 MB
docker/whalesay           latest              6b362a9f73eb        17 months ago       247 MB

How to search for an image?

In [9]:
%%bash
docker search arch
NAME      DESCRIPTION   STARS     OFFICIAL   AUTOMATED

Let us get a new image

In [10]:
%%bash
docker pull dock0/arch
Using default tag: latest
latest: Pulling from dock0/arch
61a89ad7e283: Pulling fs layer
661b4793c245: Pulling fs layer
118193de9837: Pulling fs layer
ed763a71c2c2: Pulling fs layer
9958aa3aeac5: Pulling fs layer
e90db6f659ec: Pulling fs layer
ed763a71c2c2: Waiting
9958aa3aeac5: Waiting
e90db6f659ec: Waiting
61a89ad7e283: Verifying Checksum
61a89ad7e283: Download complete
61a89ad7e283: Pull complete
661b4793c245: Pull complete
118193de9837: Verifying Checksum
118193de9837: Pull complete
ed763a71c2c2: Verifying Checksum
ed763a71c2c2: Download complete
ed763a71c2c2: Pull complete
e90db6f659ec: Download complete
9958aa3aeac5: Download complete
9958aa3aeac5: Pull complete
e90db6f659ec: Pull complete
Digest: sha256:81ccc265c4bfafbf8fade70702c17c51f7831326b0efd8b240215ce7d0b2f5ee
Status: Downloaded newer image for dock0/arch:latest

Where are these images located?

In [11]:
from IPython.core.display import HTML
HTML('<a href="https://hub.docker.com/">Docker Hub</a>')
Out[11]:

Prefer Smaller Images

In [12]:
%%bash
docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
dock0/arch                latest              9fd5c23f509f        About an hour ago   575.5 MB
nginx                     latest              e43d811ce2f4        39 hours ago        181.4 MB
dock0/arch                <none>              9b9558aeec5c        4 days ago          573.6 MB
alpine                    latest              baa5d63471ea        4 days ago          4.799 MB
nginx                     <none>              a5311a310510        10 days ago         181.4 MB
datadog/docker-dd-agent   latest              f385e05e1883        6 weeks ago         301.8 MB
node                      5-slim              c7a1798865e7        7 weeks ago         207.5 MB
ubuntu                    latest              bd3d4369aebc        8 weeks ago         126.6 MB
hello-world               latest              c54a2cc56cbb        3 months ago        1.848 kB
gophernet/mz              latest              71fa98ad2324        6 months ago        126.3 MB
networkstatic/iperf3      latest              6ea158fee1a7        7 months ago        125.5 MB
docker/whalesay           latest              6b362a9f73eb        17 months ago       247 MB

Getting details of an image

In [13]:
%%bash
docker inspect alpine
[
    {
        "Id": "sha256:baa5d63471ead618ff91ddfacf1e2c81bf0612bfeb1daf00eb0843a41fbfade3",
        "RepoTags": [
            "alpine:latest"
        ],
        "RepoDigests": [
            "alpine@sha256:1354db23ff5478120c980eca1611a51c9f2b88b61f24283ee8200bf9a54f2e5c"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2016-10-18T20:31:22.321427771Z",
        "Container": "1d811a9194c47475510bc53700001c32f2b0eb8e3aca0914c5424109c0cd2056",
        "ContainerConfig": {
            "Hostname": "1d811a9194c4",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ADD file:7afbc23fda8b0b3872623c16af8e3490b2cee951aed14b3794389c2f946cc8c7 in / "
            ],
            "Image": "",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "DockerVersion": "1.12.1",
        "Author": "",
        "Config": {
            "Hostname": "1d811a9194c4",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": null,
            "Image": "",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 4799225,
        "VirtualSize": 4799225,
        "GraphDriver": {
            "Name": "devicemapper",
            "Data": {
                "DeviceId": "586",
                "DeviceName": "docker-8:2-7864838-4c329d72fcb914fb04aca59f7987f36f640fbab7837ad22187eaed6047ff5a5e",
                "DeviceSize": "10737418240"
            }
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:011b303988d241a4ae28a6b82b0d8262751ef02910f0ae2265cb637504b72e36"
            ]
        }
    }
]

Inspect the "inside" of an image

In [14]:
%%bash
docker history nginx
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
e43d811ce2f4        39 hours ago        /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon    0 B                 
<missing>           39 hours ago        /bin/sh -c #(nop)  EXPOSE 443/tcp 80/tcp        0 B                 
<missing>           39 hours ago        /bin/sh -c ln -sf /dev/stdout /var/log/nginx/   0 B                 
<missing>           39 hours ago        /bin/sh -c apt-key adv --keyserver hkp://pgp.   58.43 MB            
<missing>           39 hours ago        /bin/sh -c #(nop)  ENV NGINX_VERSION=1.11.5-1   0 B                 
<missing>           39 hours ago        /bin/sh -c #(nop)  MAINTAINER NGINX Docker Ma   0 B                 
<missing>           45 hours ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]            0 B                 
<missing>           45 hours ago        /bin/sh -c #(nop) ADD file:23aa4f893e3288698c   123 MB              

Deleting an image

docker rmi <image-tag>

Docker containers

We create a container using docker run
specifically:

docker run OPTIONS <<image-tag>> CMD ARGS

Let us ping a server

docker run alpine ping 8.8.8.8

How do we run a container interactively?

docker run -t -i ubuntu /bin/bash

-t Attach a pseudo-tty console
-i Stands for Interactive

Have you ever had the wild dream of running

rm -rf / --no-preserve-root ?????

Well, in docker you can!!

PLEASE PLEASE PLEASE BE VERY CAREFUL!

Running a container in the background

docker run -d ubuntu /bin/sh -c "while true; do echo current date and time is: $(date); sleep 2 ; done"

-d stands for detach

How do we see the logs of a container?

docker logs <Container-Tag>

Now that we know the basics...

Have you ever set up an HTTP server?

Was it easy?

Was it one-command easy?

We can do it with docker

Lets choose HTTP server

We choose Nginx

In [15]:
from IPython.display import IFrame
IFrame('https://en.wikipedia.org/wiki/Comparison_of_web_server_software#Overview', width=700, height=350)
Out[15]:

Is it in docker hub?

In [18]:
%%bash
docker search nginx
NAME      DESCRIPTION   STARS     OFFICIAL   AUTOMATED

Of course it is!

Let us run it!

docker run -d -p 80:80 nginx

-d stands for detach
-p stands for port. We basically say: "Redirect every incomming TCP packet at port 80 of the host, to the port 80 of the container"

Try it!

Open your browser at localhost:80

Pop quiz

What happens when you execute this on the command-line?

docker run debian /bin/sh

  1. A prompt from the shell of created container will be thrown to you
  2. A container is created and then exited immediately
  3. A container is created and executes in the detached mode; you can attach to it later using the container id
  4. Docker CLI issues the error: Error response from daemon: No command specified.
In [ ]: