For various reasons (want specific version matching production environment, custom config etc) we are running our own Postgres and other servers via Docker in our CircleCI builds.
Despite the fact I did not request them in my circle.yml it seems that CircleCI starts up some services including Postgres and Mongodb by default.
I previously sought advice about this here and was advised to
sudo service postgresql stop before trying to start my docker containers
This works most of the time but some percentage of the time the container fails to start with:
$ docker run -d --name postgres -p 5432:5432 postgres:latest b3718197824603663612db13f9f1216d298bb4355f9ea332305aa3a7556a052a Error response from daemon: Cannot start container b3718197824603663612db13f9f1216d298bb4355f9ea332305aa3a7556a052a: failed to create endpoint postgres on network bridge: listen tcp 0.0.0.0:5432: bind: address already in use docker run -d --name postgres -p 5432:5432 postgres:latest returned exit code 1
I thought… well maybe the port has not been completely freed yet, so I added a step, after the shutdown but before starting my container, that runs a bash script with a wait loop using netcat polling to check that the port is free.
This still fails sometimes with same error, after netcat has reported that the port is unoccupied. So now I suspect that the default postgres service is being restarted automatically and I effectively have a race condition to start my container before the system restarts the clashing service.
Please advise a proper solution for this issue