Kloadut / dokku-pg-plugin

PostgreSQL plugin for Dokku
201 stars 75 forks source link

Docker changes container port on restart, app unable to connect to Postgres container #50

Closed dankimio closed 10 years ago

dankimio commented 10 years ago

I've created a Postgres database and linked it with a Rails app. Everything worked fine until I restarted the server. After restart Docker Postgres container received a new port and Rails application wasn't able to connect to it. If I run dokku postgresql:info myapp it shows the old port, but it has changed (relinking also gives the old port). It only works if I hardcode the port in config.yml file. It also works when I manually change DATABASE_URL variable through dokku config:set command.

timbueno commented 10 years ago

Same issue here. Fired up a dokku instance last night with Postgres for a Flask app I'm developing. Port changes on restart.

scourou commented 10 years ago

I think a potential fix is to ensure that the plugin interrogates docker for the port rather than stashing it for re-use. I'll try and get something together for a PR as this is messing me up too.

will-hart commented 10 years ago

+1

Same issue with the mariadb plugin

scourou commented 10 years ago

Right. I've re-factored the plugin to dynamically get the port/id of the application name you are after. My fork is here: https://github.com/scourou/dokku-pg-plugin

I'd appreciated if people could test it out, as I only started using dokku this morning, and I can't be certain I've not screwed something up. If all is well, I'll format into a proper PR to get it merged back in. Short on time at the moment.

will-hart commented 10 years ago

I removed the current plugin:

$ cd /var/lib/dokku/plugins
$ rm -rf postgresql
$ docker rmi <postgresql image ID>

Then installed your fork. I get the following error when I try to link a DB:

$ dokku postgresql:create mydb
$ dokku postgresql:link myapp.com mydb 
>  cat: /home/dokku/.postgresql/port_mydb: No such file or directory

EDIT: Dokku 0.2.3 on Ubuntu 14.04

will-hart commented 10 years ago

Should line 184 be

PORT=$(get_postgresql_port)

Instead of

PORT=$(cat "$DOKKU_ROOT/.postgresql/port_$3")
scourou commented 10 years ago

That'll teach me to not rush fixes. :) Will fix and update asap.

scourou commented 10 years ago

Updated my fork, and have issued PR.

Kloadut commented 10 years ago

Thanks !

dankimio commented 10 years ago

Thank you!

mallorypaine commented 9 years ago

An alternative fix for this is to modify the docker run command to specify both container port and host port. I changed this line to the following. This way, my container port never changes and I don't have to do anything after rebooting. ID=$(docker run -v $VOLUME -p 5432:5432 -d $DB_IMAGE /usr/bin/start_pgsql.sh $DB_PASSWORD)