- 需要下载tomcat安装包和jdk安装包,我这边下载的jdk版本分别为(jdk和tomcat版本需要对应上)
bash
apache-tomcat-9.0.78.tar.gz
jdk-8u381-linux-x64.tar.gz
- 创建一个readme.txt空文件
bash
readme.txt
- 创建一个Dockerfile文件
bash
# centos系统作为底层
FROM centos
# 作者信息及邮箱
MAINTAINER zrf<1213455555.163.com>
# 将readme.txt复制到容器的/usr/local/readme.txt
COPY readme.txt /usr/local/readme.txt
# 添加两个压缩包到 /usr/local之中,会自动解压缩
ADD jdk-8u381-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.78.tar.gz /usr/local
RUN cd /etc/yum.repos.d/
RUN sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
RUN sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
RUN yum -y install wget
RUN wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo \
&& yum clean all \
&& yum makecache
RUN yum -y install vim
RUN yum -y install vim
ENV MYPATH /usr/local
# 设置工作目录
WORKDIR $MYPATH
# 配置jdk环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_381
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
# 配置tomcat环境变量
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.78
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.78
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin/
# 暴露端口
EXPOSE 8080
# 执行命令
CMD /usr/local/apache-tomcat-9.0.78/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.78/bin/logs/catalina.out
- 构建镜像
bash
docker build -t mytomcat:1.0 .
- 查看镜像
bash
docker images
- 创建容器并启动容器
bash
docker run -it -d -p 8080:8080 镜像id