Docker to deploy tomcat

  • docker search tomcatFirst look up the Tomcat image, usually the one with the highest likes

  • Docker pull Tomcat pulls the official image

  • Docker Images view all images

The Tomcat image was pulled down by us.

However, this Tomcat image does not contain our project. If we want our WAR package in the image, what should we do?

I created a new Dockerfile in the path below

The contents are

from tomcat
MAINTAINER [email protected]
RUN rm -rf /usr/local/tomcat/webapps/*
COPY pms.war   /usr/local/tomcat/webapps
COPY msp.war /usr/local/tomcat/webapps
COPY isp.war /usr/local/tomcat/webapps
COPY plutus.war /usr/local/tomcat/webapps
Copy the code

  • The first line shows which image you came from. When we first looked at all the images in Docker Images, tomcat was in it

  • The second line indicates the author

  • We delete all the webapps files and copy the war package from the source Linux to the image. It will do this every time it builds an image, but it has not built the image yet. With this Dockerfile you can build the image. Close the Dockerfile

  • Docker build-t cspImage :1.0 Create an image using the Dockerfile of the current directory labeled cspImage :1.0

Now that we have our image, let’s start building containers. What is a container? A container is a running instance created with an image, that is, with this image we can build multiple different containers, and then build a container

But, in case something changes, don’t you build the image and container every time? You can write a shell script

Create a new deploy.sh file

  • The first line is to remove the container CSPContailner
  • The second line removes the image cspImage :1.0.
  • The third line is to build the image
  • The fourth line builds a container named CSPContailner with the external port number 9000 and the internal port number 8080
  • The fifth line mounts the file, with the source Linux path before the semicolon and the container after
  • Line 6 plus line 5 resolves the time zone inconsistency between Tomcat and source Linux
  • The seventh line is nothing, we can remove it, but we need to keep the cSPImage :1.0, which is our mirror image

If you run deploy.sh, it will remove the container, the image, and then build a new image from the docker build and the previous Dockerfile, and the docker run builds a new container

Chmod +x deploy. Sh Authorize deploy

OK, the container is built, we can access our project through port 9000, MSP is one of the projects we imported just now. For future deployments, just place the war package under /root/docker, where your Dockerfile and deploy.sh reside, and execute deploy.sh


Docker deployed nginx

docker pull nginxThe old rule is to pull the image to your own LinuxI already pulled that, so we don’t need to build a new image this time, because instead of copying files to the image, we can just build the container

Also, write your own shell script

  • The first and second lines stop the container named nginxContailner and delete it
  • Dist is my static resource. If you want to mount the other ones, you can write them in this format

Again, authorize and execute

over