bashspring-bootdocker-composesh

After running bash script (.sh) file, the process does not come back to the docker compose entry point


I have some spring boot microservices. I want to run them using docker compose. To be sure that services run in order, I have used a .sh file to run one of them before others because other microservices depend on it. The bash script file runs very well but it stops the caller container. I searched on Internet and realized that some commands must be added at the end of the bash script file: /cnb/process/web or ./cnb/lifecycle/launcher in order to continue the process. But this message is shown: /cnb/process/web: No such file or directory.

Spring boot version: 3.2.2

Docker compose: 3.8

.sh file:

#!/bin/bash
# check-config-server-started.sh

apt-get update -y
yes | apt-get install curl

curlResult=$(curl -s -o -I -w "%{http_code}" http://cloud-config-server:8888/actuator/health)

echo "result status code:" "$curlResult"

while [[ ! $curlResult == "200" ]]; do
  >&2 echo "Config server is not up yet!"
  sleep 2
  curlResult=$(curl -s -o -I -w "%{http_code}" http://cloud-config-server:8888/actuator/health)
done
/cnb/process/web
#./cnb/lifecycle/launcher

Docker compose file:

version: "3.8"

services:
  cloud-config-server:
    container_name: cloud-config-server-container
    build:
      dockerfile: ./cloud-config-server/Dockerfile
    ports:
      - "8888:8888"
    environment:
      - "SERVER_PORT=8888"
    networks:
      my-compose-net:
    restart: on-failure

  webapp:
    container_name: webapp-container
    build:
      dockerfile: ./webapp/Dockerfile
    ports:
      - "8081:8081"
    volumes:
      - "./check-config-server-started.sh:/usr/local/bin/check-config-server-started.sh"
    user: root
    entrypoint: [ "check-config-server-started.sh" ]
    environment:
      - "SPRING_CLOUD_CONFIG_URI=http://cloud-config-server:8888"
    depends_on:
      - cloud-config-server
    networks:
      my-compose-net:
    restart: on-failure

networks:
  my-compose-net:

Solution

  • I have faced with the same problem. I mean keeping containers running after executing .sh files by adding /cnb/process/web to the .sh file. It seems the problem originates from the way you create your docker image. In the docker compose file instead of using

    build: dockerfile: ./Dockerfile

    I create docker images using spring boot pom.xml by adding these codes:

    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring-boot.version}</version>
                    <configuration>
                        <image>
                            <name>${project.groupId}/my.service:${project.version}</name>
                        </image>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>install</phase>
                            <goals>
                                <goal>build-image</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    

    then mvn clean install to create an image.

    after that you can refer to the image in the docker compose:

    image: your-image-name
    

    instead of

    build:
      dockerfile: ./Dockerfile