I'm running Docker Compose on Ubuntu 24, and when accessing PostgreSQL from within the backend container, I get the following error:
root@60128607c5ab:/backend# psql -p 5433 -U postgres
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5433" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
When I test the connection directly from Ubuntu on port 5433 it works, but it doesn't work from inside my container even though I can ping from one container to another.
docker compose:
version: '3.4'
x-backend:
&backend
build:
context: .
dockerfile: Dockerfile.dev
environment:
RAILS_ENV: development
DB_USERNAME: postgres
DB_PASSWORD: secret
DB_HOST: db
DB_PORT: 5433
DB_NAME: es_db
SECRET_KEY_BASE: STUB
REDIS_URL: redis://redis:6379/1
restart: on-failure:3
stdin_open: true
tty: true
container_name: backend_container
volumes:
- .:/backend:rw
- bundle_cache:/bundle
networks:
- network1
services:
backend:
<<: *backend
ports:
- 3000:3000/tcp
depends_on:
- db
db:
image: postgres:11.2
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: secret
POSTGRES_HOST_AUTH_METHOD: trust
restart: always
ports:
- 5433:5432
volumes:
- postgres:/var/lib/postgresql/data
networks:
- network1
# sidekiq:
# <<: *backend
# command: bundle exec sidekiq
# depends_on:
# - redis
# - backend
redis:
image: 'redis:5.0-alpine'
command: redis-server
volumes:
- redis:/data
volumes:
bundle_cache:
postgres:
redis:
networks:
network1:
internal: true
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: streaming_api_development
username: postgres
password: secret
port: 5433
Repository with docker: https://github.com/andbri321/streaming/tree/docker_backend