Try   HackMD

Lab 8 - Docker - Creating an image

tags: LAB

Note: This lab is based on Docker Tutorials and Labs.

Podeis utilizar Docker en vuestra maquina instalandolo siguendo las indicaciones que están aqui: https://docs.docker.com/get-docker/

O utilizar la maquina virtual que està en Poliformat/RSE: Recursos/Laboratorio/código practicas laboratorio

O online: https://labs.play-with-docker.com/#

All the code necessary for this Lab session is available in Poliformat/RSE: Recursos/Laboratorio/código practicas laboratorio, or here: https://bit.ly/codigoRSE2021

STEP 1: creating a web page using Flask

The goal of this block is to create a Docker image which will run a Flask app that displays a random pizza .gif every time it is loaded.

We'll do this by first pulling together the components for a random pizza picture generator built with Python Flask, then dockerizing it by writing a Dockerfile. Finally, we'll build the image, and then run it.

We need the following three files:

  • app.py
  • templates/index.html
  • Dockerfile

The files can be found in the repository in POLIFORMAT as indicated above.

1.- Describe linea por linea el contenido del fichero Dockerfile

Apunta el resultado en el documento a entregar.

Build the image

The docker build command does the heavy-lifting of creating a docker image from a Dockerfile.

When you run the docker build command given below, make sure to replace <YOUR_USERNAME> with your Docker username, the one you used when registering on Docker Hub. (If you havent registered yet do it now.)

The docker build command is quite simple - it takes an optional tag name with the -t flag, and the location of the directory containing the Dockerfile - the . indicates the current directory:

$ docker build -t <YOUR_USERNAME>/myfirstapp .

the generated output should be something similar to:

 => [internal] load build definition from Dockerfile                        0.0s
 => => transferring dockerfile: 588B                                        0.0s
 => [internal] load .dockerignore                                           0.0s
 => => transferring context: 2B                                             0.0s
 => [internal] load metadata for docker.io/library/alpine:latest            2.2s
 => [auth] library/alpine:pull token for registry-1.docker.io               0.0s
 => [internal] load build context                                           0.0s
 => => transferring context: 2.56kB                                         0.0s
 => [1/6] FROM docker.io/library/alpine@sha256:a75afd8b57e7f34e4dad8d65e2c  0.7s
 => => resolve docker.io/library/alpine@sha256:a75afd8b57e7f34e4dad8d65e2c  0.0s
 => => sha256:4661fb57f7890b9145907a1fe2555091d333ff3d28db86c3 528B / 528B  0.0s
 => => sha256:28f6e27057430ed2a40dbdd50d2736a3f0a295924016 1.47kB / 1.47kB  0.0s
 => => sha256:ba3557a56b150f9b813f9d02274d62914fd8fce120dd 2.81MB / 2.81MB  0.5s
 => => sha256:a75afd8b57e7f34e4dad8d65e2c7ba2e1975c795ce1e 1.64kB / 1.64kB  0.0s
 => => extracting sha256:ba3557a56b150f9b813f9d02274d62914fd8fce120dd374d9  0.2s
 => [2/6] RUN apk add --update py3-pip                                      4.2s
 => [3/6] RUN pip install --upgrade pip                                     3.0s 
 => [4/6] RUN pip install -U Flask                                          2.2s 
 => [5/6] COPY app.py /usr/src/app/                                         0.0s 
 => [6/6] COPY templates/index.html /usr/src/app/templates/                 0.0s 
 => exporting to image                                                      0.6s 
...
Successfully built 2f7357a0805d

If everything went well, your image should be ready! Run:

$ docker image ls

and see if your image (<YOUR_USERNAME>/myfirstapp) shows.

Run your image

The next step in this section is to run the image and see if it actually works.

$ docker run -p 8888:5000 --name myfirstapp YOUR_USERNAME/myfirstapp

2.- Explica cual es el suo del parametro -p 8888:5000

Apunta el resultado en el documento a entregar.

Open a browser with the URL http://localhost:8888 and your app should be live.

Hit the Refresh button in the web browser to see a few more pizza images.

Push your image

Now that you've created and tested your image, you can push it to Docker Hub.

First you have to login to your Docker Cloud account, to do that:

$ docker login

Enter YOUR_USERNAME and password when prompted.

Now all you have to do is:

$ docker push YOUR_USERNAME/myfirstapp

3.- Apunta el nombre completo de tu imagen en el documento a entregar.

STEP 2: creating an image of a generic MQTT consumer

4.- Tienes que crear el Dockerfile necesario para implementar un subscriber al broker broker.hivemq.com para los mensajes con topic test/#. Como imagen base puedes utilizar alpine y como codigo python base para el subscriber puedes reutilizar el sisub.py de las sesiones de IoT .

Su ejecución tiene que dar algo asi:

$ docker run -it YOUR_USERNAME/thesub
    ('Connected to ', 'broker.hivemq.com', 'port: ', 1883)
    ('Flags: ', {'session present': 0}, 'returned code: ', 0)
    sisub: msg received with topic: test/point and payload: 0.0
    sisub: msg received with topic: test/esp/j4y0m2/Long and payload: 0.0000000
    sisub: msg received with topic: test/1 and payload: test-one
    sisub: msg received with topic: test/esp/j4y0m2/LAT and payload: 0.0000000
...

En el documento a entregar tienes que adjuntar:

  1. el fichero `Dockerfile,
  2. el fichero python del subscriber, y
  3. el nombre de la imagen que has subido al Docker hub