且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将 Spring-Boot Web 服务转换为 Docker 映像?

更新时间:2022-05-25 01:12:23

当您运行 Docker 容器时,默认情况下不会发布任何应用程序在其中侦听的所有端口.

When you run a Docker container, all the ports, which any application happen to listen on within it, are not published by default.

为了发布一个端口,你需要在使用你的镜像运行容器时指定它.有关如何执行此操作的所有详细信息,您可以查看 docker run 命令文档中的EXPOSE"部分:https://docs.docker.com/engine/reference/run/

In order to publish a port, you need to specify it while running the container using your image. For all the details on how to do it, you can check the "EXPOSE" section in the documentation of docker run command: https://docs.docker.com/engine/reference/run/

简而言之,您想在运行容器时添加另一个选项:

Shortly speaking, you want to add another option while running your container:

docker run --name mycontainer1 -p 8080:8080 myimage1

我不确定你是否想通过添加来实现这一点

I'm not sure if you wanted to achieve this by adding

EXPOSE 8080

在您的 Dockerfile 中.实际上,这并不意味着在使用镜像运行容器时会暴露端口.您可能会在 Dockerfile 参考中找到:

in your Dockerfile. Actually, this does not mean that the port will be exposed when the image is used to run a container. As you might find in Dockerfile reference:

EXPOSE 指令实际上并不发布端口.它充当构建映像的人和运行容器的人之间的一种文档类型,关于打算发布哪些端口.要在运行容器时实际发布端口,请使用 docker run 上的 -p 标志发布和映射一个或多个端口,或使用 -P 标志发布所有暴露的端口并将它们映射到高阶端口.

The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.