How to execute some task in background within a docker container - docker

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

You can add a loop inside your script to check the health of neo4j container. If the health check get pass only proceeed further in you script otherwise loop untill it pass.

You can use docker-compose with the depends_on + condition to do that.
Even docker-compose documentation recommends to implement some kind of script to wait until the service is up. Take a look to the following links docker-compose and stackoverflow
But it could be something like:
version: "2"
services:
neo4j-admin:
build: .
depends_on:
- "neo4j"
command: ["./wait-for-it.sh","--", "sh", "change_admin_passwd.sh"]
neo4j:
image: neo4j

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
}

Related

Setting up cron job to launch docker containers on ec2

I am trying to set up regular task on an amazon ec2 instance that will launch a few docker containers.
I ve created a startup_service.sh script in my home directory :
cd ~
docker-compose pull && docker-compose up
In that very same home directory, I have a docker-compose.yml file that defines my containers and image.
I have tested this file sh startup_service.sh and it is working as expected
Ive added execute permission to this startup_service.sh file and created a cronjob with crontab -e :
50 11 * * * /usr/bin/sh /home/ec2-user/startup_service.sh
However it is not working ( running docker ps doesnt show any containers being created or anything).
However checking the cron logs, it seems like the task is actually executed sudo grep -C 3 "startup" /var/log/cron :
Feb 27 11:42:01 ip-172-31-27-90 crond[3188]: (ec2-user) RELOAD (/var/spool/cron/ec2-user)
Feb 27 11:44:00 ip-172-31-27-90 crontab[18011]: (ec2-user) LIST (ec2-user)
Feb 27 11:48:54 ip-172-31-27-90 crontab[18064]: (ec2-user) LIST (ec2-user)
Feb 27 11:50:01 ip-172-31-27-90 CROND[18153]: (ec2-user) CMD (/usr/bin/sh /home/ec2-user/startup_service.sh)
Feb 27 11:50:02 ip-172-31-27-90 CROND[18156]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Feb 27 11:53:05 ip-172-31-27-90 crontab[18232]: (ec2-user) LIST (ec2-user)
Feb 27 12:00:01 ip-172-31-27-90 CROND[18343]: (root) CMD (/usr/lib64/sa/sa1 1 1)
How can I correctly setup this cron job ?

docker run does absolutely nothing, no log created

