[英]Localhost can not be accessed on Github Actions workflow
I created a Spring sample and tried to build the application into a Docker image and run the application in a Docker container in a Github actions workflow.我创建了一个 Spring 示例并尝试将应用程序构建到 Docker 映像中,并在 Github 操作工作流中的 Docker 容器中运行该应用程序。 Then I performed a simple Smoke test to make sure the image is created as expected.
然后我执行了一个简单的冒烟测试,以确保图像按预期创建。
The Docker image is built by the Spring boot maven plugin. Docker 镜像由 Spring boot maven 插件构建。
I created a simple docker-compose file to run the application and database together.我创建了一个简单的 docker-compose 文件来一起运行应用程序和数据库。
version: "3.5" # specify docker-compose version, v3.5 is compatible with docker 17.12.0+
# Define the services/containers to be run
services:
db:
image: mysql:8
ports:
- "3306:3306"
# command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: mysecret
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_DATABASE: testdb
volumes:
- ./data/mysql:/var/lib/mysql
- ./mysql-initdb.d:/docker-entrypoint-initdb.d
app:
image: hantsy/rest-many-to-many-example
depends_on:
- db
ports:
- "8080:8080"
environment:
- "SPRING_PROFILES_ACTIVE=dev"
- "SPRING_DATASOURCE_URL=jdbc:mysql://db:3306/testdb"
In the Github actions workflow file, when running docker-compose
command, it seems it can not be accessed via localhost.在 Github 操作工作流文件中,运行
docker-compose
命令时,似乎无法通过 localhost 访问。 I created a Java test and used the curl
command for test purposes, both failed to connect to http://localhost:8080 in the same flow.我创建了一个 Java 测试并使用
curl
命令进行测试,但都无法在同一流程中连接到 http://localhost:8080。
Check out the complete Github actions workflow file .查看完整的Github 操作工作流文件。
I also tried to use PublicIP actions to get the public IP and access via the public IP, failed.我还尝试使用PublicIP 操作来获取公共 IP 并通过公共 IP 访问,但失败了。 See the public IP actions step configuration here .
请参阅此处的公共 IP 操作步骤配置。
But it worked well on my local machine, accessing http://localhost:8080 is working as expected .但它在我的本地机器上运行良好,访问 http://localhost:8080 正常工作。
It's worth noting that Public Actions seems to be more for showing you the runner IP in order to allow traffic out to another service, rather than allowing public traffic in值得注意的是,Public Actions 似乎更多是为了向您显示 runner IP 以允许流量输出到另一个服务,而不是允许公共流量进入
This action allows you to whitelist the runner's address and remove it once the pipeline finishes.
此操作允许您将运行程序的地址列入白名单,并在管道完成后将其删除。
I imagine you can might not be able to access the port because your Github action may be running in a container.我想您可能无法访问该端口,因为您的 Github 操作可能正在容器中运行。 If this is the case, then
localhost
will refer to the loopback of the container you're in, not the underlying host port.如果是这种情况,则
localhost
将引用您所在容器的环回,而不是底层主机端口。
If you're looking to simply smoke test, you may be able to achieve the same results by simply calling docker exec container_id curl localhost/status
, or your healthcheck equivalent that tests DB connectivity, etc.如果您只想进行冒烟测试,您可以通过简单地调用
docker exec container_id curl localhost/status
或测试数据库连接的健康检查等价物来获得相同的结果。
Instead of using localhost, try to use the service name, ex,不要使用 localhost,而是尝试使用服务名称,例如,
http://app:8080
or或者
app:8080
It worked for me.它对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.