dokku-postgresql-plugin icon indicating copy to clipboard operation
dokku-postgresql-plugin copied to clipboard

private ip address changes upon restart, apps try to use old ip

Open colnpanic opened this issue 11 years ago • 14 comments

when initially setting up a database for an app the current private ip for the postgres container is written to the ENV file, then if the container is stopped and a new one created it (usually?) has a different private ip.

what i currently do is hand modify each ENV file to point to the new address, then dokku release $APP && dokku deploy $APP. i'd like to automate this so that it automagically finds all apps (potentially looking at /home/dokku/.postgresql/pass_*), tweaks their ENV, and re-deploys if currently running.

another probably better idea is to implement the new-ish (docker >= 0.6.5) '-name postgresql' combined with 'link' on the app run, although i need to look into that a bit more to make sure it does what i think.

the reason i haven't done this yet is that i'm curious how other people handle this, or if it is taken care of somehow that i just don't have set up or am missing.

colnpanic avatar Dec 29 '13 23:12 colnpanic

Name and link is the 'best' solution here. However I don't think dokku supports starting the app containers with the link flags. I'll try to ping the dokku devs and see if they have any suggestions.

jeffutter avatar Dec 30 '13 23:12 jeffutter

Ok, I think i have a solution here. Can you test out this branch:

https://github.com/jeffutter/dokku-postgresql-plugin/tree/docker-name-link

You will need either delete and re-create your DB or change some things in your $DOKKU_ROOT/$APP/ENV

They need to be changed to look like this:

https://github.com/jeffutter/dokku-postgresql-plugin/blob/docker-name-link/commands#L60-L63

Note that the it uses seperate variables for the user, password and db_name. And then sets DATABASE_URL to literally equal '"postgress://${DATABASE_APP}:${DATABASE_PASS}@${DATABASE_PORT_5432_TCP_ADDR}:${DATABASE_PORT_5432_TCP_PORT}/${DATABASE_DB}"'

This causes that ENV variable to be set from the other variables when the container is launched and thus can pull the ip/port from the -link command.

It's kinda hacky but it works.

jeffutter avatar Dec 31 '13 14:12 jeffutter

This is not quite working for me; the DATABASE_URL env var in my app doesn't get the host or port.

(Printed from my app:)
os.environ['DATABASE_URL']='postgres://app-name:VFRiOWVmOVc2UThiRHUxamp5M0IzU25zNGhNS2JVUThzeUt1UDRNZ0k4OD0K@:/app-name_production'

# dokku config app-name
=== app-name Config Vars
DATABASE_DATABASE:      app-name_production
DATABASE_HOST:          ${DATABASE_PORT_5432_TCP_ADDR}
DATABASE_PASSWORD:      VFRiOWVmOVc2UThiRHUxamp5M0IzU25zNGhNS2JVUThzeUt1UDRNZ0k4OD0K
DATABASE_PORT:          ${DATABASE_PORT_5432_TCP_PORT}
DATABASE_URL:           postgres://app-name:VFRiOWVmOVc2UThiRHUxamp5M0IzU25zNGhNS2JVUThzeUt1UDRNZ0k4OD0K@${DATABASE_PORT_5432_TCP_ADDR}:${DATABASE_PORT_5432_TCP_PORT}/app-name_production
DATABASE_USERNAME:      app-name
...

# dokku version
v0.2.1

root@dokku:/var/lib/dokku/plugins/postgresql# git status
# On branch docker-name-link
nothing to commit (working directory clean)

poirier avatar Feb 23 '14 18:02 poirier

This is odd. DATABASE_PORT_5432_TCP_PORT should be set in the ENV of the container by docker. It seems like none of the ENV vars are being set. Are you using a current build of dokku? Perhaps something changed recently.

Can you check your docker ps output that the postgresql container has the name of the app listed under Names. It should look something like dokku-postgresql, appname/database

jeffutter avatar Feb 24 '14 00:02 jeffutter

Dokku v0.2.1, which has been current for a while now.

Docker ps shows

b276537ba9d8        postgresql/app-name:latest   /usr/bin/start_pgsql   16 hours ago        Up 16 hours         0.0.0.0:49167->5432/tcp   happy_pasteur

dokku run app-name printenv shows only these DATABASE_ variables:

DATABASE_DATABASE=app-name_production
DATABASE_HOST=
DATABASE_PASSWORD=VFRiOWVmOVc2UThiRHUxamp5M0IzU25zNGhNS2JVUThzeUt1UDRNZ0k4OD0K
DATABASE_PORT=
DATABASE_URL=postgres://root:[email protected]:49167/db
DATABASE_USERNAME=app-name

poirier avatar Feb 24 '14 12:02 poirier

Can you check on a couple more things for me.

1.) Does $DOKKU_ROOT/.postgresql/pass_$APP_NAME exist? It should be a file containing the password for the database user for the app 2.) is /var/lib/dokku/plugins/postgresql/docker-args marked executable?

jeffutter avatar Feb 24 '14 13:02 jeffutter

I think the reason this isn't working is that Dokku v0.2.1 doesn't implement docker-args.

poirier avatar Feb 24 '14 23:02 poirier

@poirier Good observation. This is the issue. I thought docker-args had been implemented for a while but I guess it wasn't in v0.2.1

Would you care to try with a more recent version?

jeffutter avatar Feb 24 '14 23:02 jeffutter

I haven't had time to get things working completely again with the master branch of dokku, but I have gotten far enough to see a complete DATABASE_URL in my app and verify the app can access the database.

poirier avatar Feb 25 '14 12:02 poirier

OK, thanks for troubleshooting and reminding me docker-args isn't in a dokku release. I'll ping the dokku guys to see if they will release soon. Until then this will have to remain a separate branch.

jeffutter avatar Feb 25 '14 12:02 jeffutter

any plans to merge docker-name-link back into master?

bdentino avatar Mar 13 '15 21:03 bdentino

+1 Im running into this hassle as well. This seems like a common scenario. The setup should be able to survive a server reboot. If there is a recommended solution or workaround could you put it in the wiki?

joshco avatar Mar 22 '15 08:03 joshco

I found a solution. I'm using the virtual network gateway IP address rather than the container's The way things get mapped, the postgres server is accessible on the gateway IP on port 5432 The gateway address is always the same across reboots and restarts. I don't know enough about docker/dokku know if there is a piano waiting to fall on my head somewhere, but this is working for me.

To make the postgres auto start on reboot, I added this to my /etc/rc.local:

# make sure the output is logged somewhere
exec 2> /tmp/rc.local.log      # send stderr from rc.local to a log file
exec 1>&2                      # send stdout to the same log file
set -x                         # tell sh to display commands before execution

echo "Starting postgres container"
/usr/local/bin/dokku postgresql:start

joshco avatar Mar 22 '15 22:03 joshco

Latest thoughts on this? Biggest pain with this plugin :(

codeincontext avatar Aug 16 '15 14:08 codeincontext