Docker Compose - Postgres/Postgis - Shinyapp - postgresql

I have created a composed docker image, which is based on the follow components
version: "3"
services:
db:
image: kartoza/postgis:14-3.2
environment:
- POSTGRES_DB=AAAAAAAA
- POSTGRES_USER=BBBBBBBBBB
- POSTGRES_PASS=CCCCCCCCCC
- POSTGRES_MULTIPLE_EXTENSIONS=postgis,postgis_raster
ports:
- "5432:5432"
restart: on-failure
healthcheck:
test: "exit 0"
shiny:
container_name: shiny
build: ./webapp4
ports:
- "8787:8787"
depends_on:
- "db"
Everything builds up well and goes into operation.
But when the shinyapp tries to connect with the database with the following code
remote_conn <- dbConnect(RPostgres::Postgres(),
dbname = "AAAAAAAA",
host="localhost",
port="5432",
user="BBBBBBBBBB",
password="CCCCCCCCCC")
I have the following output
Warning: Error in : could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
55: <Anonymous>
54: stop
53: connection_create
52: .local
51: dbConnect
49: server
3: runApp
2: print.shiny.appobj
1: <Anonymous>
Error : could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
Can Someone explain me how to solve this?
Thanks

Have you tried to add same network to both containers?
Just define networks and then add network name to both containers and when connecting make sure to use service name as host argument, in your case it would be
host="db", instead of host="localhost"
For an example look at https://github.com/Tornike-Skhulukhia/postgres-to-mongo-importer/blob/main/docker-compose.yml

Related

Docker: Is the server running on host "localhost" (127.0.0.1) [duplicate]

This question already has answers here:
How do I access postgresql within Docker with sqlalchemy?
(3 answers)
Closed 3 months ago.
I'm trying to create a docker container for my fastAPI app. However whenever I try to run following compose file I get this error:
Error
docker logs api-api-1
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
When I first saw the error I thought that I run the database on the localhost of the container but I checked it and database is running on 0.0.0.0:5432.
docker logs api-postgres-1
2022-11-22 05:08:23.971 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
Dockerfile
FROM python:3.10
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
Compose.YAML
version: '3'
services:
api:
image: fastapilearning
depends_on:
- postgres
ports:
- 80:8000
environment:
- ACCESSTOKENEXPIREMINUTE=${ACCESSTOKENEXPIREMINUTE}
- ALGORITHM=${ALGORITHM}
- DATABASEHOSTNAME=${DATABASEHOSTNAME}
- DATABASENAME=${DATABASENAME}
- DATABASEPASSWORD=${DATABASEPASSWORD}
- DATABASEPORT=${DATABASEPORT}
- DATABASEUSERNAME=${DATABASEUSERNAME}
- SECRETKEY=${SECRETKEY}
command: /bin/bash -c "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000"
postgres:
image: postgres
environment:
- POSTGRES_PASSWORD=${DATABASEPASSWORD}
- POSTGRES_DB=${DATABASENAME}
volumes:
- C:\Users\booruledie\Documents\Git\freecodecamp\dockerPostgresData\:/var/lib/postgresql/data
What's the issue here ?
I'm sure of the source code because when I try to run it on my local machine I didn't get any error.
Most likely PostgreSQL server didn't shut down correctly.
Try:
postgres -D /usr/local/var/postgres
You see:
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 449) running in data directory "/usr/local/var/postgres"?
Then Kill the port:
kill -9 PID

When running psql in a Docker container, how to do I reference my Postgres host in another Docker container?

I have the following two containers in my docker-compose.yml file
postgres:
image: postgres:10.5
ports:
- 5105:5432
...
web:
restart: always
build: ./web
ports: # to access the container from outside
- "8000:8000"
env_file: .env
command: /usr/local/bin/gunicorn directory.wsgi:application --reload -w 1 -b :8000
volumes:
- ./web/:/app
depends_on:
- postgres
When I'm logged in to my "web" container (an Ubuntu 18 container), I'd like to be able to login to the PostGres container. How do I do this? I tried this
root#0868cef9c65c:/my-app# PGPORT=5432 PGPASSWORD=password psql -h localhost -Uchicommons directory_data
psql: error: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
but this doesn't seem to be working.
In a Docker container, localhost refers to the container itself.
By default, Docker compose creates a docker bridge network and connects each container to it. From a container on the bridge network, you can reach other containers using their service names. So to reach the database container, you'd use postgres as the host name, like this
PGPORT=5432 PGPASSWORD=password psql -h postgres -Uchicommons directory_data
On the bridge network, you use the native ports. So it's port 5432 for Postgres. If you only need to access a container from other containers on the bridge network, you don't need to map the port to a host port. Mapping to a host port is only needed if you need to access the container from the host computer.

Cannot connect to dockerized Postgres through dockerized pgAdmin

I have a docker-compose file:
version: '3'
services:
db:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_DB: ayyy
POSTGRES_USER: letsgo
POSTGRES_PASSWORD: pwpwpwpw22
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: user#example.com
PGADMIN_DEFAULT_PASSWORD: pwpwpwpw1
ports:
- "5433:80"
(I changed the environment variables to not spoof my credentials)
However I am unable to connect to my Postgres server through pgAdmin. pgAdmin is using reverse proxy from port 5433 to my subdomain pgadmin.domain.com. I am also reverse proxying HTTP from pgAdmin's container into HTTPS. (I do not know if that could be an issue)
This is the error I get:
Unable to connect to server:
connection to server at "db" (192.168.32.3), port 5432 failed: timeout expired
If I use localhost as a hostname, I get this:
Unable to connect to server:
connection to server at "localhost" (127.0.0.1), port 5432 failed:
Connection refused Is the server running on that host and accepting
TCP/IP connections? connection to server at "localhost" (::1), port
5432 failed: Address not available Is the server running on that host
and accepting TCP/IP connections?
UPDATE: I am not even able to ping the containers between each other. The service name is correctly resolved to IP, but I get no response.
I was even unable to open ports on my server, so I reinstalled it. Everything works as it should since then.

Adminer & Migrate Can't Connect to Postgresql Docker Container

I have three docker containers (postgresql, adminer, and go/migrate) and I've exposed both adminer and postgres ports to the host. I can access adminer in my browser, and postico can also connect to the DB. When I try to connect to the db from within adminer, it throws this error:
SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP
connections on port 5432? could not connect to server: Address not available
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
The migrate container throws this error too:
error: dial tcp: 127.0.0.1:5432: connect: connection refused
So, clearly there is an issue with how the containers are able to communicate with each other. Do I need to create a docker network?
version: '3.1'
services:
db:
image: postgres
environment:
POSTGRES_DB: mydbname
POSTGRES_USER: mydbuser
POSTGRES_PASSWORD: mydbpwd
ports:
- "5432:5432"
migrate:
image: migrate/migrate
volumes:
- .:/migrations
command: ["-database", "postgres://mydbuser:mydbpwd#localhost:5432/mydbname?sslmode=disable", "-path", "/migrations", "up"]
links:
- db
adminer:
image: adminer
restart: always
ports:
- "8081:8080"
depends_on:
- db
You do not need to create linking, docker-compose create default network. also service to service communication should use service name, localhost mean this container(migrate) not DB container.
change localhost to db
migrate:
image: migrate/migrate
volumes:
- .:/migrations
command: ["-database", "postgres://mydbuser:mybpwd#db:5432/mydbname?sslmode=disable", "-path", "/migrations", "up"]
as you DB service name is db so you connect with db container using it name.

Cannot connect to postgreSQL docker container via postico

I'm trying to use Postico to connect to a docker postgreSQL container on my local machine.
I've tried connecting to 0.0.0.0, localhost, and 127.0.0.1. Each give me the following error:
could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
0.0.0.0 gives me a similar, but smaller error:
could not connect to server: Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
Here is my docker-compose file:
version: '3'
services:
prisma:
image: prismagraphql/prisma:1.23
restart: always
ports:
- "4466:4466"
environment:
PRISMA_CONFIG: |
port: 4466
databases:
default:
connector: postgres
host: postgres
port: 5432
user: prisma
password: prisma
migrations: true
postgres:
image: postgres:10.5
restart: always
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: prisma
volumes:
- postgres:/var/lib/postgresql/data
volumes:
postgres:
Solution found thanks to Egor! I forgot to specify ports: - "5432:5432" inside my docker-compose file. Rookie mistake ;)
I also had issues using Postico to connect to my Postgres DB in a docker container.
Ultimately, my issue was that I had a local Postgres DB running.
As soon as I disconnected my local Postgres DB, I was able to use Postico to connect to my docker DB. With the host set to localhost, I used the POSTGRES_USER, POSTGRES_PASSWORD, and host port as defined in my docker-compose.yml file.
If postgres version doesn't matter, try to change Postgres image to this one, it works for me
And also make sure that you add ports in docker-compose.yml
postgres:
image: postgres
restart: always
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: prisma
ports:
- "5432: 5432"
volumes:
- postgres:/var/lib/postgresql/data
P.s. just updated answer for readability