If tomcat is deployed in docker and java applications are deployed

Source: Internet
Author: User
Tags docker ps

If tomcat is deployed in docker and java applications are deployed

1. Deploy tomcat in docker

Step 1: log on to the root directory of the system and create the tomcat7 folder. Run the command mkdir tomcat7 and switch to the directory cd tomcat7;

Step 2: Create a Dockerfile. The command is touch Dockerfile;

Step 3: Compile the Dockerfile command, such as vim Dockerfile. Complete the following command:

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"]

Note: EXPOSE must EXPOSE all the ports to be used in the program. According to the environment variable CATALINA_BASE, you can know that the directory specified by VOLUME is the directory where tomcat deploys the war package.

Step 4: generate an image. The command is docker build-t test/test_app. (Note: Here is an English period.) Note: in this process, docker needs to download and install the jdk and tomcat programs, so it is very busy. You can run the command: docker image to check them, in the returned data, the first row has a REPOSITORY and the TAG is both <none> data rows. The VIRTUAL_SIZE value increases gradually, indicating that the data is being downloaded.

Step 5: run the image generation container command, such as docker run -- name test_app-d-p 8080: 8080-p 9000: 9000 image id, run the command: docker images to check the image id. Note that the EXPOSE port is bound to the host port, later, you can use the IP address and port of the host to access tomcat and Its Applications in the docker container.

Step 6: Test: in the browser address bar, click http: // host Host ip Address: 8080.

You can run the docker port container id 8080 command to view the ing between container port 8080 and host port, it can be seen that port 8080 of the container is mapped to port 8080 of the host.


2. How to deploy our application to tomcat Of The docker container?

As you know, tomcat cannot be deployed in containers as it is on the host. So what should we do?

Step 1: In the previous question, we already know where the war package is deployed in the container:/var/lib/tomcat7/webapps/. Where is the location? Run docker inspect-f "{{. volumes} "container id. If you do not know the container id, you can view all running containers in the docker ps mode, you can also view all containers by using docker ps-a, including running and not running containers.

The following results are obtained:

Map [/var/lib/tomcat7/webapps:/var/lib/docker/vfs/dir/logs]. In the brackets of the map, the front side of the colon is the directory in the container, the directory in the host corresponding to the colon, so we only need to copy our war package to this place and then restart tomcat.

Note that tomcat is started by default when the container is started in the Dockerfile we wrote above, so you only need to restart the container so that tomcat can load our container. The command is as follows:

Docker restart container id

3. What if I want to view tomcat logs? For example, to view the log at startup, you can run the following command:

Docker logs container id. If you want to observe logs continuously, it is the docker logs-f container id.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.