Container – Part 1: Tomcat im Docker-Container

Categories: Allgemein, Linux, Netzwerk

Docker represents a pleasant possibility to create clearly defined, lightweight runtime environments (containers) and to distribute them if necessary. In the following, the creation and possible use is shown using the example of a container with Tomcat and OpenSuse.

The basic prerequisite for this article is knowledge of Linux, how Docker works and a functioning Docker environment. Docker installation will not be discussed in detail, but there are many good articles on the web that will lead to a successful installation on the platform of choice. This article uses a Docker installation on Windows 10, platform-specific elements (paths, etc.) may need to be adjusted appropriately.

Cration of a container

The basis for an executable container is a so-called image. This can be defined, for example, via the Dockerfile, which will be discussed in detail below.

The Dockerfile describes the individual aspects of the environment that is ultimately available in the container of the desired target application.

The starting point is the FROM element. It defines whether we want to build from scratch without a base or whether we want to use a base container, a base distribution (e.g. Opensuss Leap 15.2), for example.

FROM opensuse/leap:15.2

If you want to define a contact person for a Docker image, e.g. developers, etc., you can use the MAINTAINER element.

MAINTAINER "Rolf Rothamel nospam@example.com"

It is also helpful to add a short description to the Docker image with LABEL description, which is the primary use case for this container.

LABEL description="image for tomcat on opensuse"

After these rather “introductory” points, we are now getting closer to the individual customization of our container. For actions, commands that we want to execute based on the defined base container, the RUN command is available. Any changes made through this will become part of the Docker image we create. Here the base container remains unchanged, the changes overlay the base container. This is possible due to the layered model of the Docker image. For example, the base container represents the lowest layer. It is overlaid by the next layer, which contains the changes we make. In turn, if another image derives from ours, it also overlays a layer with its changes. Only the topmost, non-overlaid elements are visible. This is comparable with the OOP mechanism of overwriting e.g. with Java.

For our container we now need the additional installation of OpenJDK 11:

RUN zypper -n install java-11-openjdk

As a next step we need a suitable directory in which Tomcat should be unpacked.

RUN mkdir /opt/tomcat

In this example, we already have the appropriate Tomcat version available during the subsequent build of the image, alternatively it can be loaded from the net using wget.

The ADD command adds the local file to the file system of the image build process. If it is an archive file, as in this case, it is also available in an unpacked state.

ADD ./apache-tomcat-9.0.5.tar.gz /opt/apache-tomcat-9.0.5.tar.gz

If we now want to access the Tomcat contained in the archive and copy it to its destination, we must extend the path accordingly with the file name:

RUN cp -rf /opt/apache-tomcat-9.0.5.tar.gz/apache-tomcat-9.0.5/* /opt/tomcat/

Other Tomcat config files can become part of the Docker container in the same way. E.g. the server.xml

ADD ./server.xml /opt/tomcat/conf/server.xml

A web application that is to be executed in Tomcat later can also be added during image creation with ADD:

ADD ./webapp.war /opt/tomcat/webapp/

Once the desired adjustments and extensions have been made, the ports that are open to the outside must be defined:

EXPOSE 8080
EXPOSE 8009

and specify which application should be executed when the container is started:

ENTRYPOINT [ "/opt/tomcat/bin/catalina.sh" ]
CMD [ "run" ]

Now we have created a Dockerfile that can act as a base for various Tomcat web applications.

To create a Docker image, we run a build in the directory where the Dockerfile file is located:

docker build -t opensuse-comcat .

In part 2, I go over Docker’s command line interface and show a helpful parameter for running and runtime configuration of a container.

Example Dockerfile

FROM opensuse/leap:15.2
MAINTAINER "Rolf Rothamel nospam@example.com"
LABEL description="image for tomcat on opensuse"

RUN zypper -n install java-11-openjdk
RUN mkdir /opt/tomcat

ADD ./apache-tomcat-9.0.5.tar.gz /opt/tomcat-down/apache-tomcat-9.0.5.tar.gz
ADD ./server.xml /opt/tomcat/conf/server.xml
ADD ./webapp.war /opt/tomcat/webapp/

EXPOSE 8080
EXPOSE 8009

ENTRYPOINT [ "/opt/tomcat/bin/catalina.sh" ]
CMD [ "run" ]

«
»

    Leave a Reply

    Your email address will not be published. Required fields are marked *