'Connection refused trying to connect spring boot application in docker

I try to run spring boot application in Docker, Circle CI. Please, check how my .circleci/config.yml file looks like:

version: 2.1

jobs:

  test:
    docker:
      - image: maven:3.8.5-openjdk-17-slim

    environment:
      CHROMEDRIVER_VERSION: 100.0.4896.60
      CHROMEDRIVER_DIR: /tmp/chromedriver/
      WIKI_ENV: CIRCLE_CI

    steps:
      - checkout
      - run:
          name: "Install gnupg2, wget, xvfb and unzip"
          command: |
            apt-get update
            apt-get install -y gnupg2
            apt-get install -y wget xvfb unzip
      - run:
          name: "Install Chrome Browser"
          command: |
            wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
            echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
            apt-get update -y
            apt-get install -y google-chrome-stable
      - run:
          name: "Setup chromedriver"
          command: |
            mkdir -p $CHROMEDRIVER_DIR
            wget -q --continue -P $CHROMEDRIVER_DIR "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip"
            unzip $CHROMEDRIVER_DIR/chromedriver* -d $CHROMEDRIVER_DIR
            export PATH=$CHROMEDRIVER_DIR:$PATH
      - run:
          name: "Build jar"
          command: mvn clean package spring-boot:repackage
      - run:
          name: "Start"
          command: mvn spring-boot:start
      - run:
          name: "Check"
          command: curl http://localhost:8080

      - store_test_results:
          path: target/surefire-reports/junitreports

      - store_artifacts:
          path: target/surefire-reports/junitreports

      - store_artifacts:
          path: target/screenshots

workflows:
  default:
    jobs:
      - test

A pipeline fails on a "Check" step with the curl: (7) Failed to connect to localhost port 8080: Connection refused error.

Here is the output of the "Start" step:

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for vznd:selenium:jar:1.0
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.testng:testng:jar -> duplicate declaration of version ${testng.version} @ line 54, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] ---------------------------< vznd:selenium >----------------------------
[INFO] Building selenium 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.6.3:start (default-cli) @ selenium ---
[INFO] Attaching agents: []
18:36:09.878 [Thread-0] DEBUG org.springframework.boot.devtools.restart.classloader.RestartClassLoader - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@2c5fa29b

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.3)

2022-05-05 18:36:10.165  INFO 4520 --- [  restartedMain] vznd.selenium.SeleniumApp                : Starting SeleniumApp using Java 17.0.2 on aeadb1d9da7e with PID 4520 (/root/project/target/classes started by root in /root/project)
2022-05-05 18:36:10.166  INFO 4520 --- [  restartedMain] vznd.selenium.SeleniumApp                : No active profile set, falling back to default profiles: default
2022-05-05 18:36:10.239  INFO 4520 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-05-05 18:36:10.239  INFO 4520 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-05-05 18:36:11.021  INFO 4520 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-05-05 18:36:11.034  INFO 4520 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-05-05 18:36:11.035  INFO 4520 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-05-05 18:36:11.086  INFO 4520 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-05-05 18:36:11.086  INFO 4520 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 846 ms
2022-05-05 18:36:11.320  INFO 4520 --- [  restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
2022-05-05 18:36:11.418  INFO 4520 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2022-05-05 18:36:11.459  INFO 4520 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-05-05 18:36:11.468  INFO 4520 --- [  restartedMain] vznd.selenium.SeleniumApp                : Started SeleniumApp in 1.58 seconds (JVM running for 2.136)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.129 s
[INFO] Finished at: 2022-05-05T18:36:11Z
[INFO] ------------------------------------------------------------------------

Here is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>vznd</groupId>
    <artifactId>selenium</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <maven-surefire-plugin.version>3.0.0-M6</maven-surefire-plugin.version>
        <testng.version>7.5</testng.version>
        <selenium-java.version>3.141.59</selenium-java.version>
        <commons-io.version>2.11.0</commons-io.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium-java.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <systemPropertyVariables>
                        <webdriver.chrome.driver>/tmp/chromedriver/chromedriver</webdriver.chrome.driver>
                    </systemPropertyVariables>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

When I run this commands locally - it works. When it executes on CI - it fails. What I tried to fix:

  1. Run java -jar target/selenium.jar & command on the "Start" step
  2. Run mvn spring-boot:run & command instead of "Build jar" and "Start" steps
  3. Run nohup mvn spring-boot:run & command instead of "Build jar" and "Start" steps
  4. Add sleep 120 step after the "Start" step

Could you please advise me what I am doing wrong?



Solution 1:[1]

Moved to a gitlab CI, the problem is not actual anymore.

Solution 2:[2]

Pretty sure you are curl'ing before server even started, so try to wait for server to start with

- run:
    name: wait
    command: sleep 10

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitriy
Solution 2 Kirill Vizniuk