在一个空文件夹中创建Dockerfile
将jar包复制到该路径下
在Dockerfile中添加以下内容
bash
# 指定基础镜像
FROM java:8-alpine
# 和java项目的包
COPY ./study_dockerfile-1.0.0.jar /tmp/app.jar
# 暴露端口
EXPOSE 8081
# 入口,java项目的启动命令
ENTRYPOINT java -jar /tmp/app.jar
在Dockerfile目录打开命令窗口
构建镜像
bash
docker build -t dockerfileDemo:1.0 .
不支持大写,因此改成:
bash
docker build -t appdemo:1.0 .
bash
Microsoft Windows [版本 10.0.19045.3570]
(c) Microsoft Corporation。保留所有权利。
D:\software\docker\dockerfiles>docker build -t dockerfileDemo:1.0 .
invalid argument "dockerfileDemo:1.0" for "-t, --tag" flag: invalid reference format: repository name must be lowercase
See 'docker build --help'.
D:\software\docker\dockerfiles>docker build -t appdemo:1.0 .
[+] Building 16.7s (6/6) FINISHED
=> [internal] load build definition from Dockerfile 0.3s
=> => transferring dockerfile: 249B 0.0s
=> [internal] load .dockerignore 0.2s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/java:8-alpine 16.1s
=> [internal] load build context 0.1s
=> => transferring context: 2B 0.0s
=> CANCELED [1/2] FROM docker.io/library/java:8-alpine@sha256:d49bf8c44670834d3dade17f8b84d709e7db47f1887f671a0e 0.1s
=> => resolve docker.io/library/java:8-alpine@sha256:d49bf8c44670834d3dade17f8b84d709e7db47f1887f671a0e098bafa9b 0.1s
=> => sha256:d49bf8c44670834d3dade17f8b84d709e7db47f1887f671a0e098bafa9bae49f 947B / 947B 0.0s
=> => sha256:3fd9dd82815ca4510a34bafef556815c440bcff37827d8495639137aeb145a36 3.29kB / 3.29kB 0.0s
=> ERROR [2/2] COPY ./docker-demo.jar /tmp/app.jar 0.0s
------
> [2/2] COPY ./docker-demo.jar /tmp/app.jar:
------
failed to compute cache key: "/docker-demo.jar" not found: not found
D:\software\docker\dockerfiles>
我错误的原因,dockerfile中"/docker-demo.jar
"忘了改成:"study_dockerfile-1.0.0.jar
",修改之后:
##启动镜像
bash
docker run --name appdemo -p 8081:8081 -d appdemo:1.0
本地访问
那么项目里yaml是8081,Dockerfile不是8081呢?
将Dockerfile中改成8082:
无法访问,可见是不可以的。
那么要更改端口号怎么办呢
添加配置文件config/application.yaml
到jar
包路径下,在配置文件中重新定义端口
Dockerfile文件中新增:COPY ./config/ /tmp/config
bash
# 指定基础镜像
FROM java:8-alpine
# 和java项目的包
COPY ./study_dockerfile-1.0.0.jar /tmp/app.jar
# 重新指定配置文件
COPY ./config/ /tmp/config
# 暴露端口
EXPOSE 8082
# 入口,java项目的启动命令
ENTRYPOINT java -jar /tmp/app.jar --spring.config.location=/tmp/config/application.yaml
参考文章:https://blog.csdn.net/lfh996/article/details/129377131