Connect to PostgreSQL Database in Docker Container from DBeaver - ruby-on-rails

I can't get the connection between my PostgreSQL database from my Rails app which is running in a Docker container working.
The application just works fine, I just can't connect to the database.
docker-compose.yml:
services:
app:
build:
context: .
dockerfile: app.Dockerfile
container_name: application_instance
command: bash -c "bundle exec puma -C config/puma.rb"
volumes:
- .:/app
- node-modules:/app/node_modules
- public:/app/public
depends_on:
- database
- redis
env_file:
- .env
database:
image: postgres
container_name: database_instance
restart: always
volumes:
- db_data:/var/lib/postgresql/data
ports:
- "5432:5432"
env_file:
- .env
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_PRODUCTION_DB}
nginx:
build:
context: .
dockerfile: nginx.Dockerfile
depends_on:
- app
volumes:
- public:/app/public
ports:
- "80:80"
redis:
image: redis
container_name: redis_instance
ports:
- "6379:6379"
sidekiq:
container_name: sidekiq_instance
build:
context: .
dockerfile: app.Dockerfile
depends_on:
- redis
- database
command: bundle exec sidekiq
volumes:
- .:/app
env_file:
- .env
volumes:
db_data:
node-modules:
public:
If I try to connect via DBeaver I get the following message:
Any idea what's going wrong here? The port should be exposed on my local machine. I also tried with the IP of the container, but then I get a timeout exception.

This is because you most likely have postgres running locally on your machine (port 5432) and also on a docker (port 5432). Dbeaver wants to connect to database on your local machine, than on docker.
Any solution I figure out is to temporary stop/turn of your local postgres service (on Windows: Task manager -> Services -> (postgres service) -> stop).
I was also struggling with issue.

Related

How to reach localhost services from docker container with IPv4?

I have this docker-compose file where I have a couple of services:
version: "3.8"
services:
app:
container_name: mobile_app_api
env_file:
- .env.development
command: npm run watch:dev
build:
context: .
target: development
dockerfile: ./Dockerfile
restart: always
volumes:
- /usr/src/app/node_modules
- ./:/usr/src/app
depends_on:
- postgres
- redis
extra_hosts:
- host-machine:host-gateway
network_mode: host
redis:
image: "redis:alpine"
container_name: mobile_app_cache
postgres:
image: postgres
container_name: mobile_app_database
restart: "unless-stopped"
environment:
TZ: 'GMT'
PGTZ: 'GMT'
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: mobile_app_database
volumes:
- /:/data/postgres
network_mode: host
Api is supposed to work on localhost:3333, but when I start this with docker-compose up --build in terminal I get this:
mobile_app_api | server running on http://[::1]:3333
And here is 2 things.
Why does it work on IPv6?
How can I change host and make it work on localhost?
PS. My computer is MacOS, but in settings I have turned off IPv6.
I was trying to use host.docker.internal (and gateway.docker.internal) in my compose file, but it didn't work:
extra_hosts:
- host.docker.internal:host-gateway

socket hang up on request to docker container

I am beginner in Docker and can not get response from my project that running in docker. I have a Go project with 4 services. When It Run as local machine in my pc, everything is good and not have problem. But when it run in docker and send request by postman, could not get response and socket hang up was present.
I have 4 service for this:
1- Rest API service that dockerfile is :
FROM golang:latest as GolangBase
...
...
EXPOSE 8082
CMD ["/go/bin/ecg", "server"]
2- Page service that dockerfile is :
FROM golang:latest as GolangBase
...
...
EXPOSE 8080
CMD ["/go/bin/ecg", "page"]
2- Redis
3- Postgres
docker-compose in root:
version: "2.3"
services:
server:
build:
context: .
dockerfile: docker/app/Dockerfile
container_name: ecg-go
ports:
- "127.0.0.1:8082:8082"
depends_on:
- postgres
- redis
networks:
- ecg-service_default
restart: always
page:
build:
context: .
dockerfile: docker/page/Dockerfile
container_name: ecg-page
ports:
- "127.0.0.1:8080:8080"
depends_on:
- postgres
networks:
- ecg-service_default
restart: always
redis:
image: redis:6
container_name: ecg-redis
volumes:
- redis_data:/data
networks:
- ecg-service_default
postgres:
image: postgres:alpine
container_name: ecg-postgres
environment:
POSTGRES_PASSWORD: docker
POSTGRES_DB: ecg
POSTGRES_USER: ecg
volumes:
- pg_data:/var/lib/postgresql/data
networks:
- ecg-service_default
volumes:
pg_data:
redis_data:
networks:
ecg-service_default:
I build images and run containers by docker-compose up -d command and all services is created and running.
But when sending Request to http://localhost:8082/.. it return Could not get response, socket hang up.
What's the problem ??

NextJS gives an error during the build step in Docker when trying to connect to strapi api

I am trying to run nextjs with strapi by using docker. NextJS app is giving an error during the npm packages build step like this:
request to http://localhost:1337/api/categories?fields%5B0%5D=slug failed, reason: connect ECONNREFUSED
I tried to change localhost to strapi but it didn't help too.
Here is my docker-compose.yaml file:
version: '3'
services:
strapi:
build:
context: ./api
dockerfile: Dockerfile
environment:
DATABASE_CLIENT: postgres
DATABASE_NAME: strapi
DATABASE_HOST: postgres
DATABASE_PORT: 5432
DATABASE_USERNAME: strapi
DATABASE_PASSWORD: strapi
volumes:
- ./api:/srv/api
ports:
- '1337:1337'
depends_on:
- postgres
networks:
- strapi_app
postgres:
image: postgres
environment:
POSTGRES_DB: strapi
POSTGRES_USER: strapi
POSTGRES_PASSWORD: strapi
ports:
- "5432:5432"
volumes:
- ./data:/var/lib/postgresql/data
networks:
- strapi_app
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- "3000:3000"
networks:
- strapi_app
depends_on:
- strapi
networks:
strapi_app:
driver: bridge
I created a network for the services in docker-compose.yaml file. They should run in the same network. I couldn't realize what I am missing.
Also I couldn't find any effective solution from stackoverflow or google search.
Any help will be appreciated!
Thanks

How to access docker container localhost

I executed docker-compose up and my services worked as well but unfortunately when i hit the url " http://localhost:3000 " it gives me an error I'll paste the docker-compose.yml here.
version: "3.7"
services:
db:
image: postgres
container_name: psql
environment:
POSTGRES_DB: ${DB_DATABASE}
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres:/var/lib/postgresql/data
- ./postgresql/conf:/etc/postgresql/
restart: always
ports:
- '5432:5432'
api:
build: .
container_name: api
restart: always
command: yarn start
depends_on:
- db
ports:
- "3000:3000"
volumes:
- .:/usr/app
volumes:
postgres:
When I get into the container and execute curl localhost:3000 it works fine.
My problem was solved after I changed the host in my node api to '0.0.0.0' it was 'localhost' so I think the application was running only inside the container and after that change it was visible from all the created network.

How to change permissions for Gemfile created with Docker for Windows?

I'm working with a dockerized rails application, however whenever I make a change to the Gemfile the file permissions change to an unkown user, and I'm unable to do anything to the file whether I'm inside the container or not.
How can I make it so that I'm able to manipulate the file again?
Here's my .docker-copmose.yml:
version: '3'
networks:
backend:
volumes:
postgres:
services:
postgres:
image: postgres
ports:
- ${APP:-5432}:5432
volumes:
- ./db/dumps:/db/dumps # Mount the directory DB dumps folder
- postgres:/var/lib/postgresql/data
networks:
- backend
environment:
POSTGRES_PASSWORD: 3x4mpl3
web:
build:
context: .
dockerfile: Dockerfile
command: rails s -b 0.0.0.0 -p 3000
entrypoint: /app/bin/entrypoint.sh
volumes:
- .:/app
ports:
- ${APP_WEB_PORT:-3000}:3000
stdin_open: true
tty: true
depends_on:
- postgres
networks:
- backend
environment:
DATABASE_URL: postgres://postgres:3x4mpl3#postgres:5432/app_development
RAILS_ENV: development
Can you try to change the file ownership from outside of the docker and after that you can manipulate the file.

Resources