Self-redirection to localhost

I’m facing strange issue
I created two containers in my Docker environment - Percona Server for MongoDB and Percona Backup for MongoDB. The second one includes pbm-agent.

I’m unable to connect from Percona Backup to Percona Server in order to take a backup. Percona Backup always tries to reach localhost. The container’s IP that contains Percona Server is
Please see error below

Error: get status of cluster: connect to `rs0` [rs0/localhost:27017]: ping: server selection error: server selection timeout, current topology: { Type: Single, Servers: [{ Addr: localhost:27017, Type: Unknown, Last error: dial tcp connect: connection refused }, ] }

Connection string:


I decided to try to cheat a container with replacing the localhost address and swapping it to in /etc/hosts/

It worked out

Is there any possibility to avoid redirecting my IP to localhost?

Thanks in advance

Did you follow the documentation?
There is a parameter to use when launching the PBM container to specify the URI of your mongo server.


Thanks for your response. Please be informed I’ve added the connection string in my Dockerfile so I thought it’s not necessary at this point. Is it?

Both containers are running in the same Docker Network. They’re being run with the following commands:
docker run -p 27017:27017 --network perconanet -d percona-server:1.2
docker run --network perconanet -d percona-backup:0.12

Please see my PBM Dockerfile below:


EXPOSE 28017

ENV PBM_MONGODB_URI="mongodb://pbmuser:secretpwd@"

USER root 

COPY pbm-config.yaml .

RUN rm /etc/sysconfig/pbm-agent \
    && echo "PBM_MONGODB_URI='$PBM_MONGODB_URI'" > /etc/sysconfig/pbm-agent \
    && chmod 640 /etc/sysconfig/pbm-agent \
    && pbm config --file pbm-config.yaml

Then for Percona Server


EXPOSE 27017

#VOLUME /data

USER root


RUN rm -f etc/mongod.conf

COPY mongod.conf /etc

RUN chmod +x

USER mongodb

CMD ["mongod", "--wiredTigerCacheSizeGB", "0.25", "--replSet", "rs0", "--bind_ip", ""]

With the configuration above I’m getting such error messages:

2023-12-13T06:31:25.727+0000 I  NETWORK  [js] Starting new replica set monitor for rs0/
2023-12-13T06:31:25.728+0000 I  CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to
2023-12-13T06:31:25.729+0000 I  NETWORK  [ReplicaSetMonitor-TaskExecutor] Confirmed replica set for rs0 is rs0/localhost:27017
2023-12-13T06:31:25.729+0000 I  CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to localhost:27017
2023-12-13T06:31:26.228+0000 W  NETWORK  [ReplicaSetMonitor-TaskExecutor] Unable to reach primary for set rs0
2023-12-13T06:31:26.228+0000 I  NETWORK  [ReplicaSetMonitor-TaskExecutor] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 1 checks in a row.

But if I edit /etc/hosts file in the way shown below it seems to be working properly

[root@59a82c7385e2 etc]# cat hosts       localhost

With edited hosts file my logs look like this:

2023-12-13T06:33:01.840+0000 I  NETWORK  [js] Starting new replica set monitor for rs0/
2023-12-13T06:33:01.840+0000 I  CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to
2023-12-13T06:33:01.841+0000 I  NETWORK  [ReplicaSetMonitor-TaskExecutor] Confirmed replica set for rs0 is rs0/localhost:27017
2023-12-13T06:33:01.841+0000 I  CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to localhost:27017
2023-12-13T06:33:01.842+0000 I  NETWORK  [ReplicaSetMonitor-TaskExecutor] Confirmed replica set for rs0 is rs0/localhost:27017
+ exit_status=0
++ grep -E '^([0-9]+)$' /tmp/tmp.oNyx38678P
+ rs_size=1
+ [[ 0 == 0 ]]
+ [[ 1 -ge 1 ]]
+ break
+ rm /tmp/tmp.oNyx38678P
+ exec pbm-agent
2023-12-13T06:33:01.000+0000 I pbm-agent:
Version:   2.3.1
Platform:  linux/amd64
GitCommit: 8c4265cfb2d9a7581b782a829246d8fcb6c7d655
GitBranch: release-2.3.1
BuildTime: 2023-11-29_13:31_UTC
GoVersion: go1.19
2023-12-13T06:33:01.000+0000 I node: rs0/localhost:27017
2023-12-13T06:33:01.000+0000 I starting PITR routine
2023-12-13T06:33:01.000+0000 I listening for the commands
2023-12-13T06:33:06.000+0000 W [agentCheckup] get current storage status: query mongo: mongo: no documents in result

