If you deploy tomcat in docker and deploy java applications

1. Let's talk about how to deploy tomcat in docker

    Step 1: Log in as root user and create a folder tomcat7 in the root directory of the system. Commands such as: mkdir tomcat7, and switch to this directory: cd tomcat7;

    Step 2: Create a Dockerfile, such as: touch Dockerfile;

    Step 3: Write a Dockerfile, such as: vim Dockerfile, complete the following commands:

                    FROM ubuntu:14.04
                    MAINTAINER zhaichong <[email protected]>
                    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: EXPOSE here should expose the ports that need 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 -f /soft/docker/Dockerfile /root

Note that in this process, docker needs to download and install the jdk and tomcat programs, so it is busy. You can view it through the command: docker image. In the first line of the returned data, there is a data line where REPOSITORY and TAG are both <none>. The VIRTUAL_SIZE is gradually increased, indicating that it is downloading.

      Step 5: Run the image to generate the container, the command is: docker run --name test_app -d -p 8080:8080 -p 9000:9000 Image id, if you don't know what the image id is, use the command: docker images to check, pay attention Here, the port from EXPOSE is bound to the port of the host host. In the future, you can use the IP address of the host host and this port to access tomcat and the applications in the docker container.

      Step 6: Test: Knock on the browser address bar: http://host ip:8080/, it's successful.

      You can use the command: docker port container id 8080. This command checks the mapping relationship between the port 8080 of the container and the port of the host host. It can be seen that the port 8080 of the container is now mapped to the port 8080 of the host host.

 

2、再说如何部署我们的应用程序到docker容器的tomcat里?

      大家知道tomcat在容器中,如果要像在宿主主机上那样部署tomcat应用程序肯定是不行的。那么采用什么办法呢?

      第一步:上一个问题中我们已经知道war包是部署在容器的位置

docker inspect --format='{{ .Config.Volumes}}'  id

 

是:/var/lib/tomcat7/webapps/,那么这个位置到底在什么地方?使用命令:docker inspect -f "{{.Volumes}}" 容器id,如果不知道容器id,则可以通过:docker ps的方式查看到所有运行的容器,也可以通过docker ps -a的方式查看所有容器,包括运行的和不运行的。

      会得出以下的结果:

      map[/var/lib/tomcat7/webapps:/var/lib/docker/vfs/dir/28d6dd0455d92f0160288a56f838d8aeeff402a843bd57d3b21fcd80eac7df02],在这个map的中括号里,冒号前边的是容器中的目录,冒号后边的对应的宿主主机中的目录,所以我们只需要把我们的war包拷贝到这个地方然后重启tomcat即可。

      这里要注意,由于我们上边写的Dockerfile中默认容器启动时是会启动tomcat的,所以这里只需要重启容器就能让tomcat把我们的容器加载上,命令如:

      docker restart 容器id

3、如果要看成tomcat的日志呢?比如查看启动时的日志,则可以使用命令:

       docker logs 容器id,如果要持续观察日志,则是:docker logs -f 容器id,这个时候日志是从头开时读的,如果日志很长会刷屏很久,如果只打算看最新的日志可以采用以下的方式:docker logs --tail 0 -f  容器id

      注意,有的时候我们想在执行docker run命令的时候才指定映射的Volumes,则可以使用如下命令:

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,这里一定要注意的是最后的英文冒号前边是宿主主机的文件夹路径,冒号后边是容器中的路径。

 

 

http://blog.csdn.net/achilles12345/article/details/47159043

 

http://blog.csdn.net/rznice/article/details/52211620

 

 

 

复制代码

4、启动容器(挂载),将software文件夹下的项目同步到tomcat镜像中并进入到该镜像中(其中镜像ID是上一步中查找到的tomcat镜像ID)

 

复制代码
docker run -i -t -v /root/software/:/mnt/software/ 镜像ID /bin/bash


docker run  --name app -d -p 8080:8080 -p 9000:9000 -v /root/web/:/var/lib/tomcat7/webapps/ cf31e0583f61

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327037030&siteId=291194637