本次实验内容:用docker安装portainer,使用dockerfile构建jar运行环境,使用docker-compose部署jar和nginx,实现反向代理访问。

前提准备

环境:你需要已经安装了dockerdocker-compose,如果还没装,你可以参考这篇文章https://blog.hikki.site/2802e5c6.html

资源包下载:https://rookie1679.lanzouy.com/iAMQ70cqoo0h

下载该压缩包,解压后有四个文件docker-compose.ymlDockerfilehello.jarnginx.conf

将文件上传到服务器。放在/home,解压即可。

上传到服务器

1
2
# scp 本机文件路径 服务器用户名@IP地址:文件存放路径
scp D:\Study\docker.zip root@192.168.2.9:/home/

解压

1
2
3
cd /home
unzip docker.zip
cd docker && ls

00文件

构建Dockerfile

编写Dockerfile

由于jdk8官方已经不维护了,可以使用openjdk8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 基础镜像
FROM openjdk:8
#author
MAINTAINER wtl
EXPOSE 8080
# 挂载目录
VOLUME /home/docker
# 创建目录
RUN mkdir -p /home/docker
# 指定路径
WORKDIR /home/docker
# 复制jar文件到路径
COPY hello.jar /home/docker/hello.jar
# 启动认证服务
ENTRYPOINT ["java","-jar","hello.jar"]

构建image

docker build -t test .后面是有一个小点的,很多人没有留意,经常犯错。

1
docker build -t test .

01构建image

运行容器

1
docker run -d -p 8080:8080 test

查看运行情况

1
docker ps

02运行情况

部署服务

编写docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
version: "3.1"
services:
nginx: #服务名称,自定义
image: nginx:latest #镜像版本
restart: always
container_name: nginx
privileged: true #解决nginx的文件调用的权限问题
environment:
- TZ=Asia/Shanghai
ports: #暴露端口
- "800:80"
- "4443:443"
volumes:
- /home/docker/nginx/nginx.conf:/etc/nginx/nginx.conf
- /home/docker/nginx/log:/var/log/nginx
links:
- web-server

web-server:
restart: always
environment:
- TZ=Asia/Shanghai
build:
context: /home/docker
dockerfile: Dockerfile
container_name: web-server
ports:
- 8080:8080

docker-compose构建容器

1
docker-compose up

03docker-compose

浏览器打开预览

1
你的虚拟机IP地址:8080/hello

04运行成功

期间遇到的错误

jdk版本过高报错

在构建完Dockerfile后,使用docker-compose up启动容器的时候报错

1
2
3
4
5
6
7
8
9
10
11
12
web-server    | 2022-10-01 12:41:30.943 ERROR 1 --- [           main] o.s.boot.SpringApplication               : Application run failed
web-server |
web-server | java.lang.ExceptionInInitializerError: null
web-server | at org.springframework.context.annotation.ConfigurationClassEnhancer.newEnhancer(ConfigurationClassEnhancer.java:121) ~[spring-context-5.3.16.jar!/:5.3.16]
web-server | at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:109) ~[spring-context-5.3.16.jar!/:5.3.16]
web-server | at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:447) ~[spring-context-5.3.16.jar!/:5.3.16]
web-server | at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:268) ~[spring-context-5.3.16.jar!/:5.3.16]
web-server | at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:325) ~[spring-context-5.3.16.jar!/:5.3.16]
web-server | at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:147) ~[spring-context-5.3.16.jar!/:5.3.16]
web-server | at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746) ~[spring-context-5.3.16.jar!/:5.3.16]
web-server | at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564) ~[spring-context-5.3.16.jar!/:5.3.16]
.......

解决方法

我原本的jdk版本是jdk:8

Dockerfilejdk版本修改为openjdk:8

再将已经构建好的docker镜像docker容器删掉并且重启一下docker

查看容器ID

1
docker ps -a

删除该容器

1
docker rm ID

13删除容器

查看镜像ID

1
docker images

删除镜像

1
docker rmi -f REPOSITORY

12error删除镜像

重启docker

1
systemctl restart docker

重新构建Dockerfile,运行容器

docker-compose.yml版本撰写错误

1
2
ERROR: Version in "./docker-compose.yml" is unsupported. You might be seeing this error because you're using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the file to use version 1.
For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/

解决方法

在首次的运行docker-compose.yml中,版本号必须使用x.1,一般使用3.1,将docker-compose.yml改为3.1就没问题了