I have a maven project that I am trying to deploy to Amazon Web Services' Elastic Beanstalk
. I want to deploy using a Docker
file.
The Docker
file works locally on my Windows PC, but not when deployed to Elastic Beanstalk
.
The structure of my project looks like this:
~/my-project/
|-- Dockerfile
|-- src
|-- pom.xml
This is my Docker
file:
# Build stage
#
FROM maven:3.8.1-jdk-8 AS build
ADD src /tmp/src
ADD pom.xml /tmp/pom.xml
RUN mvn -f /tmp/pom.xml clean package
#
# Package stage
#
FROM openjdk:8
COPY --from=build /tmp/target/my-project-host-0.0.1-SNAPSHOT.jar /usr/local/lib/my-project.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/local/lib/my-project.jar"]
To use and execute this Docker
file locally, all I do is run these commands from project root and then my Springboot server is up and running:
docker build -t image-my-project .
docker run -d -p 8080:8080 image-my-project
Now comes the problem. Why does this fail when deployed to AWS Elastic Beanstalk
.
I will give a walkthrough of exactly how I am deploying it to Elastic Beanstalk
.
1) navigate to project root (my-project) and run the command:
a) eb create
b) this will prompt me with configuring the environment.
2) Set up environment with the following configs:
a) name of environment: my-project
a) load balancer type = 'application' (Should this be 'network' instead?)
3) After setting up the configs, the project begins to deploy but it always fails.
If I download the eb-engine.log
file, then I get errors stating that the docker failed to pull Maven. Here is that part of the log file:
start build docker app
2021/11/04 12:31:25.779536 [INFO] fetch image name
2021/11/04 12:31:25.779584 [INFO] pull docker image if update is not false in Dockerrun.aws.json
2021/11/04 12:31:25.779604 [INFO] Running command /bin/sh -c docker pull maven:3.8.1-jdk-8 AS build
2021/11/04 12:31:25.817783 [WARN] failed to execute command: docker pull maven:3.8.1-jdk-8 AS build, retrying...
2021/11/04 12:31:25.817814 [INFO] Running command /bin/sh -c docker pull maven:3.8.1-jdk-8 AS build
2021/11/04 12:31:25.859856 [ERROR] An error occurred during execution of command [app-deploy] - [Docker Specific Build Application]. Stop running the command. Error: failed to pull docker image: Command /bin/sh -c docker pull maven:3.8.1-jdk-8 AS build failed with error exit status 1. Stderr:"docker pull" requires exactly 1 argument.
See 'docker pull --help'.
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
2021/11/04 12:31:25.859878 [INFO] Executing cleanup logic
2021/11/04 12:31:25.859986 [INFO] CommandService Response: {"status":"FAILURE","api_version":"1.0","results":[{"status":"FAILURE","msg":"Engine execution has encountered an error.","returncode":1,"events":[{"msg":"Instance deployment failed to download the Docker image. The deployment failed.","timestamp":1636029085,"severity":"ERROR"},{"msg":"Instance deployment failed. For details, see 'eb-engine.log'.","timestamp":1636029085,"severity":"ERROR"}]}]}
2021/11/04 12:31:25.860367 [INFO] Platform Engine finished execution on command: app-deploy
Anyone have any idea what is going on? Why does my Docker file work locally, but won't deploy to AWS Elastic Beanstalk?
CodePudding user response:
I figured it out so I will leave an answer for future readers. Turns out AWS doesn't allow you to use the AS
keyword in Docker. So I changed the file from:
# Build stage
#
FROM maven:3.8.1-jdk-8 AS build
ADD src /tmp/src
ADD pom.xml /tmp/pom.xml
RUN mvn -f /tmp/pom.xml clean package
#
# Package stage
#
FROM openjdk:8
COPY --from=build /tmp/target/my-project-host-0.0.1-SNAPSHOT.jar /usr/local/lib/my-project.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/local/lib/my-project.jar"]
To this:
# Build stage
#
FROM maven:3.8.1-jdk-8
ADD src /tmp/src
ADD pom.xml /tmp/pom.xml
RUN mvn -f /tmp/pom.xml clean package
#
# Package stage
#
FROM openjdk:8
COPY --from=0 /tmp/target/my-project-host-0.0.1-SNAPSHOT.jar /usr/local/lib/my-project.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/local/lib/my-project.jar"]