Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to Docker PostgreSQL container in AWS EC2 instance

Docker newbie here.

I'm trying to create a docker container for PostgreSQL in AWS EC2 instance.

First, to achieve persistence I use data volume container. Here is the command I used to create a data volume container for my PostgreSQL database:

docker create -v /var/lib/postgresql/data --name postgres9.3.6-data busybox

Then I run the following command to create the PostgreSQL container:

docker run --name postgres9.3.6 \
-p 5432:5432 \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_USER=root \
-e POSTGRES_DB=db_name \
-d --volumes-from postgres9.3.6-data postgres:9.3.6

I run this in my local environment and then tried to connect to it through a PostgreSQL desktop client and it worked. I was able to connect to the db_name that is stored in local PostgreSQL docker container.

After this, I sshed into one of my EC2 instances in AWS. And followed the same flow I described above. Then returned back to the PostgreSQL desktop client and tried to connect to it. But I got the following error message:

Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

What am I missing here?

Edit

I've already checked and set the listen_addresses on postgresql.conf file to

listen_addresses='*'

and appended the following line to pg_hba.conf file:

host all all 0.0.0.0/0 md5
like image 982
hnroot Avatar asked Apr 20 '17 08:04

hnroot


People also ask

How do I access Docker in PostgreSQL?

Connecting to the PSQL server via CLI : Run the below command to enter into the container (with the ID from step-1). docker exec -it <PSQL-Container-ID> bash. Authenticate to start using as postgres user. psql -h localhost -p 5432 -U postgres -W.

How do I connect an EC2 instance to a Docker?

To install Docker on an Amazon EC2 instanceConnect to your instance using SSH. For more information, see Connect to your Linux instance using SSH in the Amazon EC2 User Guide for Linux Instances. Update the installed packages and package cache on your instance. Install the most recent Docker Engine package.

Can EC2 host Docker containers?

Amazon ECS is a highly scalable, high performance container management service that supports Docker containers and allows you to easily run applications on a managed cluster of Amazon EC2 instances.


1 Answers

The postgresql.conf and pg_hba.conf look good. The docker run correctly publish postgresql port. Check your EC2 inbound rule, probably port 5432 is not allowed to be accessed.

like image 140
CloudStax Avatar answered Sep 18 '22 04:09

CloudStax