Creando mi propia imágen

De Wiki de Sistemas Operativos
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 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.

Docker basado en la imagen de Ubuntu

Para instalar software en la imagen de Ubuntu, puedes utilizar el siguiente Dockerfile:

FROM ubuntu

RUN apt-get update
RUN apt-get install apache2 -yq
CMD service apache2 start
CMD bash

RUN se ejecuta en tiempo de construcción de la imagen, mientras que CMD se ejecuta en tiempo de ejecución (al lanzar la imagen via 'docker run').