docker build --tag NAME-OF-THE-IMAGE:latest .
Para crearnos una imagen. A partir de esa imagen crearemos nuestro contenedor así:
docker run \
--publish 80:80 \
--volume PATH-TO-YOUR-PROJECT/src:/src \
--name "NAME-OF-THE-CONTAINER" \
NAME-OF-THE-IMAGE
Y eso levantará un servidor web que nos permitira depurar la web.
Tras una sesión de trabajo, podemos parar el contenedor con el siguiente comando:
docker container stop NOMBRE-DE-NUESTRO-CONTENEDOR
Y al otro día volver a arrancarlo con el comando:
docker container start NOMBRE-DE-NUESTRO-CONTENEDOR
#
# Dockerfile - this dockerfile runs apache and tomcat
#
FROM ubuntu:latest
# Evitar preguntas de tzdata, locales, etc.
ENV DEBIAN_FRONTEND=noninteractive
# Actualizar paquetes e instalar Apache + Tomcat + utilidades
RUN apt-get update
RUN apt-get install -y apache2 libapache2-mod-jk wget openjdk-21-jdk
# Java installation directory: /usr/lib/jvm/java-21-openjdk-amd64
# Instalar editores (por si hiciera falta)
RUN apt-get install joe nano
# Instalar Tomcat
ENV CATALINA_HOME=/opt/tomcat
ENV PATH=$CATALINA_HOME/bin:$PATH
RUN wget https://archive.apache.org/dist/tomcat/tomcat-11/v11.0.9/bin/apache-tomcat-11.0.9.tar.gz -O /tmp/tomcat.tar.gz
RUN mkdir -p $CATALINA_HOME
RUN tar xzf /tmp/tomcat.tar.gz -C $CATALINA_HOME --strip-components=1
RUN rm /tmp/tomcat.tar.gz
#
# habilitamos protocolo AJP en server.xml
#
RUN export AJP_SECRET=$(dd count=10 if=/dev/urandom 2>/dev/null | md5sum | awk '{print $1}')
RUN echo "AJP_SECRET=$AJP_SECRET"
ENV AJP_SECRET=$AJP_SECRET
RUN <<CHANGE_AWK_END
cat <<CHANGE_AWK_INTERNAL_END > /tmp/change.awk
BEGIN {
# ajp_secret must be a value received from command line
begin_of_comment_found = 0;
is_first_line = 1;
}
/.*/ {
line_2 = line_1;
line_1 = \$0;
if( match( line_2, "\s*<!--\s*" ) \
&& match( line_1, "protocol=\"AJP/1.3\"") ){
# line with the first comment is ommited
begin_of_comment_found = 1;
}else if( begin_of_comment_found \
&& match( line_2, "\s*-->\s*" ) ){
# line with the final --> is ommited
begin_of_comment_found = 0;
}else if( begin_of_comment_found \
&& match( line_2, "port" ) ) {
if( ajp_secret != "" ) {
print "\t\tsecretRequired=\"true\"";
print "\t\tsecret=\"" ajp_secret "\"";
}else{
print "\t\tsecretRequired=\"false\"";
}
print line_2;
}else if( match( line_2, "address" ) ){
; # do nothing, address must be ommited
# to allow listen on ipv4 AND ipv6
}else{
if( is_first_line && line_2 == "" ){
; # do nothing
}else
print line_2;
}
is_first_line = 0;
}
END {
print line_1;
}
CHANGE_AWK_INTERNAL_END
CHANGE_AWK_END
RUN cp $CATALINA_HOME/conf/server.xml /tmp/server.xml.orig
RUN awk -v ajp_secret=$AJP_SECRET -f /tmp/change.awk /tmp/server.xml.orig > $CATALINA_HOME/conf/server.xml
# RUN rm /tmp/change.awk
# Configuración de mod_jk
RUN <<WORKERS_PROPERTIES_EOF
cat <<WORKERS_PROPERTIES_EOF_INTERNAL > /etc/libapache2-mod-jk/workers.properties
#
# workers.properties
#
workers.tomcat_home=/opt/tomcat
workers.java_home=/usr/lib/jvm/java-21-openjdk-amd64
ps=/
worker.list=worker1
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
worker.worker1.secret=$AJP_SECRET
WORKERS_PROPERTIES_EOF_INTERNAL
WORKERS_PROPERTIES_EOF
#
# configuracion de módulo jk.conf
#
RUN <<JK_CONF_EOF
cat <<JK_CONF_EOF_INTERNAL > /etc/apache2/mods-available/jk.conf
<IfModule mod_jk.c>
JkWorkersFile /etc/libapache2-mod-jk/workers.properties
JkLogFile /var/log/apache2/mod_jk.log
JkLogLevel info
</IfModule>
JK_CONF_EOF_INTERNAL
JK_CONF_EOF
RUN <<CHANGE2_AWK_END
cat <<CHANGE2_AWK_INTERNAL_END > /tmp/change2.awk
/.*/ {
print \$0;
if( match( \$0, "CustomLog" ) ){
print "";
print " <IfModule mod_jk.c>";
print " JkMount / worker1";
print " JkMount /* worker1";
print " JkMount /servlet/* worker1";
print " </IfModule>";
}
}
CHANGE2_AWK_INTERNAL_END
CHANGE2_AWK_END
RUN cp /etc/apache2/sites-available/000-default.conf /tmp/000-default.conf
RUN awk -f /tmp/change2.awk /tmp/000-default.conf > /etc/apache2/sites-available/000-default.conf
RUN a2enmod jk
RUN if [ ! -f "/etc/apache2/mods-enabled/jk.load" ] ; then ln -s /etc/apache2/mods-available/jk.load /etc/apache2/mods-enabled/jk.load ; fi
RUN if [ ! -f "/etc/apache2/mods-enabled/jk.conf" ] ; then ln -s /etc/apache2/mods-available/jk.conf /etc/apache2/mods-enabled/jk.conf ; fi
RUN <<CHANGE3_AWK_END
cat <<CHANGE3_AWK_INTERNAL_END > /tmp/change3.awk
BEGIN {
host_found = 0;
localhost_found = 0;
end_found = 0;
}
/.*/ {
if( match( \$0, "<Host") )
host_found = 1;
if( match( \$0, "name=\"localhost\"" ) )
localhost_found = 1;
if( host_found && localhost_found && match( \$0, ">" ) )
end_found = 1;
print \$0;
if( host_found && localhost_found && end_found ){
print "";
print "\t<Context path=\"\" docBase=\"/src\"/>";
end_found = 0;
localhost_found = 0;
host_found = 0;
}
}
CHANGE3_AWK_INTERNAL_END
CHANGE3_AWK_END
RUN cp /opt/tomcat/conf/server.xml /tmp/server.xml
RUN awk -f /tmp/change3.awk /tmp/server.xml > $CATALINA_HOME/conf/server.xml
# Exponer puertos
EXPOSE 80
# uncomment for debug
# CMD /bin/bash
# optional cleanup after doing all the stuff
#RUN rm /tmp/change.awk
#RUN rm /tmp/change2.awk
#RUN rm /tmp/change3.awk
#RUN rm /tmp/server.xml
#RUN rm /tmp/000-default.conf
# Lanzar Apache y Tomcat en primer plano
CMD service apache2 start && \
$CATALINA_HOME/bin/catalina.sh run