Docker releases JavaWeb project

Prepare the container

It is recommended to pull out the Tomcat image, so that it is more convenient to omit the JDK and Tomcat configuration.

If you want to practice the installation of JDK and Tomcat, you can pull an Ubuntu or CentOS image and build
it from scratch; if you have special requirements for the JDK or Tomcat version, you can first go to the official warehouse to see which ones are supported

docker pull tomcatPull the latest version by default
Insert picture description here

docker imagesYou can view the local image and
Insert picture description here
docker run -d -p 8080:8080 --name hello_tomcat tomcatuse the tomcat image to create a container

-d: run the container in the background and return the container ID;
-p: specify the port mapping, the format is: host (host) port: container port;
--name hello_tomcat: specify a name for the container;

You can see the returned container ID
Insert picture description here

  • Problems that may be encountered in the latest version: Tomcat started successfully but reported a 404
    Insert picture description here
  1. docker exec -it hello_tomcatEnter the container
  2. Found that the webapps are all empty, the files are all under webapps.dist
    Insert picture description here
  3. Copy all the files under webapps.dist to webapps and
    Insert picture description here
    finally you can access the tomcat homepage normally
    Insert picture description here

Publish war package

docker cp ./gradle_web2-1.0-SNAPSHOT.war hello_tomcat:/usr/local/tomcat/webappsCopy your own war package to the container, you can restart the container or wait for tomcat to decompress the war package,
Insert picture description here
which can be accessed through the project name
Insert picture description here

Make a mirror

After confirming that the war package published by yourself can be accessed normally, you can also make this container as a mirror.
docker stop hello_tomcatFirst stop the running
docker commit hello_tomcat helllo_tomcat_imagecontainer and submit your own container as a mirror.

If you need to give the mirror to others to use

  1. Can be saved as a file and handed over to others to load
    the file name saved by docker save -o Mirror name
    saved by docker load -i File name
  2. You can also push to the remote warehouse
    docker login remote warehouse address
    docker push remote warehouse address/project name
    docker pull remote warehouse address/project name

docker imagesView your own mirror
Insert picture description here
docker run -d -p 8088:8080 --name hello_tomcat_container hello_tomcat_image
Insert picture description here

  • Possible problems:
    The web service before the 403 Forbidden image is generated is already normal. The image generally does not change the information inside the container. The problem is most likely to be on the host.
    Insert picture description here

Based on experience, 403 may be a host port conflict

  1. docker stop hello_tomcat_containerClose the container
  2. netstat -ano|findstr "8081"Found that the port is indeed occupied, find a port that is not occupied, I chose 8088 here.
    Insert picture description here
  3. docker rm hello_tomcat_containerDelete the container with the port conflict just now, and then be sure to select a free port and recreate the container.

Use Dockerfile to make a mirror

The above is to first create a container based on the tomcat image, then transfer the war package to the container, and finally make the container into an image. We can use Dockerfile to merge these steps together.

  1. Create the hello_tomcat_dockerfile file and enter the following content: select the tomcat image, copy your war package into it, and be sure to pay attention to the path of the war package.
from tomcat
COPY gradle_web2-1.0-SNAPSHOT.war /usr/local/tomcat/webapps
  1. docker build -f ./hello_tomcat_dockerfile -t hello_tomcat_image:v2 .Make a new mirror image, v2 is the tag
  2. docker imagesYou can view the mirror image just made
    Insert picture description here
  3. docker run -d -p 8888:8080 --name hello_tomcat_container2 hello_tomcat_image:v2
    Successfully visited your own web project
    Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44112790/article/details/109889522