Install Docker and Multiple Tomcats

Lasitha Benaragama
4 min readMar 29, 2018

Docker, If you are not familiar with docker below tutorial will give an good understanding about it. And it is cool as a cucumber.

Don't know how to start? first setup a brand new Ubuntu server machine with an internet connection and follow instructions in below document.

As a Java web developer your basic needs will be

  • Java
  • Build tool
  • Application Server

If you need something more than that, please raise your point and i will help you to overcome the situation.

Docker basically provides containers for each and every applications. This is kind of like a virtual machine but not a virtual machine. Please follow above link and then you can get a clear understanding.

lets start,

First create a Java Docker.

Create a directory inside your ~ (home) directory called docker and create a file named “JavaVM.docker” and add below content .

# Dockerfile

FROM phusion/baseimage:0.9.17

MAINTAINER Author Name <lasitha@finnovation.lk>

RUN echo “deb http://archive.ubuntu.com/ubuntu trusty main universe” > /etc/apt/sources.list
RUN apt-get -y update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y -q python-software-properties software-properties-common

ENV JAVA_VER 8
ENV JAVA_HOME /usr/lib/jvm/java-8-oracle

RUN echo ‘deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main’ >> /etc/apt/sources.list && \
echo ‘deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main’ >> /etc/apt/sources.list && \
apt-key adv — keyserver keyserver.ubuntu.com — recv-keys C2518248EEA14886 && \
apt-get update && \
echo oracle-java${JAVA_VER}-installer shared/accepted-oracle-license-v1–1 select true | sudo /usr/bin/debconf-set-selections && \
apt-get install -y — force-yes — no-install-recommends oracle-java${JAVA_VER}-installer oracle-java${JAVA_VER}-set-default && \
apt-get clean && \
rm -rf /var/cache/oracle-jdk${JAVA_VER}-installer

RUN update-java-alternatives -s java-8-oracle
RUN echo “export JAVA_HOME=/usr/lib/jvm/java-8-oracle” >> ~/.bashrc
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

CMD [“/sbin/my_init”]

Save the file. (extension is not required, i added it to make my life easy.

Then create docker image.

docker build -f JavaVM.docker -t qa/oracle-java:8 .

qa/oracle-java is a name of the image and 8 is the tag of the image.

Now test the Image

Remember how you start Java? Create that exact Hello World Java file and test it. I will post the code for your convenience.

public class Main{
public static void main(String[] args) {
System.out.println("Hello, World");
}
}

Remember? First you need to compile it, then run it.

docker run --rm -v $PWD:/app -w /app qa/oracle-java:8 javac Main.javadocker run --rm -v $PWD:/app -w /app qa/oracle-java:8 java Main

Alright,

We will move in to tomcat containers.

Creating tomcat instances is one commands away. below is the command.

docker run -d -p 8080:8080 --name qaTomcat tomcat

  • -d — instruct docker to run in background.
  • -p — are the ports to run tomcat.
  • — name is the name of the instance.

There is a inbuilt image for the Tomcat container. “tomcat” is the name of that container.

To run another tomcat instance, issue same command with different name and the port.

docker run -d -p 8081:8080 --name uatTomcat tomcat

Important commands.

  • docker ps — See a list of all the running Docker containers. Add -a to see all the containers.
  • docker stop qaTomcat — Stops the container named qaTomcat.
  • docker start qaTomcat — Starts the container named qaTomcat.
  • docker rm qaTomcat — Remove the container named qaTomcat.
  • docker logs qaTomcat — Shows the container named qaTomcat logs.

So Now the tomcats are ready. you can see it when you type

XX.XX.XXX.XX:8080 and XX.XX.XXX.XX:8081 in your browser.

Now the problems? You definitely need to customize the tomcats. You cant run it as it is 😮. The Solution is simple and straightforward. Docker provided a facility to login each container separately. Here is how to do it.

First login to docker instance.

docker exec -it qaTomcat bash

Update it.

apt-get update

Install vim

apt-get install vim

and change the config files using vim

vim conf/tomcat-users.xml

The next problem. When you login to your docker instance, It don't have access to your host files. so you need to share one of the host directory when you creating the instance.

We will start by removing created instances.

docker rm qaTomcat
docker rm uatTomcat

Then create instances again by issuing below commands.

  • sudo docker run -d -p 8080:8080 — name qaTomcat -v /opt/wars:/wars tomcat
  • sudo docker run -d -p 8081:8080 — name uatTomcat -v /opt/wars:/wars tomcat

it is all about “-v /opt/wars:/wars” This will basically map the host machine /opt/wars directory to /wars directory in your docker container. after login to docker container you can access it using /wars directory path.

Please Note — If you remove docker instance applied changes will go away forever. You have to update the instance and install all the applications again. If you need to restart tomcat, restart the container, do not follow “remove and add again” methodology.

So That’s it. Happy Dockering. Just ask if you face any difficulties. I will help.

--

--