I am attempting to run the following docker container:
https://hub.docker.com/r/bgruening/pubmedportable/
I am doing so using the following command:
sudo docker run -d -v /home/$USER/docker_pubmedportable/:/export/ -p 9999:5432 bgruening/pubmedportable
The only output I get is immediately returned:
9b76caddaddbe262bf30d3edbab30da9fa29b9e5f1ad3a4148e753f4e5e929bd
And that is all that is done. There should be a postgres server that is instantiated/created, filled with data, and then hosted at the port 9999 on localhost.
I tried looking at the logs via:
docker logs -f 9b76caddaddbe262bf30d3edbab30da9fa29b9e5f1ad3a4148e753f4e5e929bd
However, this also returns no information.
Also, running docker ps provides absolutely nothing after the commands are issued.
It is my understanding that docker containers are supposed to "just work" on any platform, with little to no effort required.
However, this docker container has not been able to create and host this database and does not appear to be running at all.
Is there a method to determine which section of the docker container is causing a problem?
The OS is archlinux.
Probably some error is making the container exits.
Run it without the -d option, so you can see the log.
I was able to bring up the container with your command. I adapted the path to my environment.
..[$] <()> docker run -d -v ${pwd}:/export/ -p 9999:5432 bgruening/pubmedportable
1d21b00a5fdd376016bb09aeb472a295b86f74aea385a609ca8b33a0ba87f306
..[$] <()> docker logs 1d21b00a5fdd376016bb09aeb472a295b86f74aea385a609ca8b33a0ba87f306
Starting PostgreSQL 9.1 database server: main.
Initialized with 4 processes
######################
###### Finished ######
######################
programme started - Sat Sep 15 04:47:35 2018
programme ended - Sat Sep 15 04:47:36 2018
/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/elements.py:3779: SAWarning: Textual SQL expression '\n SELECT \n ...' should be explicitly declared as text('\n SELECT \n ...') (this warning may be suppressed after 10 occurrences)
{"expr": util.ellipses_string(element)})
-------------
processing files from year 1809 to 2016
-------------
got articles from PostgreSQL database
-------------
now indexing articles in Xapian
-------------
no search of synonyms performed, use "python RunXapian.py -h" for parameter view
2017-06-01 00:50:17 UTC LOG: aborting any active transactions
2017-06-01 00:50:17 UTC LOG: autovacuum launcher shutting down
2017-06-01 00:50:17 UTC LOG: shutting down
2017-06-01 00:50:17 UTC LOG: database system is shut down
2018-09-15 04:47:34 UTC LOG: database system was shut down at 2017-06-01 00:50:17 UTC
2018-09-15 04:47:34 UTC LOG: database system is ready to accept connections
2018-09-15 04:47:34 UTC LOG: autovacuum launcher started
2018-09-15 04:47:34 UTC LOG: incomplete startup packet
2018-09-15 04:47:36 UTC LOG: could not receive data from client: Connection reset by peer
2018-09-15 04:47:36 UTC LOG: unexpected EOF on client connection
..[$] <()> psql -h localhost -p 9999 -U parser pubmed
Password for user parser:
psql (10.5, server 9.1.24)
SSL connection (protocol: TLSv1.2, cipher: DHE-RSA-AES256-GCM-SHA384, bits: 256, compression: on)
Type "help" for help.
pubmed=#

Change jenkins timezone master/slave?

I have a jenkins configuration as follows:
Master (ubuntu)
~$ date
Tue Mar 7 08:35:06 UTC 2017
slave (redhat)
# date
Tue Mar 7 08:36:10 PST 2017
In jenkins system information the master shows (should show pacific):
user.timezone GMT
Even though I have placed these lines from jenkins wiki:
JENKINS_JAVA_OPTIONS="-Duser.timezone=America/Los_Angeles"
JAVA_ARGS="-Djava.awt.headless=true -Dorg.apache.commons.jelly.tags.fmt.timeZone=America/Los_Angeles"
And the strangest part is when I check the node configuaration in jenkins it shows my redhat slave node is 8 hours ahead.
My redhat slaves time ideally shouldn't be changed because it would screw with DB writes that I will be doing in testing. However I am completely stumped so any information would be helpful.
Where did you placed the JAVA env strings? Could you ensure that they are being used:
su jenkins
echo $JAVA_ARGS
echo $JENKINS_JAVA_OPTIONS
My problem was my UTC time was off. When I did timedatectl it showed NTP was working and UTC was in sync but it was lying. My labs firewall blocks the NTP port and I ended up finding this magic command to sync my clock:
sudo date -s "$(wget -S "http://www.google.com/" 2>&1 | grep -E '^[[:space:]]*[dD]ate:' | sed 's/^[[:space:]]*[dD]ate:[[:space:]]*//' | head -1l | awk '{print $1, $3, $2, $5 ,"GMT", $4 }' | sed 's/,//')"
It works well I just have to

Container exits if invoked from compose

