Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke dbmate from Docker and trigger migration in postgres #128

Closed
jayarjo opened this issue Apr 17, 2020 · 4 comments
Closed

Invoke dbmate from Docker and trigger migration in postgres #128

jayarjo opened this issue Apr 17, 2020 · 4 comments

Comments

@jayarjo
Copy link

jayarjo commented Apr 17, 2020

I was trying the following:

$ docker run --rm -it --env DATABASE_URL="..." --mount type=bind,source=...,target=/app amacneil/dbmate --no-dump-schema --migrations-dir /app up

However I repeatedly get:

Error: dial tcp 127.0.0.1:57806: connect: connection refused

DATABASE_URL points to the postgres server running on another Docker container (not sure how that should matter?)

Do I need to to anything in addition to the above command to be able to run migrations on another Docker container that is running postgres?

@jayarjo
Copy link
Author

jayarjo commented Apr 18, 2020

In fact forget about another Docker container, it fails to connect to the postgres on the host as well, with the same error.

@jayarjo jayarjo changed the title Invoke dbmate from Docker and trigger migration in postgres running in another Docker container Invoke dbmate from Docker and trigger migration in postgres Apr 18, 2020
@amacneil
Copy link
Owner

amacneil commented Apr 18, 2020

By default docker containers have their own network and IP address. There are several ways you can work around this.

If you're running postgres on your local computer you should be able to connect to it by running the dbmate container with host networking (I'll add this to the readme):

docker run --rm --network=host -it amacneil/dbmate

If you're running postgres in another container, again there are several ways to solve this. One option is to expose the port from the postgres container to your host (using -p flag), then use host networking (as above) when running dbmate to connect via localhost:

docker run --rm -d -p 5432:5432 -e POSTGRES_PASSWORD=postgres postgres
docker run --rm -it --network=host -e DATABASE_URL="postgres://postgres:postgres@localhost/foo?sslmode=disable" amacneil/dbmate create

Another is to put both the postgres container and dbmate on a private container network (and not expose them to the host):

docker network create mynet
docker run --rm -d -e POSTGRES_PASSWORD=postgres --name=pg --network=mynet postgres
docker run --rm --network=mynet -e DATABASE_URL="postgres://postgres:postgres@pg/foo?sslmode=disable" amacneil/dbmate create

Finally, you can also do this using docker compose by adding depends_on, which is what we do for dbmate development & testing.

@jayarjo
Copy link
Author

jayarjo commented Apr 19, 2020

@amacneil thanks for taking your time to clarify this. Would have been great to have this somewhere in readme.

@amacneil
Copy link
Owner

Thanks for the feedback - I updated the readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants