viernes, 21 de junio de 2019

Docker Containers Part2 Lab Python Flask Redis

Creación de Contenedor con aplicación http Python Flask Redis

Probamos la instalación de docker sea correcta

bext@bext-VPCF13WFX:~/DockerAppConfig/DockerSampleApp$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8
Status: Downloaded newer image for hello-world:latest

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.
    (amd64)
 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 ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

El projecto que se va a subir al contenedor consta de tres archivos
Dockerfile: que describe la construcción de la imagen docker.
      app.py: que es la aplicación html, que usa phyton, con Flask como
                   Framework, y redis para base de datos.
requirements.txt: contiene los paquetes que instalara para python con pip.


En la vista se puede observar una panoramica del projecto python.

Probamos la aplicación python en máquina local.
bext@bext-VPCF13WFX:~/DockerAppConfig/DockerSampleApp$ python app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:4000/ (Press CTRL+C to quit)

Desde el browser veremos que el nombre del localhost corresponde al de la máquina local, y se incrementa el número de visitas.


Vemos el impacto de los dos request
bext@bext-VPCF13WFX:~/DockerAppConfig/DockerSampleApp$ python app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:4000/ (Press CTRL+C to quit)
127.0.0.1 - - [21/Jun/2019 20:10:38] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [21/Jun/2019 20:11:28] "GET / HTTP/1.1" 200 -


Construimos la imagen docker.
bext@bext-VPCF13WFX:~/DockerAppConfig/DockerSampleApp$ docker build --tag=python_app .
Sending build context to Docker daemon  20.48kB
Step 1/7 : FROM python:2.7-slim
2.7-slim: Pulling from library/python
fc7181108d40: Pull complete 
8c60b810a35a: Pull complete 
d207b275197c: Pull complete 
63184f224d60: Pull complete 
Digest: sha256:1405fa2f8e9a232e2f60cafbb2b06ca2f1e0f577f4b4c397c361d6dba59fd24e
Status: Downloaded newer image for python:2.7-slim
 ---> ca96bab3e2aa
Step 2/7 : WORKDIR /app
 ---> Running in aeab2488bb81
Removing intermediate container aeab2488bb81
 ---> a0af2215bb76
Step 3/7 : COPY . /app
 ---> 65c2721d2558