I tried to:

  • use localhost directly in MongoDB connection string
  • build new Percona Server image both with and without “–bind_ip”, “”
  • set connection string with Replica Set parameter only /?replicaSet=rs0


PBM connects to the node, gets the list of members in the replset, and connects to the member using provided hostname from the list.

  • set and use a unique hostname in the network for your MongoDB and give it to PBM
  • do not set directConnection and replicaSet params, PBM ignores them (user:pwd@host:port is enough)

Also, you can avoid building custom images for PSMDB and PBM.

Try this way:

#!/usr/bin/env bash

# get UID used by mongod, pbm-agent and pbm-cli to share filesystem permissions
MONGODB_USER=$(docker image inspect percona/percona-server-mongodb:7.0-multi --format '{{.ContainerConfig.User}}')

# prepare volumes
docker volume create psmdb-rs000-config
docker volume create psmdb-rs000-scripts
docker run --rm \
	-v "$(pwd):/host-files" \
	-v "psmdb-rs000-config:/cfg" \
	-v "psmdb-rs000-scripts:/scripts" \
	busybox sh -c 'cp /host-files/mongod.conf /cfg/ && cp /host-files/*.js /scripts/'

# create network for cluster
docker network create psmdb-cluster-net
# create mongodb "--dbpath" volume
docker volume create psmdb-rs000-data
# create mongodb log volume
docker volume create psmdb-rs000-log

docker run -d \
	--restart "unless-stopped" \
	--name "psmdb-rs000" \
	--hostname "psmdb-rs000-host" \
	--net "psmdb-cluster-net" \
	-p "27017:27017" \
	-v "psmdb-rs000-config:/cfg" \
	-v "psmdb-rs000-scripts:/scripts" \
	-v "psmdb-rs000-data:/data/db" \
	-v "psmdb-rs000-log:/var/log/mongo" \
	percona/percona-server-mongodb:7.0-multi \
		-f "/cfg/mongod.conf" \
		--dbpath "/data/db" \
		--logpath "/var/log/mongo/mongod.log" \
		--wiredTigerCacheSizeGB="0.25" \
		--replSet="rs0" \
		--bind_ip_all \
		--port "27017"

# wait for mongod ready
sleep 2

docker exec psmdb-rs000 mongosh --quiet --eval 'rs.initiate()'
docker exec psmdb-rs000 mongosh --quiet '/scripts/add-users.js'

# create storage for backups
docker volume create pbm-rs000-storage
docker run --rm -it -v "pbm-rs000-storage:/s" busybox chown -R "$MONGODB_USER" /s

docker run -d \
	--restart "unless-stopped" \
	--name "pbm-rs000" \
	--net "psmdb-cluster-net" \
	-v "pbm-rs000-storage:/mnt/storage" \
	-v "psmdb-rs000-data:/data/db" \
	-e PBM_MONGODB_URI='pbmuser:secretpwd@psmdb-rs000-host:27017' \

# wait for pbm ready
sleep 2

# apply config (from host fs)
docker exec -i pbm-rs000 pbm config --file='-' <./pbm-config.yaml

sleep 2

# check status
docker exec pbm-rs000 pbm status


    user: 'adm',
    pwd: 'pass',
    roles: ['root'],

    'role': 'pbmAgent',
    'privileges': [{
        'resource': { 'anyResource': true },
        'actions': ['anyAction'],
    'roles': [

    user: 'pbmuser',
    pwd: 'secretpwd',
    roles : ['pbmAgent'],


    type: filesystem
        path: /mnt/storage/backups
1 Like

The incorrect value in replica set declaration was the issue indeed.
It’s working now.
Thanks for your assistance, a thread can be closed