linux运维

Linux-CentOS | Docker配置Java与Tomc

2019-12-10  本文已影响0人  夏浩文

略略略

运行Centos镜像,别名centos

[root@VM_0_12_centos ~]# docker run -itv /usr/local/software/centos/:/software/centos/ --name centos  centos /bin/bash

参数说明:

Tip :可使用ctrl + p + q 退出容器(且不关闭容器)


jdk复制到centos容器内文件夹

[root@VM_0_12_centos ~]# docker cp /usr/local/src centos:/jdk

进入centos

[root@VM_0_12_centos ~]# docker attach centos
[root@7336a552c305 /]# [光标]

解压jdk安装包

[root@7336a552c305 ~]# cd /
[root@7336a552c305 /]# ls
bin  etc   jdk  lib64       media  opt   root  sbin  srv  tmp  var
dev  home  lib  lost+found  mnt    proc  run   software  sys  usr
[root@7336a552c305 /]# cd jdk/
[root@7336a552c305 jdk]# ls
jdk-11.0.5_linux-x64_bin.tar.gz  jdk-13.0.1_linux-x64_bin.tar.gz  jdk-8u231-linux-x64.tar.gz
[root@7336a552c305 jdk]# tar -xzvf jdk-8u231-linux-x64.tar.gz

安装vim

yum -y install vim

查看解压路径,复制备用

[root@7336a552c305 jdk]# ls
jdk-11.0.5_linux-x64_bin.tar.gz  jdk-13.0.1_linux-x64_bin.tar.gz  jdk-8u231-linux-x64.tar.gz  jdk1.8.0_231

vim ~/.bashrc

Tip : Linux vi/vim 使用指北

# Java Environment
export JAVA_HOME=/jdk/jdk1.8.0_231
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

ESC 退出编辑模式,输入:wq 保存并退出

使环境变量生效

[root@7336a552c305 jdk]# source ~/.bashrc

验证一哈jdk

[root@7336a552c305 jdk]# java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)

安装wget

[root@7336a552c305 yum.repos.d]# yum -y install wget

centos-8中 出现key验证失败

Error: GPG check FAILED

可进入yum.repos.d关闭对应gpgcheck再安装

[root@7336a552c305 ~]# cd /etc/yum.repos.d/ 
[root@7336a552c305 yum.repos.d]# ls
CentOS-AppStream.repo  CentOS-Debuginfo.repo  CentOS-PowerTools.repo  CentOS-centosplus.repo
CentOS-Base.repo       CentOS-Extras.repo     CentOS-Sources.repo     CentOS-fasttrack.repo
CentOS-CR.repo         CentOS-Media.repo      CentOS-Vault.repo
# 修改对应repo,如:
[root@7336a552c305 yum.repos.d]# vim CentOS-Base.repo 

//------------------------------我是分割线-------------------------------//

# File:CentOS-AppStream.repo 
[AppStream]
name=CentOS-$releasever - AppStream
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=AppStream&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/AppStream/$basearch/os/
gpgcheck=0 #关闭gpgcheck
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

冷不防的验证文件链接

http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-Official

下载tomcat ➡️ 查看要安装的tomcat版本

[root@7336a552c305 /]# mkdir tomcat
[root@7336a552c305 /]# cd tomcat/
[root@7336a552c305 tomcat]# wget https://mirrors.cnnic.cn/apache/tomcat/tomcat-9/v9.0.29/bin/apache-tomcat-9.0.29.tar.gz

解压tomcat

[root@7336a552c305 tomcat]# ls
apache-tomcat-9.0.29.tar.gz
[root@7336a552c305 tomcat]# tar zxvf apache-tomcat-9.0.29.tar.gz

获取tomcat启动路径

# 获取个路径
[root@7336a552c305 tomcat]# ls
apache-tomcat-9.0.29  apache-tomcat-9.0.29.tar.gz
[root@7336a552c305 tomcat]# cd apache-tomcat-9.0.29/bin/
[root@7336a552c305 bin]# vi catalina.
catalina.bat  catalina.sh   
[root@7336a552c305 bin]# pwd
/tomcat/apache-tomcat-9.0.29/bin 
// end

设置自启动

#启动脚本
[root@7336a552c305 bin]# vi /root/run.sh

#!/bin/bash
# Start tomcat
sh /tomcat/apache-tomcat-9.0.29/bin/catalina.sh run

#添加可执行权限:
chmod u+x /root/run.sh

重新启动

# 关闭容器
[root@VM_0_12_centos ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7336a552c305        centos              "/bin/bash"         2 hours ago         Up 2 hours                              centos
[root@VM_0_12_centos ~]# docker stop centos
centos

# 创建镜像
[root@VM_0_12_centos ~]# docker commit -p -a "code_ivan" -m "centos+java_1.8.0_231+tomcat-9.0.29" centos code_ivan/jre:v1.0.0

# 启动容器
[root@VM_0_12_centos ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
code_ivan/jre       v1.0.0              c56601db36c7        About a minute ago   1.28GB
tomcat              latest              6408fdc94212        9 days ago           507MB
nginx               latest              231d40e811cd        9 days ago           126MB
centos              latest              0f3e07c0138f        2 months ago         220MB
[root@localhost ~]# docker run -d -p 9999:8080 --name jreserver -v /usr/local/software/centos/:/software/centos/ c56601db36c7 /root/run.sh

# 之后启动容器
[root@VM_0_12_centos ~]# docker start jreserver

创建镜像说明:

Docker运行说明:

上一篇下一篇

猜你喜欢

热点阅读