dockershellcurlneo4jdocker-entrypoint

How to execute some task in background within a docker container


I'm trying to perform some user operation(change admin-user), after Neo4j container boots up. But my background script doesn't wait for the neo4j to come up and dies before Neo4j comes online.

entrypoint.sh is something like

if [some condition]
    my_function &
fi

if [${cmd}" == "neo4j" ]; then
    exec neo4j console 
fi

helper_file.sh has my_function

function my_function {

    echo "Checking to see if Neo4j has started at http://${DB_HOST}:${DB_PORT}..."
    curl --retry-connrefused --retry 5 --retry-max-time 300  http://${DB_HOST}:${DB_PORT}
    if [ $? -ne 0 ]; then
       echo "Curl failed with error $?. Exiting.."
       return 1
    fi
    migrate_users <--- another function
}

the problem that I'm facing is Neo4j doesn't bootup till curl is doing the retries.

Tue Sep 20 12:46:35 UTC 2022 Checking to see if Neo4j has started at http://localhost:7474...
Tue Sep 20 12:46:35 UTC 2022   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Tue Sep 20 12:46:35 UTC 2022                                  Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
Tue Sep 20 12:46:35 UTC 2022 curl: (7) Failed to connect to localhost port 7474: Connection refused
Tue Sep 20 12:46:35 UTC 2022 Curl failed with error 0. Exiting..
user: vmanage; command: neo4j
Directories in use:

How can I ensure that migrate_users function gets called after Neo4j has come up online completely?

Edit:

thank you for providing the suggestion.

If I go with the background process approach, I'm seeing that Neo4j doesn't boots up, till curl queries have finished

Tue Sep 20 18:57:34 UTC 2022 Checking to see if Neo4j has started 

at  http://localhost:7474...
Tue Sep 20 18:57:34 UTC 2022 Neo4j not ready
Tue Sep 20 18:57:34 UTC 2022 Connection refused
Tue Sep 20 18:57:34 UTC 2022 config-db is not up, try to setup password again

user: vmanage; command: neo4j
Directories in use:
home:         /var/lib/neo4j
config:       /var/lib/neo4j/conf
logs:         /log
plugins:      /var/lib/neo4j/plugins
import:       /var/lib/neo4j
data:         /data
certificates: /var/lib/neo4j/certificates
licenses:     /var/lib/neo4j/licenses
run:          /var/lib/neo4j/run
Starting Neo4j.

Going to try this : https://github.com/neo4j/docker-neo4j/issues/166#issuecomment-486890785


Solution

  • Your function named my_function could use until to keep waiting for neo4j to start, for example:

    function my_function {
        let RETRIES=0
        declare SUCCESS=0
        until [[ $SUCCESS -eq 1 ]] || [[ $RETRIES -eq 50 ]]; do
          echo "Checking to see if Neo4j has started at 
          http://${DB_HOST}:${DB_PORT}..."
          STATUS_CODE=$(curl -w %{http_code} -o /dev/null -s http://${DB_HOST}:${DB_PORT})
          if [[ $STATUS_CODE -eq 200 ]]; then
            echo "Neo4j is up and running" && SUCCESS=1 && exit 0
          else
            echo "Neo4j not ready" && let RETRIES+=1 && sleep 10
          fi
        done
        migrate_users
    }