Detailed steps for deploying tomcat in docker and deploying java applications

  • 2020-05-17 06:55:57
  • OfStack

Let me give you a brief idea of what Docker is

Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container and release them to any popular Linux machine, as well as virtualization. Containers are completely sandboxed and have no interfaces with each other.

1. First, how to deploy tomcat in docker

Step 1: the root user logs in and creates the tomcat7 folder in the system root directory. The command is: mkdir tomcat7, and switches to this directory: cd tomcat7;

Step 2: create Dockerfile with commands like touch Dockerfile;

Step 3: write Dockerfile, 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 here will expose all the ports that need to be used in the program. You can tell from the environment variable CATALINA_BASE that the directory specified by VOLUME is the directory where tomcat deployed the war package.

Step 4: create a mirror. Command: docker build-t test/test_app. Note that in this process, docker needs to download the installed jdk and tomcat programs, so it is busy, you can check the command: docker image, the returned data line 1 has REPOSITORY and TAG < none > The data row of VIRTUAL_SIZE, where VIRTUAL_SIZE is periodically enlarged, indicates that it is being downloaded.

Step 5: run the image generation container. Command docker run --name test_app-d-p 8080:8080-p 9000:9000 image id. If you don't know what image id is, use the command: Take a look at docker images. Notice that the port from EXPOSE is now bound to the host port, so you can use the host's IP address and this port to access tomcat and the applications in the docker container.

Step 6: test: in the browser address bar type: http:// host ip:8080/, ok, successful.

You can use the command docker port container id 8080, which looks at the mapping between port 8080 of the container and the host port. You can see that port 8080 of the container now maps to port 8080 of the host.

2. How do we deploy our application to tomcat in the docker container?

You know that tomcat is in a container, and you can't deploy an tomcat application as you would on a host. So what's the solution?

Step 1:1 on the problems we already know war bag is deployed in the position of the container is: / var/lib/tomcat7 webapps /, so where is this location? Use the command: docker inspect-f "{{.Volumes}}" container id. If you do not know the container id, you can view all the containers running by docker ps or by docker ps-a.

The following results are obtained:


map[/var/lib/tomcat7/webapps:/var/lib/docker/vfs/dir/28d6dd0455d92f0160288a56f838d8aeeff402a843bd57d3b21fcd80eac7df02] . 

In the brackets of map, the directory in the container is the one before the colon, and the directory in the host is the one after the colon, so we just copy our war package to this location and restart tomcat.

Note here that since tomcat will be started when the default container is started in Dockerfile above, we only need to restart the container to ask tomcat to load our container. The command is as follows:

docker restart container id

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

docker logs container id, docker logs-f container id, this is when the log is read from scratch, if the log is very long it will be refresh for a long time, if you only want to look at the latest log you can use the following way: docker logs --tail 0-f container id

Note that sometimes we want to specify the mapped Volumes when we execute the docker run command, so we 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/ 64de1445c34c

The important thing to note here is that the directory path of the host is preceded by the last colon, and the directory path in the container is followed by the colon.


Related articles: