I'm trying to run two databases (msql and postgres) inside a container with docker installed. Unfortunately, when using pgloader, there is an error connecting to the databases. Here are the steps I take:
docker pull docker:dind
docker run --privileged --name dind-container --hostname lch -d docker:dind
apk add --no-cache pgloader
docker pull -q mysql:8
and docker pull -q postgres:16
docker run -p 3306:3306 -d --network my-network --name mysql-db --hostname mysql -e MYSQL_ROOT_PASSWORD=mysql -e MYSQL_ROOT_HOST=root -e MYSQL_DATABASE=my_db -e MYSQL_USER=my_user -e MYSQL_PASSWORD=my_password mysql:8 --default-authentication-plugin=mysql_native_password
docker run -d --network my-network --name postgres-db --hostname postgres -e POSTGRES_DB=my_db -e POSTGRES_USER=my_user -e POSTGRES_PASSWORD=my_password postgres:16
pgloader --with "batch concurrency = 1" mysql://my_user:my_password@lch/my_db postgresql://my_user:my_password@$lch/my_db
My question is here: what to enter for 'lch'. I have tried various things, including 'localhost', but there is always an error. I tried logging ip like this and pasting as host: ip route|awk '{print $3; exit}'
.
Error:
I will add that I managed to do this locally on my machine, then I simply used 'localhost'. However, now I have to use dind image and do it inside container and this is the problem. I also know that databases are not populated with tables, but that is not a problem here.
If you run the database with a correct docker run -p
option to publish its ports
docker run -p 5432:5432 ... postgres:16
# ^^^^^^^^^^^^
then, from a non-container process inside the DinD container, you should be able to access localhost:5432
to reach the database. As with other -p
options, your non-container tool needs to connect to the first -p
port, and the second -p
port must match the standard port the process inside the container is using.
If you want to access this port further from the actual host system, you also need to start the DinD container with a similar -p
option.