file not found in build context or excluded by .dockerignore:stat target/Test.jar file does not exit

前言

最近在给一个古老的k8s服务做Nacos升级,升级完成之后,在CI平台构建报错,报错信息如下

06-02 18:33:48 开始docker构建
06-02 18:33:49 + docker build -t art.aiqinhai.com/cbg-docker-repo/knk1rqqjxtv10/testservicek8s:v1.0_1022 -f ./thyx_test-k8s/Dockerfile .
06-02 18:33:49 Sending build context to Docker daemon     71MB

06-02 18:33:49 Step 1/5 : FROM art.aiqinhai.com/cbg-docker-repo/thyx/openjdk:jdk-8u312
06-02 18:33:49  ---> qwqeqwewqeq
06-02 18:33:49 Step 2/5 : ENV TZ "Asia/Shanghai"
06-02 18:33:49  ---> Using cache
06-02 18:33:49  ---> qwewqewqeew
06-02 18:33:49 Step 3/5 : ARG JAR_FILE=target/THYX_TestService.jar
06-02 18:33:49  ---> Running in rwerwrwrerw33
06-02 18:33:49 Removing intermediate container rwerwrwrerw33
06-02 18:33:49  ---> qwewqewqeew
06-02 18:33:49 Step 4/5 : COPY ${JAR_FILE} THYX_TestService.jar
06-02 18:33:49 COPY failed: file not found in build context or excluded by .dockerignore: stat target/THYX_TestService.jar: file does not exist

问题排查

从上面的日志看,开始构建镜像,第四步在拷贝jar的时候没有找到对应的jar包文件。后来发现是服务构建的jar包名字有问题,打开pom.xml文件,可以看到 <finalName>THYX-TestService</finalName>

    <build>
        <finalName>THYX-TestService</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

所以把pom.xml中的finalName和镜像文件中的服务名改成THYX_TestService保持一致即可。

Docker Build命令

Docker build命令用于构建Docker镜像。它从Dockerfile文件中读取指令,并根据这些指令来构建镜像。Dockerfile是一个文本文件,其中包含了一系列的指令,用于描述如何构建Docker镜像。Docker build命令的一般语法如下:

docker build [OPTIONS] PATH | URL | -

其中,OPTIONS参数用于指定一些构建选项,如镜像标签、构建上下文等;PATH参数用于指定Dockerfile文件所在的路径;URL参数用于指定Dockerfile文件的URL地址;“-”参数用于指定标准输入作为Dockerfile文件输入。下面是Docker build命令的一些常用选项:

  • -t, --tag:为镜像指定一个名称和标签;
  • -f, --file:指定Dockerfile文件的路径或URL地址;
  • -m, --memory:设置Docker build命令的内存限制;
  • --build-arg:设置构建参数,可以在Dockerfile文件中使用。
# 注意 
# 1.后面使用的是两个点,指定在上一级目录运行docker,如果是一个点时指在当前目录
# 2.imagename 中不能使用大写字母
sudo docker build -t imagename -f Dockerfile ..

猜你喜欢

转载自blog.csdn.net/qq_28165595/article/details/131032888