1. 创建dockerfile存放目录
1.1 创建目录
[root@docker ~]# mkdir -p /dockerfile
[root@docker ~]# cd /dockerfile/
[root@docker dockerfile]# mkdir -p java
[root@docker dockerfile]# cd java/
2. 创建tomcat dockerfile配置文件
2.1 创建文件
[root@docker nginx]# touch Dockerfile
2.2 下载地址
github地址:https://github.com/tiaotiaodan/dockerfile-java-demo.git
2.3 下载jar的demo包
[root@docker java]# cd /tmp/
[root@docker tmp]# ll
总用量 0
drwxr-xr-x. 2 root root 6 10月 15 19:34 hsperfdata_root
drwx------. 3 root root 17 9月 27 21:45 systemd-private-0b1f5a81877943188701d6297bbc0965-chronyd.service-6L6jNz
[root@docker tmp]# git clone https://github.com/tiaotiaodan/dockerfile-java-demo.git
正克隆到 'dockerfile-java-demo'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
[root@docker tmp]# ll
总用量 0
drwxr-xr-x. 3 root root 35 10月 15 19:48 dockerfile-java-demo
drwxr-xr-x. 2 root root 6 10月 15 19:34 hsperfdata_root
drwx------. 3 root root 17 9月 27 21:45 systemd-private-0b1f5a81877943188701d6297bbc0965-chronyd.service-6L6jNz
[root@docker tmp]# cd dockerfile-java-demo/
[root@docker dockerfile-java-demo]# ll
总用量 4
-rw-r--r--. 1 root root 960 10月 15 19:48 hello.jar
[root@docker dockerfile-java-demo]# mv hello.jar /dockerfile/java/
3. 编写dockerfile文件
[root@docker java]# vim Dockerfile
[root@docker java]# cat Dockerfile
# this docker file
# VERSION 1.0
# author: shichao
# email: shichao@scajy.cn
FROM java:8-jdk-alpine
MAINTAINER shichao@scajy.cn
ENV JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8 -Duser.timezone=GMT+08"
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
apk add -U tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY hello.jar /
EXPOSE 8888
CMD ["/bin/sh", "-c", "java -jar $JAVA_OPTS /hello.jar "]
4. 构建微服务jar
[root@docker java]# docker build -t jar:v1 .
Sending build context to Docker daemon 3.584kB
Step 1/7 : FROM java:8-jdk-alpine
8-jdk-alpine: Pulling from library/java
709515475419: Pull complete
38a1c0aaa6fd: Pull complete
5b58c996e33e: Pull complete
Digest: sha256:d49bf8c44670834d3dade17f8b84d709e7db47f1887f671a0e098bafa9bae49f
Status: Downloaded newer image for java:8-jdk-alpine
---> 3fd9dd82815c
Step 2/7 : MAINTAINER shichao@scajy.cn
---> Running in f50249514436
Removing intermediate container f50249514436
---> d6f56767edae
Step 3/7 : ENV JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8 -Duser.timezone=GMT+08"
---> Running in 0e20cedd3b70
Removing intermediate container 0e20cedd3b70
---> d3e36a34f548
Step 4/7 : RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && apk add -U tzdata && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
---> Running in 96f2c4a4bcac
fetch http://mirrors.aliyun.com/alpine/v3.4/main/x86_64/APKINDEX.tar.gz
fetch http://mirrors.aliyun.com/alpine/v3.4/community/x86_64/APKINDEX.tar.gz
(1/1) Installing tzdata (2016d-r0)
Executing busybox-1.24.2-r13.trigger
OK: 144 MiB in 36 packages
Removing intermediate container 96f2c4a4bcac
---> f8be322019f2
Step 5/7 : COPY hello.jar /
---> 7fab81b93d23
Step 6/7 : EXPOSE 8888
---> Running in 085b0beb6af9
Removing intermediate container 085b0beb6af9
---> 99143195fbde
Step 7/7 : CMD ["/bin/bash", "-c", "java -jar $JAVA_OPTS /hello.jar "]
---> Running in da7c8b3606ce
Removing intermediate container da7c8b3606ce
---> 866c952a3175
Successfully built 866c952a3175
Successfully tagged jar:v1
5. 查看镜像,验证构建成功没
[root@docker java]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jar v1 866c952a3175 10 minutes ago 147MB
6. 使用jar镜像,创建docker容器
[root@docker java]# docker run -d --name=jar --network=test -p8888:8888 jar:v1
d4ee5d1bc159c517a48ccd90c0fd6dcfa6cb6cad4c5852ad182a9e62c032e6d8
7. 验证jar容器是否正常运行
[root@docker java]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4ee5d1bc159 jar:v1 "/bin/sh -c 'java -j…" About a minute ago Up About a minute 0.0.0.0:8888->8888/tcp jar
84a078c18812 tomcat:v1 "catalina.sh run" 2 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp, 9000/tcp tomcat
8. 浏览器访问
8.1 查看本机IP
[root@docker java]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.200.126 netmask 255.255.255.0 broadcast 192.168.200.255
inet6 fe80::250:56ff:feb6:c59f prefixlen 64 scopeid 0x20<link>
inet6 fd37:1f22:7906:0:250:56ff:feb6:c59f prefixlen 64 scopeid 0x0<global>
ether 00:50:56:b6:c5:9f txqueuelen 1000 (Ethernet)
RX packets 130188002 bytes 11612360985 (10.8 GiB)
RX errors 0 dropped 368300 overruns 0 frame 0
TX packets 550582 bytes 50513244 (48.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
8.2 docker log 查看后台输出内容
[root@docker ~]# docker logs jar
Hello world!
sleep 1 day...
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:docker-dockerfile构建与部署微服务jar包 - Python技术站