GitHub Actions cannot build docker image for spring boot app - Stack Overflow

I have a Spring Boot App which works great!I have a basic Dockerfile, and locally on my machine I can

I have a Spring Boot App which works great! I have a basic Dockerfile, and locally on my machine I can issue the build statement, and I can get an image. I now want to use GitHub Actions on my project so when there is a checkin, then we have a new image build and deployed to AWS ECS. I just started my first workflow, and when I try to build the docker image with GitHub Actions, I can get an error message:

Dockerfile:11
--------------------
 9 |     ENV APP_HOME /usr/src/app
10 |     
11 | >>> COPY target/htmx-demo-0.0.1-SNAPSHOT.jar $APP_HOME/htmx-demo-0.0.1-SNAPSHOT.jar
12 |     
13 |     WORKDIR $APP_HOME
 --------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 6a8bf038-5108-4101-bd5f-2bc4f2707329::asav9k18bmsdva1pr8t7kcu99: failed to walk /var/lib/docker/tmp/buildkit-mount3589117485/target: lstat /var/lib/docker/tmp/buildkit-mount3589117485/target: no such file or directory
Error: Process completed with exit code 1.

Dockerfile

FROM openjdk:23
EXPOSE 8888
ENV APP_HOME /usr/src/app
COPY target/htmx-demo-0.0.1-SNAPSHOT.jar $APP_HOME/htmx-demo.jar
WORKDIR $APP_HOME
ENTRYPOINT exec java -jar htmx-demo.jar

GitHub Action Workflow

name: Docker Image CI
on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:

  build:

    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v4
    - name: Build the Docker image
      run: docker build . --file Dockerfile --tag my-image-name:$(date +%s)

This is it, this is all I have just these two files. I did a general Google Search and tried to read several StackOverflow issues on this. So, any help would be much appreciated.

I have a Spring Boot App which works great! I have a basic Dockerfile, and locally on my machine I can issue the build statement, and I can get an image. I now want to use GitHub Actions on my project so when there is a checkin, then we have a new image build and deployed to AWS ECS. I just started my first workflow, and when I try to build the docker image with GitHub Actions, I can get an error message:

Dockerfile:11
--------------------
 9 |     ENV APP_HOME /usr/src/app
10 |     
11 | >>> COPY target/htmx-demo-0.0.1-SNAPSHOT.jar $APP_HOME/htmx-demo-0.0.1-SNAPSHOT.jar
12 |     
13 |     WORKDIR $APP_HOME
 --------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 6a8bf038-5108-4101-bd5f-2bc4f2707329::asav9k18bmsdva1pr8t7kcu99: failed to walk /var/lib/docker/tmp/buildkit-mount3589117485/target: lstat /var/lib/docker/tmp/buildkit-mount3589117485/target: no such file or directory
Error: Process completed with exit code 1.

Dockerfile

FROM openjdk:23
EXPOSE 8888
ENV APP_HOME /usr/src/app
COPY target/htmx-demo-0.0.1-SNAPSHOT.jar $APP_HOME/htmx-demo.jar
WORKDIR $APP_HOME
ENTRYPOINT exec java -jar htmx-demo.jar

GitHub Action Workflow

name: Docker Image CI
on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:

  build:

    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v4
    - name: Build the Docker image
      run: docker build . --file Dockerfile --tag my-image-name:$(date +%s)

This is it, this is all I have just these two files. I did a general Google Search and tried to read several StackOverflow issues on this. So, any help would be much appreciated.

Share Improve this question asked Mar 22 at 2:02 tjholmes66tjholmes66 2,0204 gold badges44 silver badges79 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The best thing I did was Google the internet and I found several pages on how to use GitHub Actions to deploy a SPring Boot App Docker Image to AWS ECS. So, it took me several examples, but I can tell you what worked for me:

Dockerfile:

FROM openjdk:23
ARG JAR_FILE=target/*.jar
COPY ./target/htmx-demo-0.0.1-SNAPSHOT.jar htmx-demo-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","htmx-demo-0.0.1-SNAPSHOT.jar"]

pom.xml does not have a <finalName> tage as it seemed I didn't need it. Some suggested it was needed and others do not.

my .github/workflows/main.yml

name: build and deploy htmx-demo

on:
  push:
    branches:
      - main

jobs:
  build-deploy:
    name: build and deploy spring-api
    runs-on: ubuntu-20.04
    steps:
      - name: checkout code
        uses: actions/checkout@v3

      - name: setup jdk 21
        uses: actions/setup-java@v3
        with:
          distribution: 'corretto'
          java-version: 21

      #- name: unit tests
       # run: mvn -B test --file pom.xml

      - name: build the app
        run: |
          mvn clean
          mvn -B package --file pom.xml -DskipTests=true
      - name: build the docker image
        uses: docker/build-push-action@v4
        with:
          context: .
          dockerfile: Dockerfile
          push: false

The section for unit tests had to be commented out. My Unit tests are Integration Tests in that they do exercise the database for real. So, when I run this locally, this works as I can access the database locally. If I run these tests in a GitHub Actions, then the app has no way of contacting the database, so I had to comment out those lines.

The database I am using is a MySQL database, and it is running in it's own container

Also, the "mvn -B package --file pom.xml" had to have the -DskipTests=true added for the same reason as the unit tests, with "mvn clean package" by itself, tests would run, and in this case we don't want them to run.

I've very used to coding and running tests locally, and then deploying to AWS EC2 instances. Now, I've taken on Docker, and I have no issues creating a Docker Image or running it locally, but this the first time I've done it with GitHub Actions, so this is a new ground for me. Not only am I learning GitHub Actions, I am also learning AWS ECS and EKS as well. I know I should deploy to EKS, but right now I think ECS and Fargate might be ok for personal projects.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744327088a4568702.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信