Diferencia entre revisiones de «Creando mi propia imagen»

De Wiki de EGC
Saltar a: navegación, buscar
 
(No se muestran 8 ediciones intermedias de otro usuario)
Línea 3: Línea 3:
 
<syntaxhighlight lang="sh">
 
<syntaxhighlight lang="sh">
 
# docker run -it ubuntu bash
 
# 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>
  
 
<syntaxhighlight lang="sh">
 
<syntaxhighlight lang="sh">
# apt-get install apache2
+
# 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>
 
</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 de Tomcat =
 +
 +
Podemos usar la imagen de tomcat existente en los repositorios de docker, y modificar anadiendo en la carpeta webapps la aplicacion que hemos desarrollado y que distribuimos en formato fichero .war
 +
 +
Para ello podemos crear un fichero Dockerfile que nos automatiza la creacion de una imagen modificada:
  
 
<syntaxhighlight lang="sh">
 
<syntaxhighlight lang="sh">
# exit
+
FROM tomcat:8-jre8
 +
 
 +
ADD target/hello-java-0.1.0.war /usr/local/tomcat/webapps
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Este fichero indica que docker tiene que descargar la imagen tomcat y tiene que anadir el fichero hello-java-0.1.0.war a la carpeta /usr/local/tomcat/webapps.
 +
 +
Docker nos ofrece un lenguaje de scripting para automatizar la creacion de imagenes.
 +
 +
<syntaxhighlight lang="sh">
 +
# docker build -t holamundo-java .
 +
</syntaxhighlight>
 +
 +
Va a crear la imagen, tras esto podemos lanzar:
  
 
<syntaxhighlight lang="sh">
 
<syntaxhighlight lang="sh">
# docker commit -m "Imagen de ubuntu con apache2" -a "profe" XXXXXX ubuntu/apache2
+
# docker run -it -p 8080:8080 holamundo-java
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Tras esto, desde un navegador, abrimos la URI: http://localhost:8080/hello-java-0.1.0/greeting.

Revisión actual del 19:21 8 nov 2018

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 de Tomcat

Podemos usar la imagen de tomcat existente en los repositorios de docker, y modificar anadiendo en la carpeta webapps la aplicacion que hemos desarrollado y que distribuimos en formato fichero .war

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

FROM tomcat:8-jre8

ADD target/hello-java-0.1.0.war /usr/local/tomcat/webapps

Este fichero indica que docker tiene que descargar la imagen tomcat y tiene que anadir el fichero hello-java-0.1.0.war a la carpeta /usr/local/tomcat/webapps.

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

# docker build -t holamundo-java .

Va a crear la imagen, tras esto podemos lanzar:

# docker run -it -p 8080:8080 holamundo-java

Tras esto, desde un navegador, abrimos la URI: http://localhost:8080/hello-java-0.1.0/greeting.