ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 668a7264-5d0b-45a6-b547-fa8fff014bda::g00ukurq2ipxuvrrz8rnpyskp: "/swagger2-demo-0.0.1-SNAPSHOT.jar": not found
[root@localhost /]# docker build -f ./home/docker/Dockerfile -t api .
[+] Building 0.0s (6/8)
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 410B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/java:8 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 2B 0.0s
=> [1/4] FROM docker.io/library/java:8 0.0s
=> ERROR [2/4] ADD swagger2-demo-0.0.1-SNAPSHOT.jar app.jar 0.0s
------
> [2/4] ADD swagger2-demo-0.0.1-SNAPSHOT.jar app.jar:
------
Dockerfile:3
--------------------
1 | FROM java:8
2 | VOLUME /tmp
3 | >>> ADD swagger2-demo-0.0.1-SNAPSHOT.jar app.jar
4 | # 设置时区
5 | RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
--------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 668a7264-5d0b-45a6-b547-fa8fff014bda::g00ukurq2ipxuvrrz8rnpyskp: "/swagger2-demo-0.0.1-SNAPSHOT.jar": not found
玄学,目前看,进入Dockerfile路径下,再build就可以,直接用-f 指定路径还是找不到
docker build 命令 :
docker build -t api .
终端
[root@localhost /]# cd /home
[root@localhost home]# ls
docker redis website za
[root@localhost home]# cd docker
[root@localhost docker]# ls
bk docker-compose.yml Dockerfile nginx swagger2-demo-0.0.1-SNAPSHOT.jar
[root@localhost docker]# docker build -t api .
[+] Building 0.0s (9/9) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 410B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/java:8 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 115B 0.0s
=> [1/4] FROM docker.io/library/java:8 0.0s
=> CACHED [2/4] ADD swagger2-demo-0.0.1-SNAPSHOT.jar app.jar 0.0s
=> CACHED [3/4] RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 0.0s
=> CACHED [4/4] RUN echo 'Asia/Shanghai' >/etc/timezone 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:9d4269948f2d634446a5180c8b0d7fb5219f6246c5cf8a5aa437ce8354ca588e 0.0s
=> => naming to docker.io/library/api 0.0s
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
api latest 9d4269948f2d 15 minutes ago 725MB
swagger-demo latest 9d4269948f2d 15 minutes ago 725MB
nginx latest 7d3c40f240e1 8 days ago 143MB
redis latest 0ec8ab59a35f 4 weeks ago 117MB
hello-world latest 9c7a54a9a43c 6 weeks ago 13.3kB
openjdk 17 5f94f53bbced 18 months ago 471MB
mysql latest 3218b38490ce 18 months ago 516MB
mysql 8.0.23 cbe8815cbea8 2 years ago 546MB
java 8 d23bdf5b1b1b 6 years ago 643MB
这种一般是,build时候就有问题,Dockerfile 有问题。
FROM java:8
VOLUME /tmp
ADD swagger2-demo-0.0.1-SNAPSHOT.jar app.jar
# 设置时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
# 暴露17080端口
EXPOSE 8081
ENTRYPOINT [ "java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar" ]
这里ADD 打包的jar包 build后的jar 包
一般还是因为路径问题找不到你打包的jar 包导致也找不到app.jar,
所以还有回去重新打包,看Dockerfile文件
我之前是没有明确jar包,改成
ARG JAR_RELEASE_PATH=swagger2-demo-0.0.1-SNAPSHOT.jar
== docker 运行命令 ==
docker run -d -p 8090:8090 --name music api
FROM java:8
ARG JAR_RELEASE_PATH
ARG JAR_POSE=8081
RUN echo "ARGS is ${JAR_RELEASE_PATH}"
COPY ${JAR_RELEASE_PATH} app.jar
ENTRYPOINT ["java","-jar","-Duser.timezone=GMT+8","/app.jar"]
EXPOSE ${JAR_POSE}
启动成功示意
[root@localhost docker]# docker run -d -p 8081:8081 --name music api
f57245ddf45b7709906dadc8d663244079f04154677eb2a1a02d9bafcc0e1a55
[root@localhost docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f57245ddf45b api "java -Djava.securit…" 3 seconds ago Up 2 seconds 0.0.0.0:8081->8081/tcp, :::8081->8081/tcp music
4cc0e3a49eff redis "docker-entrypoint.s…" 8 days ago Up 40 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp myredis
后端部署流程
在/home/docker 路径下,尽量用root用户
1.给项目打jar包
打好的包在target里
通过xftp传到虚拟机里
注意如果没有docker-compose文件,需要提前在application.yml里修改mysql的地址为ip地址而不是localhost
2.编写Dockerfile文件
FROM java:8
VOLUME /tmp
ADD swagger2-demo-0.0.1-SNAPSHOT.jar app.jar
# 设置时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
# 暴露17080端口
EXPOSE 8081
ENTRYPOINT [ "java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar" ]
3.build jar包
docker build -t api .
## .是指当前目录,所以要cd到Dockerfile的目录下
4. 查看镜像
docker images
5.运行镜像
docker run -d -p 8081:8081 --name httpapi api
## -d:后台运行该容器 ,-p:容器内部端口随机映射到主机的端口
docker ps
查看运行容器命令
docker ps -a
查看全部容器
docker rm 容器ID
删除 容器
docker rmi 镜像ID
删除镜像
mkdir 文件名
新建文件
mv 原文件地址 现文件地址
移动/修改文件名
然后主机就可以访问swagger了
访问地址:
http://192.11.11(虚拟机ip地址):8081/swagger-ui.html#/