Elixir on docker cannot connect to postgres - postgresql

I am trying to deploy elixir application on Docker container, it is successfully deployed but when I run an API endpoint it shows following error
{
errors: {
detail: "Internal Server Error"
}
}
after check logs docker logs [CONTAINER_ID] the following errors occurred (it cannot connect to database)
my database is on AWS Aurora and I am able to connect to it using pgadmin
18:21:26.624 [error] #PID<0.652.0> running ABC.Endpoint (connection #PID<0.651.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /api/v1/popular-searches/us/en
** (exit) an exception was raised:
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2850ms. This means requests are coming in and your connection pool cannot serve them fast enough. You can address this by:
1. Tracking down slow queries and making sure they are running fast enough
2. Increasing the pool_size (albeit it increases resource consumption)
3. Allowing requests to wait longer by increasing :queue_target and :queue_interval
See DBConnection.start_link/2 for more information
(ecto_sql 3.5.3) lib/ecto/adapters/sql.ex:751: Ecto.Adapters.SQL.raise_sql_call_error/1
(ecto_sql 3.5.3) lib/ecto/adapters/sql.ex:684: Ecto.Adapters.SQL.execute/5
(ecto 3.5.5) lib/ecto/repo/queryable.ex:229: Ecto.Repo.Queryable.execute/4
(ecto 3.5.5) lib/ecto/repo/queryable.ex:17: Ecto.Repo.Queryable.all/3
(ecto 3.5.5) lib/ecto/repo/queryable.ex:157: Ecto.Repo.Queryable.one!/3
(_api 0.1.1) lib/api_web/controllers/V1/cms_data_controller.ex:14: ApiWeb.V1.CMSDataController.get_popular_searches/2
(_api 0.1.1) lib/_api_web/controllers/V1/cms_data_controller.ex:1: ApiWeb.V1.CMSDataController.action/2
(_api 0.1.1) lib/_api_web/controllers/V1/cms_data_controller.ex:1: ApiWeb.V1.CMSDataController.phoenix_controller_pipeline/2
18:21:26.695 [error] Postgrex.Protocol (#PID<0.487.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (postgres:5432): non-existing domain - :nxdomain
18:21:26.714 [error] Postgrex.Protocol (#PID<0.479.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (postgres:5432): non-existing domain - :nxdomain
18:21:26.718 [error] Postgrex.Protocol (#PID<0.469.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (postgres:5432): non-existing domain - :nxdomain
18:21:26.810 [error] Postgrex.Protocol (#PID<0.493.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (postgres:5432): non-existing domain - :
I have checked the env variables in the container which are correct, database URL is correct, my Docker file looks file this:
# base image elixer to start with
FROM elixir:1.13.4
# install hex package manager
RUN mix local.hex --force
RUN curl -o phoenix_new.ez https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez
RUN mix archive.install ./phoenix_new.ez
RUN mkdir /app
COPY . /app
WORKDIR /app
ENV MIX_ENV=prod
ENV PORT=4000
ENV DATABASE_URL=postgres://[URL]
RUN mix local.rebar --force
RUN mix deps.get --only prod
RUN mix compile
RUN mix phx.digest
CMD mix phx.server
after building the images i start it like this:
docker build . -t [name]
docker run --name [name] -p 4000:4000 -d [name]
What am I doing wrong?
any help would be appreciated.

Related

MongoDB Cannot run tests when building Docker image - Server selection error: server selection timeout

I'm running a mongo image in a container with this config:
version: '3'
services:
mongodb:
image: mongo
ports:
- '27017:27017'
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_INITDB_DATABASE=microservices
volumes:
- ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
And I have started to build microservices with golang and I want to start dockerizing those as well to speed up the development process. However when I try to run this Dockerfile below it panics at the run test command with the error:
database url: mongodb://user:password#127.0.0.1:27017/microservices
server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: 127.0.0.1:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
panic: server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: 127.0.0.1:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
The db connection works since when I try to run the test from the go files, they pass. There only seems to be any problem when I try to run them from the container when building it up.
FROM golang:1.18 as build
WORKDIR /go/src/app
COPY . .
RUN go mod download
RUN go vet -v /go/src/app/...
RUN go test -v /go/src/app/...
RUN CGO_ENABLED=0 go build -o /go/bin/app/authsvc /go/src/app/authentication/main.go
FROM gcr.io/distroless/static-debian11
COPY --from=build /go/bin/app/authsvc /
COPY --from=build /go/src/app/authentication/.env /
CMD ["/authsvc"]
Change database url
try mongodb
database url: mongodb://user:password#mongodb:27017/microservices
So first of all you must check your default network ip with
docker network ls
Result
After get network id and use this command :
docker inspect <your network id>
Inside result schema get your Gateway ip
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "111.11.0.0/11",
"Gateway": "111.11.0.1"
}
]
}
and use that ip
database url: mongodb://user:password# < your Gateway ip > :27017/microservices

Cannot connect to my PostgreSQL server remotely from chainlink node terminal

I have tried everyting to connect my Chainlink node up to my postgresql database with no luck. I have scoured the interwebs for answers to no avail...
Here is the error message I am receiving:
[ERROR] failed to initialize database, got error failed to connect to `host=/tmp user=root database=`: dial error (dial unix /tmp/.s.PGSQL.5432: connect: no such file or directory)
Here is my .env file:
ROOT=/chainlink
LOG_LEVEL=debug
ETH_CHAIN_ID=42
MIN_OUTGOING_CONFIRMATIONS=2
LINK_CONTRACT_ADDRESS=0xa36085F69e2889c224210F603D836748e7dC0088
CHAINLINK_TLS_PORT=0
SECURE_COOKIES=false
GAS_UPDATER_ENABLED=true
ALLOW_ORIGINS=*
ETH_URL=wss://kovan.infura.io/ws/v3/id...
DATABASE_URL=https://chainlink-db-url://postgres:Password#chainlink-kovan:5432
I have tried every configuration of the connection string. Also I am able to connect to the db via pgAdmin no problem and the dbs are publicaly accessible.
The postgresql database is on AWS.
Please change the syntax of your DATABASE_URL to:
DATABASE_URL=postgresql://"username":"password"#"public-ip-pg-server":5432/"database-name"
just change:
"username" : you need to configure a new user, because the default/admin user postgres will not work for it.
"password" : password of the user
"public-ip-pg-server" : the public ip address of your postgresql-server
"database-name" : the name of your database
PS: delete all " in your syntax (;
Here is the link to the official documentation: https://docs.chain.link/docs/connecting-to-a-remote-database/

IllegalStateException: Can not connect to database. Please check connectivity and settings

CentOS 7
Docker 20.10.5
In my machine run PostgreSQL 9.5 and success open my db:
localhost:5432/sonar
And success open DB by PGAdmin
Nice.
Now in Docker I installed SonarQube 4.5. And want to connect to my db.
I try this:
sudo docker run -e SONARQUBE_JDBC_USERNAME=sonar -e SONARQUBE_JDBC_PASSWORD=sonar -e SONARQUBE_JDBC_URL=jdbc:postgresql://localhost:5432/sonar sonarqube:4.5.7
But I get error:
2021.04.20 11:47:55 INFO web[o.s.s.p.ServerImpl] SonarQube Server / 4.5.7 / e2afb0bff1b8be759789d2c1bc9348de6f519f83
2021.04.20 11:47:55 INFO web[o.s.c.p.Database] Create JDBC datasource for jdbc:postgresql://localhost:5432/sonar
2021.04.20 11:47:55 ERROR web[o.a.c.c.C.[.[.[/]] Exception sending context initialized event to listener instance of class org.sonar.server.platform.PlatformServletContextListener
java.lang.IllegalStateException: Can not connect to database. Please check connectivity and settings (see the properties prefixed by 'sonar.jdbc.').
at org.sonar.core.persistence.DefaultDatabase.checkConnection(DefaultDatabase.java:115) ~[sonar-core-4.5.7.jar:na]
at org.sonar.core.persistence.DefaultDatabase.start(DefaultDatabase.java:73) ~[sonar-core-4.5.7.jar:na]

CraftCMS with Redis: Failed to open redis DB connection (tcp://:, database=): 0 - Failed to parse address ":"

I have yii2 and yii2-redis installed on a CraftCMS project, but when I fire up the project, I get an error message that reads:
Database Exception – yii\db\Exception
Failed to open redis DB connection (tcp://:, database=): 0 - Failed to parse address ":"
Error Info: Failed to parse address ":"
I'm not quite sure what to do with this error. In my .env file, I put in:
REDIS_HOST=localhost
REDIS_PORT=3306
REDIS_PASSWORD=
REDIS_KEY_PREFIX=craft
But that only yielded a different error:
Database Exception – yii\db\Exception
Failed to open redis DB connection (tcp://:3306, database=): 0 - php_network_getaddresses:
getaddrinfo failed: nodename nor servname provided, or not known
Error Info: php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
Help?
Additional context: I didn't set up the build for this project, so I more or less inherited this Redis setup.
Update:
I went and dug into the config file for the Redis setup and found a misnaming between the .env variables and what was present in the .env file. Now I'm getting a completely different error:
"Received illegal data from redis: Redis command was: SELECT 0"
Please check that your configuration matches the environment variables you use.
Turns out I'd deleted the Redis database (don't remember how or when), so creating a new one (with the correct .env variables set) fixed the problem immediately.
Whew.

Postgresql connection with gorm dial error cannot assign requested address

I'm trying to use the new gorm v2 implementation with Postgresql (I use Docker for the Golang app and for Postgres). I tried to do it as shown in the gorm documentation.
That gave me the following error:
web_1 | 2020/09/19 19:25:57 /go/src/caiqueservice/main.go:36 failed
to connect to host=/tmp user=admin database=caique: dial error (dial
unix /tmp/.s.PGSQL.5432: connect: no such file or directory)
So since the documentation didn't specify host, but the error message set that to /tmp I set that value.
dsn := fmt.Sprintf("host=%v user=%v password=%v dbname=%v port=%v sslmode=disable",
os.Getenv("DB_HOST"),
os.Getenv("DB_USERNAME"),
os.Getenv("DB_PASSWORD"),
os.Getenv("DB_DATABASE"),
os.Getenv("DB_PORT"),
)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
.env
DB_HOST=localhost
DB_PORT=5432
DB_DATABASE=caique
DB_USERNAME=admin
DB_PASSWORD=password
Doing so gives me the following error message:
web_1 | 2020/09/19 19:36:47 /go/src/caiqueservice/main.go:36 failed
to connect to host=localhost user=admin database=caique: dial error
(dial tcp [::1]:5432: connect: cannot assign requested address)
The postgres db is reachable by pgadmin.
I don't know what to do next and help would be very much appreciated.
Inside a container, localhost refers to the container itself, not to the host machine. If you are using docker-compose, you should be able to connect to the postgres container (from the app container) using the postgres container name as the host name. If you are running both containers separately, you will need to connect them to the same docker network or link them.