Docker Deployment Javaweb Project Combat

Source: Internet
Author: User
Tags tag name java web docker ps docker run

Lin Bingwen Evankaka original works. Reprint please specify the source Http://

Summary: This article focuses on how to create a Docker container that runs a Java Web application in the Ubuntu14.04 64-bit system.

First, download the image, start the container

1. Download image

View Mirroring First

Docker images

Remember this image ID and we need to use it to launch the container below.
If you see the above output, you can use the "oursuer/ubuntu" image, or call it a warehouse (Repository), which has a label named "14.04" and a tag, plus a
The image ID of the 1F879014F4D2 (perhaps the image ID you see is inconsistent with this, which is normal, because this number is randomly generated). In addition, we can see that the image is only 188.4 MB, very small, and not as large as the virtual machine's image file.
Now that the image is there, we need to use the image below to start the container.

2. Start the container

Containers are run on the basis of mirroring, and once the container is started, we can log in to the container and install the software or application we need. Now that the image has been downloaded locally, how can you start the container?
Just use the following command to start the container:

Docker Run-i-t-v/download/:/mnt/software/1f879014f4d2/bin/bash

This command is longer, we break it down a little bit, actually contains the following three parts:
Docker run < related parameters > < mirroring id> < initial commands >
If you see the above output, you can use the "" image, or call it a warehouse (Repository), which has a label named "Centos6", and a tag called " 25c5298b1a36 "Image ID (perhaps the image ID you see is inconsistent with this one, which is normal because the number is randomly generated). In addition, we can see that the image is only 215.8 MB, very small, and not as large as the virtual machine's image file.
Now that the image is there, we need to use the image below to start the container.
Among them, the relevant parameters include:-I: To run the container in "interactive mode"-T: Indicates that the container starts to enter its command line-V: Indicates which directory to mount to the container, format:-v < host directory >:< container directory >

Assuming that all our installers are placed in the/download/directory of the host, it is now necessary to mount it to the container's/mnt/software/directory.
It is necessary to note that you do not have to use "mirror ID" or "warehouse Name: Tag name", for example: oursuer/ubuntu:14.04.
The initial command indicates the command to run once the container is started, using "/bin/bash" at this time to indicate nothing, just go to the command line.

Exit the container using Ctrl+d or enter exit

If you exit the container, you can use the command again (note that you will have to run Docker run and then stop the container before you can use the following command)

Docker start container ID

Where the container ID is. Using the command Docker ps-a looks like this:

Then start the container again that has been started but is now closed

Docker start Sick_mestorf
This command only launches the container and does not enter the container. To get into the container, use the command.

Docker attach container ID or container name

As follows:

second, the installation of related software

First put the JDK and Tomcat installation package. gz files in the outside/download folder

Then start the container again. Enter the container

Then see if there is an installation package. We've found two of them, and you can install them below.

1. JDK Installation

Direct use

Cd/usrmkdir JAVACD javamkdir Jdkcd/mnt/software/tar zxvf jdk-8u65-linux-x64.gz-c/usr/java/jdk
Here directly to the files extracted to the/USR/JAVA/JDK directory, as follows

Configuring Environment variables

Add the following content:

Note that this is changed to the directory of the corresponding JDK and will be changed by the root user, otherwise it cannot be saved
Save after execution:
To verify the installation:

If the following appears, the installation is successful

2. Tomcat Installation

Direct decompression

Cd/usr/javamkdir Tomcattar zxvf  -c/usr/java/tomcat
Configuring the Environment
Go to the upper Tomcat folder under the bin:

Open it


Add the following content:

Save it.

Then go back to the bin directory:



Description of the Tomcat boot success

Or, use the following command to view:

Iii. Configuring the container to start environment variables

(The JDK configuration environment variable above can not be, but it must be written!!!!!!!!!! Because this is the environment variable that is loaded when the container starts up)

1. Setting Environment variables

First, edit the. bashrc file

Then, add the following configuration at the end of the file:
Finally, you need to use the source command to make the environment variable effective:

2. Write Run script

We need to write a run script that, when the container is started, runs the script and launches Tomcat, as follows:
First, create a run script:

Then, edit the script content as follows:
#!/bin/bashsource ~/.bashrcsh/usr/java/tomcat/apache-tomcat-7.0.68/bin/ Run
Note: You must load the environment variable first, and then use Tomcat's run script to start the Tomcat service.
Finally, add execute permissions to run the script:
chmod u+x/root/

3. Exit the container

When all of the above steps are complete, you can use the exit or CTRL+D command to exit the container.
You can then use the following command to view the running container:
Docker PS
At this point, you should not see any programs running, because the container that you just exited with the Exit command is in a stopped state, and you can view all the containers using the following command:
Docker Ps-a
The output is as follows:

Remember the above container ID (container ID), and then we will create a mirror that can run the Java Web through the container.

Iv. Creating a Java Web imageUse the following command to create a new "mirror" based on a "container ID":
Docker commit 89a47b5b749e  lin_javaweb:0.1
The ID of the container is "89a47b5b749e" and the image name created is "lin_javaweb:0.1, you can then use mirroring to start the Java Web container.

This is a successful creation

You can also view the next image

v. Start the Java Web containerIt is necessary to first use the Docker Images command to view all current mirrors:
Visible, you now see the newly created mirror "lin_javaweb:0.1" with the image ID "4487bd38df06". As described above, we can start the container with a "mirror name" or "Mirror ID", unlike the last time the container was started, we are now no longer entering the container's command line, but instead directly start the TOMCAT service inside the container. At this point, you need to use the following command:
Docker run-d-P 58080:8080--name javaweb lin_javaweb:0.1/root/
A little explanation:
-D: The/root/ script is executed in "daemon mode", at which point the Tomcat console does not appear on the output terminal. -P: Represents the port mapping of the host to the container, at which point the 8080 port inside the container is mapped to the 58080 port of the host, exposing 58080 ports to the outside, and can access the 8080 ports inside the container via the Docker bridge. --name: Represents the container name and is named with a meaningful name.

The content of the Docker bridge needs to be supplemented with instructions. Actually, Docker builds a bridge between the host and the container, and we can use the host IP address and port number to map the IP address and port number inside the container.
After a series of parameters is the "Mirror name" or "Mirror ID", how convenient to come. The last is the "initial command", which is a running script written above that encapsulates the commands that load environment variables and start the Tomcat service.
When the above command is run, a long list of "container IDs" is immediately output, and we can view the currently running containers through the Docker PS command.

In the browser, enter the following address to access the Tomcat home page:


http://localhost: 58080 /
Note: The IP address of the host is used here, with the externally exposed port number 58080, which maps the port number 8080 inside the container.

Docker Deployment Javaweb Project Combat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.