Steps for deploying Tomcat in Docker and deploying Java applications _docker

Source: Internet
Author: User
Tags mkdir first row tomcat docker ps docker run

Let's start with a simple Docker concept.

Docker is an open source application container engine that allows developers to package their apps and dependencies into a portable container, and then publish it to any popular Linux machine or virtualization. Containers are completely using the sandbox mechanism, and there will be no interface between each other.

1, first say how to deploy Tomcat in Docker

The first step: root user Login in the system root directory to create a folder TOMCAT7, commands such as: mkdir TOMCAT7, and switch to the directory: CD TOMCAT7;

Step Two: Create Dockerfile, commands such as: Touch Dockerfile;

Step three: Write Dockerfile, commands such as: Vim Dockerfile, complete the following command to write:

From ubuntu:14.04
maintainer zhaichong <344616313@qq.com>
ENV refreshed_at 2015-7-28
RUN apt-get- YQQ Update
RUN apt-get-yqq install tomcat7 default-jdk
env catalina_home/usr/share/tomcat7
env Catalina_ BASE/VAR/LIB/TOMCAT7
env catalina_pid/var/run/tomcat7.pid
env catalina_sh/usr/share/tomcat7/bin/ catalina.sh
ENV catalina_tmpdir/tmp/tomcat7-tomcat7-tmp
RUN mkdir-p $CATALINA _tmpdir
VOLUME ["/var/ lib/tomcat7/webapps/"]
expose 8080 9000
entrypoint ["/usr/share/tomcat7/bin/catalina.sh "," Run "]

Description: The expose here should be exposed to the ports that need to be used in the program. Depending on the environment variable catalina_base can know that the directory specified by volume is the directory where Tomcat deploys the war package.

Fourth step: Generate mirror, command such as: Docker Build-t Test/test_app. Note that in this process Docker need to download the installed JDK and Tomcat program so busy, you can use the command: Docker image view, the returned data in the first row has a repository and tag are < None> of the data row, where the virtual_size is phased out, the description is in the download.

Step Fifth: Run the Mirrored build container, commands such as: Docker run--name test_app-d-P 8080:8080-p 9,000:9,000 Mirror ID, if you do not know how much the mirror ID is using the command: Docker images view, Note that the expose port is bound to the host host's port, and the host host's IP address and this port can then be used to access Tomcat and the application in the Docker container.

Sixth step: test: In the browser address bar knocking: http://host host ip:8080/, good success.

You can use the command: Docker port container ID 8080, which looks at the mapping relationship between the 8080 port of the container and the host host port. The 8080 port of the container now maps to the 8080 port of the host host.

2. How to deploy our application to Tomcat in the Docker container?

It is known that Tomcat is in the container, and it must not be possible to deploy the Tomcat application as if it were on the host host. So what's the solution?

Step one: In the last question we already know that the war package is deployed in the container location:/var/lib/tomcat7/webapps/, so where exactly is this position? Using the command: Docker inspect-f "{{. Volumes} "Container ID, if you do not know the container ID, you can view all the running containers by Docker PS, or you can view all containers Docker ps-a, including running and not running.

The following results are obtained:

map[/var/lib/tomcat7/webapps:/var/lib/docker/vfs/dir/ 28D6DD0455D92F0160288A56F838D8AEEFF402A843BD57D3B21FCD80EAC7DF02],

In the square brackets of this map, the colon precedes the directory in the container, the directory in the corresponding host host after the colon, so we just copy our war package to this place and restart Tomcat.

It is important to note here that because Tomcat is started when the default container is started in the dockerfile we write above, it is only necessary to reboot the container to allow Tomcat to load our container, as ordered:

Docker Restart container ID

3. What if you want to be a tomcat log? For example, to view the log at startup, you can use the command:

Docker logs container ID, if you want to continue to observe the log, it is: Docker logs-f container ID, this time the log is read from the beginning, if the log is very long to screen for a long time, if you only want to see the latest log can be used in the following way: Docker logs--tail 0- F Container ID

Note that there are times when we want to specify the mapping volumes when executing the Docker Run command, you can use the following command:

Docker run--name gboat2_ca-d-P 8080:8080-p 9000:9000--link db001:db_ca-v/HOME/WEBAPP/CA:/VAR/LIB/TOMCAT7/WEBAPPS/6 4de1445c34c

It must be noted here that the last English colon precedes the host host's folder path, followed by the path in the container.

The above is a small set to introduce the deployment of Tomcat in the Docker and deployment of Java applications in the detailed steps, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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: info-contact@alibabacloud.com 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.