Building tomcat7 image

Time:2021-5-13

1.Dockerfile

Docker Manual:
Free Chinese Manual [docker – from introduction to practice]
https://vuepress.mirror.docke…

get ready:
Download Image:https://hub.docker.com

centos:7 image
JDK package: jdk-8u212-linux-x64.tar.gz
Tomcat7 package: apache-tomcat-7.0.96.tar.gz

In the root directory, create the tomcat7 directory

mkdir tomcat7

Put jdk8 and tomcat7 into tomcat7 directory, and create dockerfile file

cd tomcat7
vim Dockerfile

Building tomcat7 image

Dockerfile file (Chinese note)
When dockerfile cannot be written in Chinese, the second configuration will be used

#Based on centos7, install Oracle jdk8 and tomcat7
FROM centos:7

#The add command passes the compressed package to the specified directory in the image and decompresses it at the same time
ADD jdk-8u212-linux-x64.tar.gz /opt/

ADD apache-tomcat-7.0.96.tar.gz /usr/

#For convenience, change the name of the folder to be simpler
RUN mv /usr/apache-tomcat-7.0.96 /usr/tomcat

#Setting environment variables
ENV JAVA_HOME=/opt/jdk1.8.0_212 \
    CATALINA_HOME=/usr/tomcat \
    PATH=$PATH:/opt/jdk1.8.0_212/bin:/usr/tomcat/bin

#Port 8080 of exposure container
EXPOSE 8080

#Set Tomcat to run automatically when starting the container
ENTRYPOINT /usr/tomcat/bin/startup.sh && tail -F /usr/tomcat/logs/catalina.out

Dockerfile file:

FROM centos:7
ADD jdk-8u212-linux-x64.tar.gz /opt/
ADD apache-tomcat-7.0.96.tar.gz /usr/
RUN mv /usr/apache-tomcat-7.0.96 /usr/tomcat
ENV JAVA_HOME=/opt/jdk1.8.0_212 \
    CATALINA_HOME=/usr/tomcat \
    PATH=$PATH:/opt/jdk1.8.0_212/bin:/usr/tomcat/bin
EXPOSE 8080
ENTRYPOINT /usr/tomcat/bin/startup.sh && tail -F /usr/tomcat/logs/catalina.out

Building tomcat7 image

Save to exit:
Press ESC

:wq

see:

ll

Building tomcat7 image

Use dockerfile to build image:

docker build -t tomcat:7 .

Notice the dot at the end, which indicates that you are looking for files from the current directory during the build process

Building tomcat7 image

View image:

docker images

Building tomcat7 image

To view image settings:

docker history tomcat:7

Building tomcat7 image

2. Start the container

Ready to store directory

Webapps directory, for example / opt / webapps
Logs directory, such as / var / lib / Tomcat logs

Build command:
Docker build – t image name: label folder path

docker run -d --name cat7 -p 80:8080 \
-v /opt/web:/tomcat/webapps/ROOT tomcat:7

“: it’s a splice, which splices the following code together for execution

Building tomcat7 image

Test redis container:

docker ps -a
docker run -d --name redis7000 --restart=always \
-p 7000:6379 redis
docker run -d --name redis7001 --restart=always \
-p 7001:6379 redis
docker run -d --name redis7002 --restart=always \
-p 7002:6379 redis
docker exec -it redis7000 redis-cli

Building tomcat7 image

Empty container:

docker rm -f redis7000 redis7001 redis7002

Building tomcat7 image

4. Web application

Virtual network:

4.1 if there are other networks, delete them first

#View virtual network
docker network ls
docker network rm my-net

Building tomcat7 image

4.2 create dockernet virtual network:

New virtual network my net:
–Subnet can specify IP and radix

# docker network create my-net --subnet 172.22.0.0/24
docker network create --subnet=172.18.0.0/24 dockernet

#View virtual network
docker network ls

Building tomcat7 image

4.3 create MySQL database:

Create and view data volumes:

docker volume create mysql-data
docker volume ls

Building tomcat7 image

Start the MySQL container:

docker run -d --restart=always \
-v mysql-data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-p 3306:3306 \
--net dockernet \
--ip 172.18.0.11 \
--name mysql mariadb

Building tomcat7 image

docker ps -a
docker inspect mysql

4.4 connect to database:

Building tomcat7 image

Import / docker / easymall / easymall.sql

Building tomcat7 image

4.5 start web application

mkdir ~/mall
cd ~/mall

Upload root. War to the mall directory

docker run -d --restart=always \
-v /opt/mall:/usr/tomcat/webapps \
-p 80:8080 \
--net dockernet \
--ip 172.18.0.12 \
--name web \
tomcat:7

Test address:
http://192.168.64.150