Why are rails connections not working in docker-compose? - ruby-on-rails

I setup a very basic rails app that connects to a postgres instance and a rails instance. For some reason, when I execute rails db:setup, it won't connect to the database. My setup is below. Don't think it can get much simpler.
Important to note, postgres was up and ready to accept connections.
docker-compose.yml
version: '3.4'
services:
postgres:
image: postgres
environment:
- POSTGRES_PASSWORD=password
volumes:
- ./tmp/postgres/data:/var/lib/postgres/data
redis:
image: redis
ports:
- '6379:6379'
web:
build:
context: .
target: development
command: tail -f /dev/null # I exec'ed in to run command
depends_on:
- postgres
- redis
env_file:
- ./docker-dev.env
ports:
- '3000:3000'
database.yml
development:
adapter: postgresql
encoding: unicode
pool: 5
username: postgres
password: password
host: postgres
The error ouput
D, [2020-04-03T03:20:34.562188 #32] DEBUG -- : using default configuration
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?
Couldn't create 'hinge_test_db' database. Please check your configuration.
rails aborted!
PG::ConnectionBad: 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?

The error is not related to docker but related to postgres, open postgres configuration file pg_hba.conf and set it to allow permissions from localhost and restart the postgres server. Hope it helps šŸ‘

Related

How to expose mysql port?

I'm trying to expose the port 3310 to allow remote MySQL connection. So far, I have this configuration:
database:
container_name: sfapi_db
restart: always
ports:
- 3310:3306
build:
context: ./docker/database
environment:
- MYSQL_DATABASE=${DB_NAME}
- MYSQL_USER=${DB_USER}
- MYSQL_PASSWORD=${DB_PWD}
- MYSQL_ROOT_PASSWORD=${DB_PWD_ROOT}
volumes:
- dbdata:/var/lib/mysql
- type: bind
source: ./docker/database/my.cnf
target: /etc/mysql/my.cnf
and this is the Dockerfile:
FROM mariadb:latest
CMD ["mysqld"]
EXPOSE 3310
here the my.cnf:
[client-server]
# Port or socket location where to connect
port = 3310
socket = /run/mysqld/mysqld.sock
# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/
when I run docker-compose up --build -d and access to the container list, I get:
I also opened the port on the server using sudo ufw allow 3310. Also, I cannot use the port 3306 'cause it's used by another container, that's why I'm using 3310.
For some reason, when I go there and check if the port 3310 is opened, I always get that is closed.
How can I fix this?
If the my.cnf resides in the container. Then the port used should be 3306
[client-server]
# Port or socket location where to connect
port = 3306
socket = /run/mysqld/mysqld.sock
Since in the context of the container, this is the port that exposes mysql.
So what probably happens is that you expose 3306 but the container has assigned 3310 for the mysql server. Thus 3306 is indeed closed.

Docker compose container is not visible inside pgAdmin4

I created a docker file to create a pgAdmin4 container and a postgres container.
version: '3.8'
services:
postgres:
container_name: pg_container
image: postgres
restart: always
environment:
POSTGRES_USER: webquiver
POSTGRES_PASSWORD: webquiver
POSTGRES_DB: quiver_db
ports:
- "5432:5432"
pgadmin:
container_name: pgadmin4_container
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: admin
ports:
- "5050:80"
When running docker compose up I can go to the localhost:5050 to reach pgAdmin4 and login in with my credentials you can see in the code.
But when I use the dropdown menu for servers, it is empty. nothing is created. And I can not create any server there. It does not allow me to. I get the Error:
Unable to connect to server:
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?
please help. THX ^^
greetings
I've reproduced your docker-compose file and everything is fine with it.
I'm guessing there is a misunderstanding how postgres and pgadmin are interacting with each other from scratch.
pgadmin is only the frontend that you can connect to a postgres database, it's not automatically searching for any existing postgres server.
I'm able to add the server inside the pgadmin with this host and port: postgres:5432 As inside the docker will resolve the service name as hostname postgres to the other service pgadmin.
This configuration will be lost, when the composition is restartet, as no volumes to persist the configuration are specified. Therefore, you have to follow steps from Importing-servers and repeat them every time you start the composition.
eg: build you own pgadmin image that specifies the json to import at startup
Use the docker compose build: property to specify a source
Ok after a night of sleep I found the issue. For the server Host I wrote the db name and not the real container name. So pgAdmin could not find the container.
Have a nice day. : )

