Detail how to make your own JDK+tomcat images in docker

  • 2020-06-15 10:47:10
  • OfStack

If you, like me, want to make a hot image yourself, you might want to specify JDK and tomcat versions. Of course, that's ok.

At my level, there are currently two ways to make the image I want. Come on, let's start with something simple.

Method 1

First of all, get the jdk and tomcat you want, plus, we need to create 1 Dockerfile file, what, you said you don't know what Dockerfile is and can't write Dockerfile file? Oh, it doesn't matter, you Ctrl+C. The full contents of an Dockerfile file are shown below:

FROM     ubuntu:14.10

# the java with tomcat Add to container 
ADD jdk-8u31-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-8.0.20.tar.gz /usr/local/

# configuration java with tomcat The environment variable 
ENV JAVA_HOME /usr/local/jdk1.8.0_31
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.0.20
ENV CATALINA_BASE /usr/local/apache-tomcat-8.0.20

# The port on which the container is listening when it runs 

Of course, you still need to change 1 bit, which is jdk and tomcat version, then the corresponding path after version, the rest can not be changed.

Then, jdk and tomcat need to be in the same path as this file.

Then, call out the terminal in the current path and type

Docker build  � t linx/tomcat

This command follows the rules of the Dockerfile file to build the image. When the monkey is finished, run stands up and starts tomcat using the following command


OK, access port 8080 and you can see the familiar cat.

As a side note, the container opens port 8080, but you don't use port 8080 to access tomcat inside the container. Oh, you know, the container's port needs to be mapped to a port on the host.

#sudo docker run -d -p 5000:8080 training/webapp

Starting a container with the -ES48en parameter maps port 8080 to port 5000 of the host, so that you access port 5000 of the host to port 8080 of the container.

Also, the Dockerfile file actually helps you set the environment variables in the container, and so on. There are many more powerful things you can do.

Way 2

Although troublesome, still mention 1, after all also is very normal way.

First of all, next the empty mirror of any system, then run get up and go in, in fact, you are facing a host that has just installed the new system, so you get the idea, start downloading the jdk and tomcat you want, oh, originally your host has already installed, ok, so:

docker run -i -t -v /root/software/:/mnt/software/ 25c5298b1a36 /bin/bash

Launching the container this way allows you to mount your software folder to the container's mnt/software/ directory, so when you get inside the container, you'll see the jdk and tomcat you want.

Okay, let's unzip, let's set the environment variables. What, I don't know how to set the environment variable in Linux? Let's not do that.

vi ~/.bashrc And into the vi Edit mode 
 Add the following configuration at the end 
export JAVA_HOME=/opt/jdk/bin
 The input wq exit vi interface 
// Let environment variables take effect 
source ~/.bashrc

Above 1 paragraph linux set the environment variable method, for reference only, more ways more powerful functions you must look up the documentation!

So, we've set the environment variables for jdk and tomcat, right? Not yet? Oh, let's move on to the next step. At this time, the environment variable has been set in the container. You can enter ES84en-ES85en test 1. Then enter


Exit the container, the container is in a suspended state, and then

docker ps -a List all the containers 

See the container for exit, remember the container id, because we want to create a new image of ourselves based on the id, using the following command:

docker commit 57c312bbaad1 myName/javaweb

We then created a mirror named javaweb, where jdk and tomcat were specified.

Related articles: