Creando mi propia imagen para python

De Wiki de EGC
Saltar a: navegación, buscar

Podemos modificar una imagen existente y guardarla con un nombre diferente:

# docker run -it ubuntu bash
root@d66f7a3268e6:/#

Fijaros en que el prompt del interprete de ordenes ha cambiado e indica el numero que identifica de manera unica al contenedor que hemos lanzado. Este numero cambia para cada nueva instancia que lanzamos.

root@d66f7a3268e6:/# apt-get update
root@d66f7a3268e6:/# apt-get install apache2
apt-get install apache2
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils ...
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils ...
0 upgraded, 57 newly installed, 0 to remove and 4 not upgraded.
Need to get 22.6 MB/22.6 MB of archives.
After this operation, 102 MB of additional disk space will be used.
Do you want to continue? [Y/n] 
...
root@d66f7a3268e6:/#

Escribo 'Y' y pulso enter para comenzar la instalacion del servidor web apache2.

root@d66f7a3268e6:/# exit
# docker commit -m "Imagen de ubuntu con apache2" -a "profe" d66f7a3268e6 ubuntu/apache2
sha256:b68ef77405104a6ea2de6fde986a519a2fc9710b26d9ae8ec2553f5050c51fd3
# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu/apache2      latest              b68ef7740510        36 seconds ago      260MB
# docker run -it -p 8888:80 ubuntu/apache2
# service apache2 start
 * Starting Apache httpd web server apache2                                     AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
 *

Y ahora si abro un navegador web al localhost al puerto 8888, me aparece la pagina que indica que apache esta funcionando correctamente.

Creando una imagen para una app Flask en python

Comenzamos creando nuestra app. File holamundo.py

from flask import Flask  
app = Flask(__name__) 
@app.route('/') 
def hello_world():     
    return 'Hello World' 
@app.route('/hello/<name>')
def hello_name(name):
    return 'Hello %s!' % name
if __name__ == '__main__':      
    app.run()


Definimos las dependencias File requirements.txt

Flask

Podemos usar la imagen de python3 existente en los repositorios de docker, y modificar anadiendo en la carpeta apps la aplicacion que hemos desarrollado

Para ello podemos crear un fichero Dockerfile que nos automatiza la creacion de una imagen modificada:

# Base image 
FROM python:3 
#copy the requirements txt file with all our dependencies
COPY requirements.txt ./ 
#install the dependencies
RUN pip install --no-cache-dir -r requirements.txt  
#copy the app in the image
COPY holamundo.py ./  
#define a default command to execute
CMD [ "python", "./holamundo.py" ]

Este fichero indica que docker tiene que descargar la imagen python en su tercera versión y tiene que añadir el fichero requirements a la carpeta de usuario de la imagen.

Docker nos ofrece un lenguaje de scripting para automatizar la creacion de imagenes.

# docker build -t holamundo-flask .

Va a crear la imagen, tras esto podemos lanzar:

# docker run -it -p 5000:5000 holamundo-flask

Tras esto, desde un navegador, abrimos la URI: http://localhost:5000/hello/"yourname"