Cannot connect to container within compose but host connectivity works

Using the docker compose file
version: '3'
services:
sqlserver:
image: microsoft/mssql-server-linux:2017-latest
ports:
- 1401:1433
volumes:
- ./db:/tmp/data
environment:
- ACCEPT_EULA=Y
- "MSSQL_SA_PASSWORD='Password99'"
command:
- /tmp/data/run.sh
api:
build:
context: .
dockerfile: dockerfile
depends_on:
- sqlserver
links:
- sqlserver
ports:
- 5000:80
With appsettings.json connection string:
"ConnectionStrings": {
"Entities": "data source=(local),1401;initial catalog=DB_Test;user id=sa;password=Password99;enlist=False;multipleactiveresultsets=True;connect timeout=30;App=EntityFramework"
},
I get the error
api_1 | System.Data.SqlClient.SqlException (0x80131904):
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: TCP
Provider, error: 40 - Could not open a connection to SQL Server)
A connection from a host running api instance to the sql server instance running in docker-compose works fine, so the SQL instance running in the container is fine.
Can anyone help with connectivity between api & sql inside docker?
Thanks!
Inside docker the hostname for the database will be "sqlserver" not localhost or 127.0.0.1

Rails Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?

I'm building a rails app and I'm having trouble installing docker.
The build works, but when I try to create the DB with this command :
docker-compose run web bundle exec rails db:create
it throws this 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: Address not available
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
I've try to rename the host from localhost to db, it didn't work and I also tried to edit my etc/hosts.
docker-compose.yml
version: '3'
services:
localhost:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- localhost
working_dir: /myapp
Dockerfile
FROM ruby:2.6.2-alpine
RUN apk add --no-cache build-base postgresql-dev tzdata libxml2-dev libxslt-dev nodejs yarn
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
COPY package.json /myapp/package.json
RUN gem install bundler:2.1.2
RUN bundle install
COPY . /myapp
RUN yarn install --check-files
database.yml
default: &default
adapter: postgresql
encoding: unicode
host: localhost
username: postgres
password:
# 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: myapp_development
/etc/hosts
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section
127.0.0.1 postgres
How can I fix this issue ? I'm not sure what I'm missing!
In Docker each "service" runs in its own container. So you would have the Rails app and the database in different containers. Each service is conceptually speaking a different server.
Your error (Is the server running on host "localhost" (127.0.0.1)..) suggests that your Rails application is looking for a database engine on the same machine. That DB is running on a separate server.
In a docker-compose file each service is addressable by its name. So change the connection string or setting in your Rails app from localhost to db or something.
Edit: I just now see that you named your DB server localhost, that's asking for trouble. Change that to db or something? :)

iOS postgres in docker-compose with port mapping

I have the following docker compose:
version: '3'
services:
postgres:
container_name: test-psql
image: postgres:10.4-alpine
restart: always
ports:
- 5432:5432
volumes:
- ~/docker_data/postgres:/data/postgres
environment:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_DB: test
I start everything with docker-compose up and then docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f4cae94afaa6 postgres:10.4-alpine "docker-entrypoint.sā€¦" 48 seconds ago Up 47 seconds 0.0.0.0:5432->5432/tcp test-psql
But when trying to connect to it with psql
psql -h localhost -U test
I get the error
psql: 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?
What you're trying is correct - I tested it on my end just in case and it's working as intended. I'd suggest checking your firewall configuration.

Resources