简体   繁体   English

如何使用一个Dockerfile对Spring Boot应用程序进行Docker化?

[英]How to dockerizing a spring boot app with one Dockerfile?

From the official guide , we can build a docker image by maven or gradle. 根据官方指南 ,我们可以通过maven或gradle构建docker映像。 It saying we need a Dockerfile and then run the maven or gradle command to generate a runable docker image: 它说我们需要一个Dockerfile,然后运行maven或gradle命令以生成可运行的Docker映像:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

But in my use case is that the cloud vendor requires that we just supplying one Dockerfile and they will build an Docker image from my code base based on the supplied Dockerfile. 但是在我的用例中,云供应商要求我们仅提供一个Dockerfile,他们将根据提供的Dockerfile从我的代码库构建Docker映像。 And there is no chance to run my own build script. 而且没有机会运行自己的构建脚本。 So how can I handle this situation? 那么我该如何处理呢? Is there way to build a runable docker image from just a Dockerfile? 有没有办法仅从Dockerfile构建可运行的Docker映像?

Use the RUN command inside your Dockerfile to perform any action at build time. 在Dockerfile中使用RUN命令在构建时执行任何操作。 Eg: 例如:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
RUN echo "something" && \
  echo "then print this" && \
  echo "and so on.. any shell command can be performed here"
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Avoid multiple RUN commands. 避免使用多个RUN命令。 A new overlay layer is created for each one, which increases the size of your final image. 为每个图层创建一个新的覆盖层,这会增加最终图像的大小。

It's good to know how an image is created to understand. 最好了解如何创建图像以供理解。 When you do docker build . 当你做docker build . , your docker client will copy everything that is in the directory of your Dockerfile to the docker host. ,您的Docker客户端会将Dockerfile目录中的所有内容复制到Docker主机。 Then docker creates a container from the FROM instruction, and simply follows the instructions in the Dockerfile to modify that container. 然后docker根据FROM指令创建一个容器,并简单地遵循Dockerfile中的指令修改该容器。 Once the instructions are completed, docker creates an image from the state of that temporary container and discards the container. 指令完成后,泊坞窗从该临时容器的状态创建映像,并丢弃该容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM