wbh1 / grafana-sqlite-to-postgres

Grafana SQLite to Postgres Database Migrator
MIT License
100 stars 26 forks source link

Docker command from README requires --network=host #20

Closed timgeb closed 3 years ago

timgeb commented 3 years ago

Thank you for this tool!

I'm facing an issue when trying to reach the postgres database that is running on the host. I'm not sure if there's something missing in the README or if I misunderstood something.

The README provides the command

docker run --rm -ti -v <PATH_TO_DB_FILE>:/grafana.db grafana-sqlite-to-postgres /grafana.db "postgres://<USERNAME>:<PASSWORD>@<HOST>:5432/<DATABASE_NAME>?sslmode=disable"

which in my case would look like this (docker requires sudo on my system, Ubuntu 18.04.5 LTS):

sudo docker run --rm -ti -v /home/tim/grafana/grafana.db:/grafana.db grafana-sqlite-to-postgres /grafana.db "postgres://grafana:grafana@localhost:5432/grafana?sslmode=disable"

However, the command fails with the following output:

INFO[2021-05-17T08:30:00Z] 📁 SQLlite file: /grafana.db                  
INFO[2021-05-17T08:30:00Z] 📁 Dump directory: /tmp                       
INFO[2021-05-17T08:30:00Z] ✅ sqlite3 command exists                     
INFO[2021-05-17T08:30:58Z] ✅ sqlite3 database dumped to /tmp/grafana.sql 
INFO[2021-05-17T08:32:39Z] ✅ CREATE statements removed from dump file   
INFO[2021-05-17T08:39:53Z] ✅ sqlite3 dump sanitized                     
INFO[2021-05-17T08:43:31Z] ✅ migration_log statements removed           
INFO[2021-05-17T08:43:37Z] ✅ char keyword transformed                   
INFO[2021-05-17T08:44:46Z] ✅ hex-encoded data decoded                   
FATAL[2021-05-17T08:44:46Z] ❌ dial tcp 127.0.0.1:5432: connect: connection refused - failed to connect to Postgres database. 

When I add the --network=host option to the docker command, the database connection is successful.

wbh1 commented 3 years ago

Hey Tim,

The issue is with the way that DNS resolution works in containers. localhost in a container means just that container. You would need to specify the name of the postgres container running on your machine for that to work.

You'd have to create a postgres container like this:

docker run --rm --name mypostgrescontainer  -p 5432:5432 postgres:alpine

and then connect to it with:

sudo docker run --rm -ti -v /home/tim/grafana/grafana.db:/grafana.db grafana-sqlite-to-postgres /grafana.db "postgres://grafana:grafana@mypostgrescontainer:5432/grafana?sslmode=disable"

Host mode works because it doesn't do any of the container networking segmentation. From a networking perspective, it's just like running another process on your machine directly so localhost resolves to your machine's IP (rather than a container IP).