I have a dockerized server process that merely listens on a port 5000
[admin#gol05854 compose]$ cat ../proc1/server.sh
#!/bin/sh
echo `date` "Starting server"
nc -v -l -p 5000
echo `date` "Exiting server"
I have a client that is expected to continuously send messages to the server:
[admin#gol05854 compose]$ cat ../client/client.sh
#!/bin/sh
echo `date` "Starting client"
while true
do
date
done | nc my_server 5000
echo `date` "Ending client"
I start these together using compose. However, the server exits with following messages:
[admin#gol05854 compose]$ docker logs e1_my_server_1
Wed Oct 26 04:10:34 UTC 2016 Starting server
listening on [::]:5000 ...
connect to [::ffff:172.27.0.2]:5000 from e1_my_client_1_1.e1_default:36500 ([::ffff:172.27.0.3]:36500)
Wed Oct 26 04:10:36 UTC 2016
Wed Oct 26 04:10:36 UTC 2016
Wed Oct 26 04:10:36 UTC 2016
Wed Oct 26 04:10:36 UTC 2016
Wed Oct 26 04:10:36 UTC 2016
Exiting server
What is surprising is that if the same containers are started without compose, using docker run, the server remains running.
What is it that docker compose does that causes the server to exit after receiving a few messages?
The code can be found at https://github.com/yashgt/dockerpoc

PostgreSQL wrong ownership

I'll preface this with the fact that I haven't used PostgreSQL much. I tried using it with RoR but the fact that it uses an ORM, I never got why PostgreSQL was the flavor of choice.
After fighting with getting the damn thing installed on Ubuntu 14.04, I need to clone a repo that depends on it.
After about 30 minutes of dealing trying a few things, I discovered:
$ /usr/lib/postgresql/9.4/bin/postgres -d 3 -D /var/lib/postgresql/9.4/main -c config_file=/etc/postgresql/9.4/main/postgresql.conf
LOG: skipping missing configuration file "/var/lib/postgresql/9.4/main/postgresql.auto.conf"
2015-02-14 21:05:01 PST [7665-2] FATAL: data directory "/var/lib/postgresql/9.4/main" has wrong ownership
2015-02-14 21:05:01 PST [7665-3] HINT: The server must be started by the user that owns the data directory.
2015-02-14 21:05:01 PST [7665-4] DEBUG: shmem_exit(1): 0 before_shmem_exit callbacks to make
2015-02-14 21:05:01 PST [7665-5] DEBUG: shmem_exit(1): 0 on_shmem_exit callbacks to make
2015-02-14 21:05:01 PST [7665-6] DEBUG: proc_exit(1): 0 callbacks to make
2015-02-14 21:05:01 PST [7665-7] DEBUG: exit(1)
One, I don't know what this auto.conf file it's looking for as I'm specifying the conf file.
However... (edited to what I think are the appropriate line[s])
$ sudo gedit /etc/postgresql/9.4/main/pg_hba.conf
local all postgres 127.0.0.1 peer
(I added in the local IP after nothing working. Still doesn't work.)
And (/etc/postgresql/9.4/main/)
-rw-r--r-- 1 postgres postgres 315 Feb 14 20:20 environment
-rw-r--r-- 1 postgres postgres 143 Feb 14 20:20 pg_ctl.conf
-rw-r----- 1 postgres postgres 4641 Feb 14 20:55 pg_hba.conf
-rw-r----- 1 postgres postgres 4641 Feb 14 20:20 pg_hba.conf~
-rw-r----- 1 postgres postgres 1636 Feb 14 20:20 pg_ident.conf
-rw-r--r-- 1 postgres postgres 21461 Feb 14 20:20 postgresql.conf
-rw-r--r-- 1 postgres postgres 378 Feb 14 20:20 start.conf
Seems to me the configuration files are owned by postgres. What gives?
Update (9:30p)
Running the following command (as postgres) gives the same result.
$ su - postgres; /usr/lib/postgresql/9.4/bin/postgres -d 3 -D /var/lib/postgresql/9.4/main -c config_file=/etc/postgresql/9.4/main/postgresql.conf
Judging from the error message, ownership for the data directory seems to be misconfigured. If so, fix with (as privileged system user):
chown postgres:postgres /var/lib/postgresql/9.4
chown postgres:postgres /var/lib/postgresql/9.4/main
Use the "recursive" option -R if anything inside those directories is owned by different users.

Resources