centos7 docker构建java项目

1、目标

基于docker构建helloworld镜像,学习如何用docke构建java镜像

2、准备工作

docker安装部分此处略,可以参考:https://blog.csdn.net/lsnow8624/article/details/113248798

2.1、安装java

a、下载jdk1.8
b、解压jdk,将积压后的文件夹移动到/usr/local
[root@localhost docker]# tar -xvf jdk-8u212-linux-x64.tar.gz
[root@localhost docker]# mv jdk1.8.0_212 /usr/local/

2.2、安装maven

a、下载maven
b、解压maven,将积压后的文件夹移动到/usr/local
[root@localhost docker]# unzip apache-maven-3.6.3.zip
[root@localhost docker]# mv apache-maven-3.6.3 /usr/local

2.3、设置环境变量

a、在/etc/profile文件末尾追加以下内容
export JAVA_HOME=/usr/local/jdk1.8.0_212
export M2_HOME=/usr/local/apache-maven-3.6.3
export PATH=$PATH:$M2_HOME/bin:$JAVA_HOME/bin
b、使环境变量生效
[root@localhost docker]# source /etc/profile

2.4、下载helloworld项目

a、安装git
[root@localhost docker]#yum install git -y
b、下载helloworld项目
[root@localhost docker]#git clone https://github.com/gazgeek/springboot-helloworld.git
c、用maven打包项目
[root@localhost docker]# cd springboot-helloworld/
[root@localhost springboot-helloworld]# ls
Dockerfile  pom.xml  Procfile  README.md  src
[root@localhost springboot-helloworld]#mvn clean install -DskipTests
[root@localhost springboot-helloworld]# ls
Dockerfile  pom.xml  Procfile  README.md  src  target
mvn执行成功后会生成target目录
[root@localhost springboot-helloworld]# cd target
[root@localhost target]# ls
classes            generated-test-sources         helloworld-0.0.1-SNAPSHOT.jar.original  maven-status
generated-sources  helloworld-0.0.1-SNAPSHOT.jar  maven-archiver                          test-classes
此时生成了一个helloworld-0.0.1-SNAPSHOT.jar
c、运行helloworld项目
[root@localhost target]# java -jar helloworld-0.0.1-SNAPSHOT.jar

此时准备工作执行完毕,下面用Dockerfile构建helloworld镜像
注:下载从docker hub上下载java镜像比较慢,最好先单独拉取下来

3、制作java项目镜像

3.1编写dockerfile文件

[root@k8sm01 springboot-helloworld]# cat hellodockerfile 
FROM java # 基础镜像

COPY ./helloworld-0.0.1-SNAPSHOT.jar helloworld-0.0.1-SNAPSHOT.jar #  复制jar包到镜像
EXPOSE 8080 # 暴露端口
CMD java -jar helloworld-0.0.1-SNAPSHOT.jar # 在镜像中运行项目

3.2构建镜像

使用编好的dockerfile构建一个名为hello的镜像
[root@k8sm01 springboot-helloworld]# docker build -t hello -f hellodockerfile .
[root@k8sm01 springboot-helloworld]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello               latest              33adcb82bfd1        11 minutes ago      656MB
java                latest              d23bdf5b1b1b        4 years ago         643MB

3.2启动容器

启动容器,名称为helloworld

[root@k8sm01 springboot-helloworld]# docker run -itdp 8080:8080 --name helloworld hello

3.3修改tcp6映射

注:容器运行的端口映射出来后是tcp6的,无法直接访问,如下修改可以解决:

[root@localhost ~]# vim  /usr/lib/sysctl.d/00-system.conf

net.ipv4.ip_forward=1 #添加这段代码

[root@localhost ~]# vim /etc/sysctl.conf

net.ipv4.ip_forward=1  #添加这段代码

重启网络
[root@dc02 log]# systemctl restart network

3.4验证结果

[root@k8sm01 springboot-helloworld]# docker logs -f helloworld #查看容器日志
[root@k8sm01 springboot-helloworld]# curl 127.0.0.1:8080 #查看项目运行结果,OK
Hello from GazGeek!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值