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
Work:  Deutsche Telekom Chair of Communication Networks

Press esc for a menu of the slides

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

What images do we have?

In [8]:
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]:
docker search arch

Where are these images located?

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

Deleting an image

docker rmi <image-tag>

Docker containers

We create a container using docker run

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

Let us ping a server

docker run alpine ping

How do we run a container interactively?

docker run -t -i ubuntu /bin/bash

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

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 [1]:
from IPython.display import IFrame
IFrame('', width=700, height=350)

Is it in docker hub?

In [2]:
docker search nginx
NAME                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                     Official build of Nginx.                        4523      [OK]       
jwilder/nginx-proxy       Automated Nginx reverse proxy for docker c...   854                  [OK]
richarvey/nginx-php-fpm   Container running Nginx + PHP-FPM capable ...   301                  [OK]
million12/nginx-php       Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS...   75                   [OK]
webdevops/php-nginx       Nginx with PHP-FPM                              60                   [OK]
maxexcloo/nginx-php       Framework container with nginx and PHP-FPM...   58                   [OK]
h3nrik/nginx-ldap         NGINX web server with LDAP/AD, SSL and pro...   31                   [OK]
bitnami/nginx             Bitnami nginx Docker Image                      20                   [OK]
gists/nginx               Nginx on Alpine                                 8                    [OK]
evild/alpine-nginx        Minimalistic Docker image with Nginx            8                    [OK]
million12/nginx           Nginx: extensible, nicely tuned for better...   8                    [OK]
maxexcloo/nginx           Framework container with nginx installed.       7                    [OK]
webdevops/nginx           Nginx container                                 6                    [OK]
1science/nginx            Nginx Docker images based on Alpine Linux       4                    [OK]
drupaldocker/nginx        NGINX for Drupal                                3                    [OK]
ixbox/nginx               Nginx on Alpine Linux.                          3                    [OK]
webdevops/hhvm-nginx      Nginx with HHVM                                 3                    [OK]
dock0/nginx               Arch container running nginx                    2                    [OK]
servivum/nginx            Nginx Docker Image with Useful Tools            2                    [OK]
frekele/nginx             docker run --rm --name nginx -p 80:80 -p 4...   2                    [OK]
xataz/nginx               Light nginx image                               2                    [OK]
blacklabelops/nginx       Dockerized Nginx Reverse Proxy Server.          1                    [OK]
tozd/nginx                Dockerized nginx.                               1                    [OK]
unblibraries/nginx        Baseline non-PHP nginx container                0                    [OK]
c4tech/nginx              Several nginx images for web applications.      0                    [OK]

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

What is going on there?

The container is an HTTP server listening to port 80

How do we forward the traffic from the host to the container?

When we use the flag -p we "publish a port, or range of ports to the host".

Format: hostPort:containerPort

For UDP ports: hostPort:containerPort/udp

Let us map a different port in the host

Let us stop the server

First we need to find the container

docker ps

Then, we stop it

docker stop CONTAINER_ID

Let us also delete it

docker rm CONTAINER_ID

Check that the container is not there anymore. Go to localhost:80 in your browser. You should see an error

Note: Use incognito mode. Otherwise, the browser will display the cached site.

Let us make the new server

docker run -d -p 45700:80 nginx

Now, if go in your browser to localhost:4570

Making changes to an image

Go to and create an account

Run the image you want to modify

docker run -it ubuntu
  • Make the modifications
    apt update
    apt install inetutils-ping
  • Take notes of the container ID
    docker commit -a 'Juan' -m 'ping function' a122a23fd3bb  juancabre/ubu_lecture:ping

Lets push the image

  • Log in. You need sudo for this.
    sudo docker login
  • Push the image (use sudo docker push (USERNAME)/(NAME:[TAG])
    sudo docker push juancabre/ubu_lecture:ping
  • Go to and see your new image there

Repeat all the steps with a new function and a new tag

I will do it for vim

Lets pull the images

  • First lets remove the images
    sudo rmi juancabre/ubu_lecture:ping
  • And now we pull it
    docker pull juancabre/ubu_lecture:ping

Lets use :ping as a base image to create a new image with both functionalities

  • Run your image (juancabre/ubu_lecture:ping)
  • Install the new functionalities
  • Exit
  • Commit the changes WITHOUT A TAG
    docker commit -a 'Juan' -m 'ping function' a122a23fd3bb  juancabre/ubu_lecture
  • Push the changes WITHOUT A TAG
    sudo docker push juancabre/ubu_lecture
  • What does this do? The default tag is called :latest

Now I have 3 images with the same base image

  • ubu_lecture:ping (it can ping)
  • ubu_lecture:vim (it can edit text)
  • ubu_lecture:latest (it can ping and edit text)

Doing the same with a Dockerfile

# Base image
FROM ubuntu
# The maintainer
# The actual recipee
RUN apt update;\
    apt install -y vim
# Adding files
ADD /root/.vimrc
# The default command to run
CMD /bin/bash

Build it

docker build -t juancabre/dockerfile:vim_config .

Run it

docker run -it juancabre/dockerfile:vim_config

In [ ]: