Diferencia entre revisiones de «Creando mi propia imagen para python»

De Wiki de EGC
Saltar a: navegación, buscar
(Página creada con «Podemos modificar una imagen existente y guardarla con un nombre diferente: <syntaxhighlight lang="sh"> # docker run -it ubuntu bash root@d66f7a3268e6:/# </syntaxhighligh...»)
 
(Página blanqueada)
 
(No se muestran 5 ediciones intermedias de 2 usuarios)
Línea 1: Línea 1:
−
Podemos modificar una imagen existente y guardarla con un nombre diferente:
 
  
−
<syntaxhighlight lang="sh">
 
−
# docker run -it ubuntu bash
 
−
root@d66f7a3268e6:/#
 
−
</syntaxhighlight>
 
−
 
−
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.
 
−
 
−
<syntaxhighlight lang="sh">
 
−
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:/#
 
−
</syntaxhighlight>
 
−
 
−
Escribo 'Y' y pulso enter para comenzar la instalacion del servidor web apache2.
 
−
 
−
<syntaxhighlight lang="sh">
 
−
root@d66f7a3268e6:/# exit
 
−
</syntaxhighlight>
 
−
 
−
<syntaxhighlight lang="sh">
 
−
# docker commit -m "Imagen de ubuntu con apache2" -a "profe" d66f7a3268e6 ubuntu/apache2
 
−
sha256:b68ef77405104a6ea2de6fde986a519a2fc9710b26d9ae8ec2553f5050c51fd3
 
−
</syntaxhighlight>
 
−
 
−
<syntaxhighlight lang="sh">
 
−
# docker images
 
−
REPOSITORY          TAG                IMAGE ID            CREATED            SIZE
 
−
ubuntu/apache2      latest              b68ef7740510        36 seconds ago      260MB
 
−
</syntaxhighlight>
 
−
 
−
<syntaxhighlight lang="sh">
 
−
# 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
 
−
*
 
−
</syntaxhighlight>
 
−
 
−
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 =
 
−
 
−
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:
 
−
 
−
<syntaxhighlight lang="sh">
 
−
FROM python:3
 
−
COPY requirements.txt ./
 
−
COPY .myPythonScript.py ./
 
−
</syntaxhighlight>
 
−
 
−
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.
 
−
 
−
<syntaxhighlight lang="sh">
 
−
# docker build -t holamundo-flask .
 
−
</syntaxhighlight>
 
−
 
−
Va a crear la imagen, tras esto podemos lanzar:
 
−
 
−
<syntaxhighlight lang="sh">
 
−
# docker run -it -p 5000:5000 holamundo-flask
 
−
</syntaxhighlight>
 
−
 
−
Tras esto, desde un navegador, abrimos la URI: http://localhost:5000/hello/"yourname"
 

Revisión actual del 19:18 18 nov 2020