I'm using the following docker-compose.yml file:
version: "3.5"
services:
db:
image: postgres:latest
volumes:
- ./tmp/postgresql/:/var/run/postgresql/:rw
ports:
- 5432:5432
environment:
POSTGRES_DB: dev
POSTGRES_USER: username
POSTGRES_PASSWORD: pw
I can connect to the postgres instance from my host as I have 5432 port forwarded to the host. I wanted to try connecting via the socket, but I am hitting issues. I'm not sure if this is possible?
From my host I can use the following:
psql --host=/Users/jalbert/Projects/postgres-sockets/tmp/postgresql -U username -d dev`
psql: could not connect to server: Connection refused
Is the server running locally and accepting
connections on Unix domain socket "/Users/jalbert/Projects/postgres-sockets/tmp/postgresql/.s.PGSQL.5432"?
Although, I do see the socket file present there. If I go within the docker container I can connect via the socket no problem using a similar command.
Am I missing something to allow the host to use the socket connection? Unfortunately, I am not too familiar with the socket technologies.
Related
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.
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.
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.
The main problem is that i cannot run postgresql even on vm with the error:
root#a2c8a58d4e0e:/# psql -h localhost -U psqluser -W
Password for user psqluser:
psql: 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?
For that purpose i run this commands inside VM:
pg_createcluster 9.6 main --start
/etc/init.d/postgresql start
And then it works properly on VM. But that's manually.
I configured everything by official docker repo docs.
This is my docker compose file:
version: "3.3"
services:
postgresql:
build:
context: .
dockerfile: postgresql
container_name: Postgres
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_DB: 'psqldb'
POSTGRES_USER: 'psqluser'
POSTGRES_PASSWORD: 'temp123'
volumes:
- /home/VOLUMES/DB/Postgresql:/var/lib/postgresql
I did inheritance from original repo as i want to run postgresql service automatically. Otherwise it's not running.
postgresql file:
FROM postgres:9.6.11
RUN pg_createcluster 9.6 main --start
RUN /etc/init.d/postgresql start
It does not run Postgres as well. Only manually inside VM.
What's wrong?
Within docker compose, ports aren't exposed on the host by default. https://docs.docker.com/compose/compose-file/
ports is virtual within the docker compose network. If you want to expose them to the host machine, you can use the expose option instead of ports.
Alternatively, you can also run docker-compose run with the --service-ports flag which will automatically expose the ports to the host when running.
docker-compose run --service-ports postgresql (see doc)
I am trying to deploy a second database container on a remote server using Docker compose. This postgresql server runs on port 5433 as opposed to 5432 as used by the first postgresql container.
When I set up the application I get this error output:
web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused
web_1 | Is the server running on host "db" (172.17.0.2) and accepting
web_1 | TCP/IP connections on port 5433?
and my docker compose file is:
db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: route_admin
POSTGRES_USER: route_admin
expose:
- "5433"
ports:
- "5433"
volumes:
- ./backups:/home/backups
web:
build: .
command: bash -c "sleep 5 && python -u application/manage.py runserver 0.0.0.0:8081"
volumes:
- .:/code
ports:
- "81:8081"
links:
- db
environment:
- PYTHONUNBUFFERED=0
I feel the issue must be the postgresql.conf file on the server instance having set the port to 5432 causing the error when my app tries to connect to it. Is there a simple way of changing the port using a command in the compose file as opposed to messing around with volumes to replace the file?
I am using the official postgresql container for this job.
Some people may wish to actually change the port Postgres is running on, rather than remapping the exposed port to the host using the port directive.
To do so, use command: -p 5433
In the example used for the question:
db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: route_admin
POSTGRES_USER: route_admin
expose:
- "5433" # Publishes 5433 to other containers but NOT to host machine
ports:
- "5433:5433"
volumes:
- ./backups:/home/backups
command: -p 5433
Note that only the host will respect the port directive. Other containers will not.
Assuming postgres is running on port 5432 in the container and you want to expose it on the host on 5433, this ports strophe:
ports:
- "5433:5432"
will expose the server on port 5433 on the host. You can get rid of your existing expose strophe in this scenario.
If you only want to expose the service to other services declared in the compose file (and NOT localhost), just use the expose strophe and point it to the already internally exposed port 5432.