How to build a JAVA Tomcat running environment under docker, have you tried?

  docker, question

I am learning Docker recently and want to build a java tomcat environment. Do you know? The more detailed the better, thanks first.

I think this blog has made your question very clear. You can take a look at it carefully.

Docker aims to provide an automated deployment solution for applications. It quickly creates a container (lightweight virtual machine) on Linux system and deploys and runs applications. It is very convenient to automatically install, deploy and upgrade applications through configuration files. Because the container is used, it is very convenient to separate the production environment from the development environment without affecting each other. This is the most common way to play docker. More games include large-scale web applications, database deployment, continuous deployment, clustering, testing environment, service-oriented cloud computing, virtual desktop VDI, etc.

Subjective impression: Docker is written in Go language, resources are isolated by cgroup, and container technology adopts LXC. It provides a lightweight virtualization solution that can independently run Unix processes. It provides a way to automatically deploy software in a secure and repeatable environment. LXC command is somewhat complicated. If you are interested, here is an article I wrote earlier based on LXC (from scratch, build a simple version of JAVA PAAS cloud platform), which can be reviewed in advance.

The implementation principle, relevant theories, application scenarios, etc. will be written later in this series. Here, let’s try to build a Tomcat operating environment based on Docker by hand. First come out a decent Demo, which can see the effect and may let us go further.


In all environments of this article, ubuntu-13.10-server-amd64 is running on VMware WorkStation. Note that it is a 64-bit system. In theory, other virtual machines are also completely feasible.

Install doc

Docker version 0.7 requires linux kernel 3.8 support and AUFS file system.

Check if AUFS is installed

sudo apt-get update
sudo apt-get install linux-image-extra-uname -r

Add Docker repository key

sudo sh -c “wget -qO-| apt-key add -“

Add a Docker repository and install Docker

sudo sh -c “echo deb main > /etc/apt/sources.list.d/docker.list”
sudo apt-get update
sudo apt-get install lxc-docker

Check if Docker has been installed successfully

sudo docker version

Terminal output Client version: 0.7.1

Go version (client): go1.2
Git commit (client): 88df052
Server version: 0.7.1
Git commit (server): 88df052
Go version (server): go1.2
Last stable version: 0.7.1
To get rid of sudo

Under Ubuntu, it is very tiring to enter sudo and password every time you execute the docker. here, fine-tune and add the current user’s execution permission to the corresponding Docker user group.

Add a new docker User Group

sudo groupadd docker

Add the current user to the docker user group, and notice that yongboy here is the login user name of ubuntu server.

sudo gpasswd -a yongboy docker

Restart Docker Background Monitoring Process

sudo service docker restart

After restarting, try to see if it takes effect

docker version

If it has not yet taken effect, the system will restart and take effect.

sudo reboot
Install a Docker Run Instance -ubuntu Virtual Machine

After the Docker installation is completed, the background process is automatically started, and the virtual machine instance can be installed (here take the learn/tutorial image used in the official demonstration as an example directly):

docker pull learn/tutorial
After the installation is completed, see the effect.

docker run learn/tutorial /bin/echo hello world
Interactive access to newly installed virtual machines

docker run -i -t learn/tutorial /bin/bash
You will see:

Indicates that you have entered an interactive environment.

Install SSH terminal server to facilitate our external login and access using SSH client.

apt-get update
apt-get install openssh-server
which sshd
mkdir /var/run/sshd
Passwd # Enter the user password, I set it to 123456 here, which is convenient for SSH client login
Exit #
Gets the instance container ID of the operation just now

docker ps -l

51774a81beb3 learn/tutorial:latest /bin/bash 3 minutes ago Exit 0 thirsty_pasteur
It can be seen that the container ID of the current operation is 51774a81beb3. Note that once all operations are performed, they need to be submitted and saved for SSH login:

docker commit 51774a81beb3 learn/tutorial
Run this mirror instance for a long time as a background process:

docker run -d -p 22 -p 80:8080 learn/tutorial /usr/sbin/sshd -D
SSH Server running in ubuntu container occupies 22 ports and -p 22 specifies. -p 80:8080 means that ubuntu will run tomcat on port 8080, but the port mapped to the outside (outside the container) is 80.

At this time, check to see if it runs successfully.

docker ps

871769a4f5ea learn/tutorial:latest /usr/sbin/sshd -D About a minute ago Up About a minute>22/tcp,>8080/tcp focused_poincare
Note that the randomly assigned SSH connection port number here is 49154:

ssh root@ -p 49154
Enter a passable password, is it possible to enter? Once you control SSH, the rest is very simple, install JDK, install tomcat, etc., as you wish. The following is the installation script:

Installing oracle jdk 7 on ubuntu 12.04

apt-get install python-software-properties
add-apt-repository ppa:webupd8team/java
apt-get update
apt-get install -y wget
apt-get install oracle-java7-installer
java -version

Download tomcat 7.0.47


Unzip, run

tar xvf apache-tomcat-7.0.47.tar.gz
cd apache-tomcat-7.0.47
By default, tomcat will occupy port 8080. Just now, when starting the mirror instance, it specified -p 80:8080, ubuntu mirror instance/container, open port 8080, and map to host port 80. If you know the IP address of the host, you can access it freely. On the host machine, pass the curl test:

Of course, you can also use the browser to access it.

In fact, tomcat may not be directly opened to the outside world at port 80, which is usually located behind nginx/apache or firewall. The above is for demonstration only.


With the help of Docker, a Tomcat runtime environment is set up, which is very simple in general. We can see the figure of PAAS. Yes, using Docker as the underlying service of PAAS is not complicated in itself. There is time to talk about how to use script files to build a mirror image instance, and talk about the implementation principle and mechanism of Docker.