dockerpgloader

What is the correct hostname for pgloader when using this tool in docker-in-docker image?


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:

  1. i am pulling dind image: docker pull docker:dind
  2. running container from dind image: docker run --privileged --name dind-container --hostname lch -d docker:dind
  1. installing pgloader to container: apk add --no-cache pgloader
  2. get images of msql db and postgres: docker pull -q mysql:8 and docker pull -q postgres:16
  3. create network: docker network create my-network || true
  4. run mysql container:
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
  1. run postgres database:
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

inside docker

  1. waiting for setting up databases
  2. run pgloader command:
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:

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.


Solution

  • 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.