The Dockerfile is the recipe for creating an image. In the current file, EXPOSE 8080 describes the application port and ENTRYPOINT starts the Spring Boot class. The Docker image should be built for linux/amd64 to match the Fargate x86_64 environment.
The current Dockerfile copies the target/dependency/BOOT-INF and target/dependency/META-INF directories. If these directories do not exist after the Maven build, docker build will fail. To make the lab accessible, you can use a direct-JAR Dockerfile after aligning the JAR file name with pom.xml. Do not proceed to ECR before running the container locally.
1. Create a .dockerignore file to specify files and directories that Docker will ignore when packaging (building) a Docker image from the project components.
.dockerignore with the following content:.git
.gitignore
.dockerignore
.vscode
*.class
README.md

2. Build JAR file and extract
Open Terminal in VS Code (Ctrl + ~), navigate to the .../fcaj-spring directory and run the following commands:
.\mvnw.cmd clean package -DskipTests

target/dependency directory:mkdir -Force target\dependency
tar -xf (Get-Item target\*.jar).FullName -C target\dependency
Verification: Inside the target/dependency/ folder, 3 subdirectories will appear: BOOT-INF, META-INF, and org

3. Create Dockerfile
In the root directory of the project, create a file named Dockerfile (without any extension).
The variable DEPENDENCY = target/dependency points to the directory containing Maven’s extracted build artifacts:
FROM eclipse-temurin:25-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
EXPOSE 8080
ENTRYPOINT ["java", "-cp", "app:app/lib/*", "com.example.fcaj_spring.FcajSpringApplication"]

Important notes:
4. Build Docker Image in VS Code Terminal Ensure Docker Desktop is running, then execute the following command in the VS Code terminal:
docker build --platform linux/amd64 -t productsservice:1.0.0 .
Note the dot . at the end of the command representing the build context in the current directory.

5. Check Docker Image
docker images

In this step, we will run a Docker container from the newly built productsservice:1.0.0 image, verify the application running on the local environment on port 8080, and view container logs.
1. Launch Docker container
Open Terminal in VS Code (Ctrl + ~) and run the container start command:
docker run -d --name productsservice-app -p 8080:8080 productsservice:1.0.0
Explanation of parameters:
-d: Run container in detached mode (background), without occupying the terminal window.
–name productsservice-app: Assigns an easily manageable name to the container.
-p 8080:8080: Maps port 8080 on the host machine to port 8080 inside the container.
productsservice:1.0.0: The image name created in lab 2.4.

2. Follow container logs
After running in the background, you have 2 ways to view the Spring Boot startup logs inside the container:
docker logs -f productsservice-app

(Press Ctrl + C to exit the log viewing screen without stopping the container).
Option 2 (Using the Docker Extension UI in VS Code):
Expand the Containers section.
You will see the container productsservice-app with a green status (Running).
Right-click on that container -> select View Logs. VS Code will open a dedicated Terminal tab showing real-time logs.

When you see the familiar log line:
Started FcajSpringApplication in ... seconds
=> The Spring Boot application inside Docker has started successfully.

This error occurs due to logging library conflicts.
Currently, both Logback (default Spring Boot logging) and Log4j2 exist simultaneously on the classpath. This happens because spring-boot-starter-logging was not completely excluded from all dependent starters (e.g., both spring-boot-starter-web and spring-boot-starter-actuator pull in starter-logging), or old Logback jar files remain in the target/dependency/BOOT-INF/lib folder.
To resolve this error, open pom.xml, check if you are using spring-boot-starter-actuator or any other starters besides spring-boot-starter-web. Add <exclusions> for all those starters.


docker rm -f productsservice-app

.\mvnw.cmd clean package -DskipTests
Remove-Item -Recurse -Force target\dependency -ErrorAction SilentlyContinue
mkdir -Force target\dependency
tar -xf (Get-Item target\*.jar).FullName -C target\dependency

docker build --no-cache --platform linux/amd64 -t productsservice:1.0.0 .

docker run -d --name productsservice-app -p 8080:8080 productsservice:1.0.0
docker logs -f productsservice-app

3. Verify API (Verification) Open Postman or a Web Browser and test the endpoints:
URL: http://localhost:8080/actuator/health
Method: GET
Expected Result: Status 200 OK, JSON {“status”: “UP”}.
4. Stop local container to prevent port conflicts in subsequent steps
docker stop productsservice-app
You have finished building and running the Docker image on your local machine. You are now ready to push the application to ECR.
If you encounter errors, you can clean the build directory following the steps starting from 1. Remove the currently broken container: