将 Docker 部署到 AWS Elastic Beanstalk:无法执行命令“docker pull maven”
Posted
技术标签:
【中文标题】将 Docker 部署到 AWS Elastic Beanstalk:无法执行命令“docker pull maven”【英文标题】:Deploying Docker to AWS Elastic Beanstalk: failed to execute command 'docker pull maven' 【发布时间】:2021-12-18 16:50:25 【问题描述】:我有一个 Maven 项目,我正在尝试将其部署到 Amazon Web Services 的Elastic Beanstalk
。我想使用Docker
文件进行部署。
Docker
文件在我的 Windows PC 上本地运行,但在部署到 Elastic Beanstalk
时无法运行。
我的项目结构如下:
~/my-project/
|-- Dockerfile
|-- src
|-- pom.xml
这是我的Docker
文件:
# 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"]
要在本地使用和执行这个 Docker
文件,我所做的就是从项目根目录运行这些命令,然后我的 Springboot 服务器启动并运行:
docker build -t image-my-project .
docker run -d -p 8080:8080 image-my-project
现在问题来了。为什么部署到AWS Elastic Beanstalk
时会失败。
我将详细介绍如何将其部署到 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.
如果我下载 eb-engine.log
文件,则会收到错误消息,指出 docker 无法拉取 Maven。这是日志文件的那部分:
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
有人知道发生了什么吗?为什么我的 Docker 文件可以在本地运行,但无法部署到 AWS Elastic Beanstalk?
【问题讨论】:
ElasticBeanstalk 泊坞窗似乎无法理解 Dockerfile 的AS build
部分。不知道为什么会这样
【参考方案1】:
我想通了,所以我会为以后的读者留下答案。事实证明 AWS 不允许您在 Docker 中使用 AS
关键字。所以我改变了文件:
# 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"]
到这里:
# 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"]
【讨论】:
以上是关于将 Docker 部署到 AWS Elastic Beanstalk:无法执行命令“docker pull maven”的主要内容,如果未能解决你的问题,请参考以下文章
如何将具有多个 Docker 容器的应用程序部署到 AWS Elastic Beanstalk?
sh 构建Docker镜像,将其推送到AWS EC2 Container Registry,然后将其部署到AWS Elastic Beanstalk
是否建议将 Docker 与 AWS Elastic beanstalk 一起使用?
使用 Docker 的开发环境并部署到 AWS Elastic Beanstalk