Article Directory
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 tomcat
Pull the latest version by default
docker images
You can view the local image and
docker run -d -p 8080:8080 --name hello_tomcat tomcat
use 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
- Problems that may be encountered in the latest version: Tomcat started successfully but reported a 404
docker exec -it hello_tomcat
Enter the container- Found that the webapps are all empty, the files are all under webapps.dist
- Copy all the files under webapps.dist to webapps and
finally you can access the tomcat homepage normally
Publish war package
docker cp ./gradle_web2-1.0-SNAPSHOT.war hello_tomcat:/usr/local/tomcat/webapps
Copy your own war package to the container, you can restart the container or wait for tomcat to decompress the war package,
which can be accessed through the project name
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_tomcat
First stop the running
docker commit hello_tomcat helllo_tomcat_image
container and submit your own container as a mirror.
If you need to give the mirror to others to use
- 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- 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 images
View your own mirror
docker run -d -p 8088:8080 --name hello_tomcat_container hello_tomcat_image
- 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.
Based on experience, 403 may be a host port conflict
docker stop hello_tomcat_container
Close the containernetstat -ano|findstr "8081"
Found that the port is indeed occupied, find a port that is not occupied, I chose 8088 here.
docker rm hello_tomcat_container
Delete 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.
- 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
docker build -f ./hello_tomcat_dockerfile -t hello_tomcat_image:v2 .
Make a new mirror image, v2 is the tagdocker images
You can view the mirror image just made
docker run -d -p 8888:8080 --name hello_tomcat_container2 hello_tomcat_image:v2
Successfully visited your own web project