Unable to Access PostgreSQL Container Externally After Docker Restart

I am facing an issue with accessing my PostgreSQL container externally after restarting Docker. Prior to the restart, I was able to connect to the container using pgAdmin. The postgres containers is up and running in my VM and I am able to connect to the db internally from the VM.
But I am unable to connect to my DB using pgAdmin. When I checked the /var/lib/docker/containers directory on my VM, I couldn’t find the container file there.

Recreating the containers is not possible for me at the moment, so I’m looking for suggestions on how to regain external access to my PostgreSQL.

I would greatly appreciate any help or troubleshooting steps you can provide.

Best Regards,
Anooj

There are many relevant details we don’t know

  • How did you create the containers? (as in the exact commands or the content of the compose file)
  • Does the postgres container use a restart policy?
  • Do postgres and pgAdmin share a user defined container network?
    • If they do, did you use the container or service name in pgAdmin to access the postgres container, or did you use the ip of the postgres container?

Furthermore, please share the output of docker ps -a

• How did you create the containers? (as in the exact commands or the content of the compose file)

docker run --name postgres -p 5432:5432 -e POSTGRES_DB= -e POSTGRES_USER= -e POSTGRES_PASSWORD= -d postgres

• Does the postgres container use a restart policy?

No, it’s not using any restart policy. Three days before the container automatically went to exit state, then I restarted it manually using command :- sudo docker restart

• Do postgres and pgAdmin share a user defined container network?

No

Furthermore, please share the output of docker ps -a

Thank you for sharing the details. Since you haven’t shared how you start a pgadmin container, I assume you run pgadmin on the docker host (which is a vm). Though, I feel like there is something relevant with pgadmin that you didn’t share so far.

Instead of starting the container each time you start your host, you might want to add a restart policy.

Furthermore, your postgres container does not use any volume, which means whenever you have to re-create the container, all it’s state will be lost.

If pgAdmin is on in the same vm the docker host is, you should be able to reach the postgres container with localhost:5432. If pgAdmin is on a different computer, you should be able to reach the pg container with host-ip-in-lan:5432. Never try to reach a container by it’s container ip: from outside you have the published host port to connect to. Containers in the same container network can leverage dns-based service discovery and call other containers by their container name - this works for ever user defined network, but not for the default bridge network.

Furthermore, you might want to acquaint yourself with docker compose, as it makes life way easier.

Thank you for your reply.

The pgadmin is running on my local machine, I am trying to connect to the postgres DB from the pgadmin on my local machine. To connect to the container through pgadmin , I am using the host port and the vm’s IP address

This part only makes sense with Docker Desktop. I am not sure why I wrote it in your case.

This part matches you current scenario.

What we still don’t know:

  • does vm mean local on the computer, or a remote vm?
  • does the vm use a lan-ip?

For instance Virtualbox uses a natted network interface by default and requires Portforwarding of host ports to vm ports. But it could also be configured in bridge mode and use a lan-ip.

  • does vm mean local on the computer, or a remote vm?
    Its the cloud instance on which the container is running, ie a remote vm

  • does the vm use a lan-ip?
    No

The pgAdmin is on the same computer

I feel like these details should have been part of the first post.
And there are still many of unknown.

Would you be so kind and provide the full picture, so that I don’t have to assume everything.

Try to use in the connection string to postgresql IP address “127.0.0.1” instead of “localhost”. It helped for me when I struggled with the same issue