Step 4/7 : RUN pip install --trusted-host pypi.python.org -r requirements.txt
 ---> Running in d9055f7d946e
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting flask (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/9a/74/670ae9737d14114753b8c8fdf2e8bd212a05d3b361ab15b44937dfd40985/Flask-1.0.3-py2.py3-none-any.whl (92kB)
Collecting redis (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/ac/a7/cff10cc5f1180834a3ed564d148fb4329c989cbb1f2e196fc9a10fa07072/redis-3.2.1-py2.py3-none-any.whl (65kB)
Collecting itsdangerous>=0.24 (from flask->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Jinja2>=2.10 (from flask->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl (124kB)
Collecting Werkzeug>=0.14 (from flask->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/9f/57/92a497e38161ce40606c27a86759c6b92dd34fcdb33f64171ec559257c02/Werkzeug-0.15.4-py2.py3-none-any.whl (327kB)
Collecting click>=5.1 (from flask->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->flask->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/fb/40/f3adb7cf24a8012813c5edb20329eb22d5d8e2a0ecf73d21d6b85865da11/MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
Installing collected packages: itsdangerous, MarkupSafe, Jinja2, Werkzeug, click, flask, redis
Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.4 click-7.0 flask-1.0.3 itsdangerous-1.1.0 redis-3.2.1
Removing intermediate container d9055f7d946e
 ---> 09637f2e02fc
Step 5/7 : EXPOSE 4000
 ---> Running in 427e82fd31d8
Removing intermediate container 427e82fd31d8
 ---> d94ae6b2d53d
Step 6/7 : ENV NAME Mundito (env var definido en Dockerfile)
 ---> Running in ef1030ab55c9
Removing intermediate container ef1030ab55c9
 ---> c11af545add6
Step 7/7 : CMD ["python","app.py"]
 ---> Running in 8f2d4143b72a
Removing intermediate container 8f2d4143b72a
 ---> 68e8339a3346
Successfully built 68e8339a3346
Successfully tagged python_app:latest
bext@bext-VPCF13WFX:~/DockerAppConfig/DockerSampleApp$ 

Checamos las imagenes docker
bext@bext-VPCF13WFX:~/DockerAppConfig/DockerSampleApp$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python_app          latest              68e8339a3346        4 minutes ago       131MB
python              2.7-slim            ca96bab3e2aa        10 days ago         120MB
hello-world         latest              fce289e99eb9        5 months ago        1.84kB

Corremos la imagen docker reasignando el puerto 4000 original de la aplicación python a el puerto 5000 que consumirá desde la máquina local

bext@bext-VPCF13WFX:~/DockerAppConfig/DockerSampleApp$ docker run -p 5000:4000 python_app
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:4000/ (Press CTRL+C to quit)
172.17.0.1 - - [22/Jun/2019 01:20:06] "GET / HTTP/1.1" 200 -

Podemos observar que el valor de la variable de entorno NAME, la toma de la configuración que se le indicó en el Dockerfile, y el hostname es el de la máquina del contenedor docker, no se puede accesar al servidor redis.

Desde otra terminal podemos ver el proceso corriendo del contenedor
bext@bext-VPCF13WFX:~$ docker ps
CONTAINER ID  IMAGE       COMMAND          CREATED            STATUS              PORTS                    NAMES
249d886cd6b9  python_app  "python app.py"  About a minute ago Up About a minute   0.0.0.0:5000->4000/tcp   unruffled_brattain

bext@bext-VPCF13WFX:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS                    NAMES
249d886cd6b9        python_app          "python app.py"     9 minutes ago       Up 9 minutes                   0.0.0.0:5000->4000/tcp   unruffled_brattain
9e71a2aaba38        hello-world         "/hello"            About an hour ago   Exited (0) About an hour ago                            wizardly_swartz

Alternativamente podemos ver el los procesos con la herramienta ctop


Ahora detendremos los contenedores y los borraremos.

bext@bext-VPCF13WFX:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
cd9e83defb99        python_app          "python app.py"     5 minutes ago       Exited (0) About a minute ago                       cocky_shirley
249d886cd6b9        python_app          "python app.py"     17 minutes ago      Exited (0) 7 minutes ago                            unruffled_brattain
9e71a2aaba38        hello-world         "/hello"            About an hour ago   Exited (0) About an hour ago                        wizardly_swartz
bext@bext-VPCF13WFX:~$ docker stop $(docker ps -aq)
cd9e83defb99
249d886cd6b9
9e71a2aaba38
bext@bext-VPCF13WFX:~$ docker rm $(docker ps -aq)
cd9e83defb99
249d886cd6b9
9e71a2aaba38
bext@bext-VPCF13WFX:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
bext@bext-VPCF13WFX:~$ 

Ahora compartiremos la imagen por medio del repositorio de docker

bext@bext-VPCF13WFX:~/DockerAppConfig/DockerSampleApp$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python_app          latest              68e8339a3346        About an hour ago   131MB
python              2.7-slim            ca96bab3e2aa        11 days ago         120MB
hello-world         latest              fce289e99eb9        5 months ago        1.84kB
bext@bext-VPCF13WFX:~/DockerAppConfig/DockerSampleApp

Accedemos al repositorio docker
bext@bext-VPCF13WFX:~/DockerAppConfig/DockerSampleApp$ docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /home/bext/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
bext@bext-VPCF13WFX:~/DockerAppConfig/DockerSampleApp$ 

Etiquetamos la imagen que subiremos al repositorio, previamente ya debimos rear el repositorio donde subiremos la imagen python_app






 Etiquetamos la imagen
bext@bext-VPCF13WFX:~/DockerAppConfig/DockerSampleApp$ docker tag python_app jalbertomr/lab1:python_app_1
bext@bext-VPCF13WFX:~/DockerAppConfig/DockerSampleApp$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jalbertomr/lab1     python_app_1        68e8339a3346        About an hour ago   131MB
python_app          latest              68e8339a3346        About an hour ago   131MB
python              2.7-slim            ca96bab3e2aa        11 days ago         120MB
hello-world         latest              fce289e99eb9        5 months ago        1.84kB

La subimos
bext@bext-VPCF13WFX:~/DockerAppConfig/DockerSampleApp$ docker push jalbertomr/lab1:python_app_1
The push refers to repository [docker.io/jalbertomr/lab1]
fcce160d5a7c: Pushed 
d3b05a658449: Pushed 
c2a45b91534b: Pushed 
a212ef9c5ee1: Pushed 
a47fa5565167: Mounted from library/python 
658556256f47: Mounted from library/python 
cf5b3c6798f7: Mounted from library/python 
python_app_1: digest: sha256:ce8c79d3c0bedbbf7b4c890581481ab90440fbd9005bf47da04059a21149d553 size: 1789
bext@bext-VPCF13WFX:~/DockerAppConfig/DockerSampleApp$ 

 Verificamos en el repositorio de docker


OK. 

https://github.com/jalbertomr/DockerSampleApp.git

eot

No hay comentarios:

Publicar un comentario