Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

I created a simple docker-compose file to run the application and database together.

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. 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.

Check out the complete Github actions workflow file.

I also tried to use PublicIP actions to get the public IP and access via the public IP, failed. See the public IP actions step configuration here.

But it worked well on my local machine, accessing http://localhost:8080 is working as expected.

like image 281
Hantsy Avatar asked Jul 23 '26 01:07

Hantsy


1 Answers

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

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. If this is the case, then localhost will refer to the loopback of the container you're in, not the underlying host port.

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.

like image 108
TheQueenIsDead Avatar answered Jul 24 '26 22:07

TheQueenIsDead



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!