Docker connection refused between containers - postgresql

I'm using Postgres, Redis and Node.js (adding dependencies with yarn), and trying to integrate it all with a docker-compose.yml file.
I have the following docker-compose.yml:
version: '3'
services:
postgres:
image: postgres:latest
restart: unless-stopped
environment:
- POSTGRES_DB=mybase
- POSTGRES_USER=myuser
- POSTGRES_PASSWORD=mypass
redis:
image: redis:latest
restart: unless-stopped
migrate:
build: .
entrypoint: node_modules/.bin/sequelize db:migrate --config src/config/database.js --migrations-path src/database/migrations/
volumes:
- ./:/app
- /app/node_modules
depends_on:
- postgres
wavetech-be:
build:
dockerfile: Dockerfile
context: .
restart: on-failure
volumes:
- /app/node_modules
- ./:/app
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379
- DB_HOST=postgres
- DB_USER=myuser
- DB_PASS=mypass
- DB_PORT=5432
- DB_NAME=mybase
depends_on:
- redis
- migrate
And the following Dockerfile:
FROM node:alpine
WORKDIR "/app"
COPY ./package.json ./
RUN apk add yarn
RUN yarn
COPY . .
CMD [ "yarn", "dev" ]
However, when I docker-compose up, I keep getting connection problems with both databases:
migrate_1 |
migrate_1 | ERROR: connect ECONNREFUSED 127.0.0.1:5432
migrate_1 |
...
wavetech-be_1 | (node:85) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6379

The answer has two parts:
First, as pointed out by #jonrsharpe, the description of the migrate service was lacking the environment variables. So, as it is with the volumes, each service needs its own environment variables configured.
migrate:
build: .
entrypoint: node_modules/.bin/sequelize db:migrate --config src/config/database.js --migrations-path src/database/migrations/
volumes:
- ./:/app
- /app/node_modules
environment:
- DB_HOST=postgres
- DB_USER=myuser
- DB_PASS=mypass
- DB_PORT=5432
- DB_NAME=mybase
- APP_PORT=3000
depends_on:
- postgres
Second, I am using Bull to manage my Redis server. I was importing a config and passing it directly to Redis, so:
import redisConfig from '../../config/redis';
...
init() {
this.queues = Object.values(jobs).map(job => ({
bull: new BullQueue(job.key, redisConfig),
name: job.key,
handle: job.handle,
}));
}
And it turns out that Bull was trying to just use the default Redis configuration. When I passed the environment variables directly into the Bull config, it worked properly:
init() {
this.queues = Object.values(jobs).map(job => ({
bull: new BullQueue(job.key, {
redis: {
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
},
}),
name: job.key,
handle: job.handle,
}));
}

Related

knex connection string with docker compose fails to connect postgres

I am new to docker and knex. I am connecting docker postgresql with knex connection string. But I am getting below error. I am changing db env variables a lot. But still I am getting the same error. Where I am missing.
knexfile
module.exports = {
client: "pg",
connection: "postgresql://postgres:postgres#localhost:5432/apps",
migrations: {
directory: "./migrations",
tableName: "knex_migraation_apps"
},
}
docker-compose.yml:
version: "3.9"
volumes:
apps-data:
services:
apps:
build: .
depends_on:
- postgres
ports:
- "3001:3001"
environment:
- "APPS_PORT=3001"
postgres:
image: postgres:14.1-alpine
environment:
- "POSTGRES_DB=apps"
ports:
- "5432:5432"
volumes:
- apps-data:/var/lib/pgsql/data
pgadmin-compose:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: "placeholder#example.com"
PGADMIN_DEFAULT_PASSWORD: "fakepassword123!"
ports:
- "16543:80"
depends_on:
- postgres

How to add ParseServer to GraphQL Mesh file

I am trying to connect via this documentation of GraphQL Mesh the ParseServer which is a docker container to Mesh via the .meshrc.yaml file. But I am not sure how to send the MASTER Key and Application ID in order to do the proper connection.
What I've tried already is:
schemaHeaders
operationHeaders
Neither of them worked. When I am running the graphql_mesh from my docker-compose I am getting the fallowing error:
Failed to generate the schema Error: Failed to fetch introspection from http://localhost:1337/graphql: Error: connect ECONNREFUSED 127.0.0.1:1337 2023-02-10T13:38:52.347296144Z at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)
Below I my docker files and my .meshrc.yaml.
docker-compose.yml file:
version: '3.9'
services:
database:
image: mongo:6.0.4
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
volumes:
- ${HOME}/_DOCKER_DATA_/database:/data/db
networks:
- my_network
server:
restart: always
image: parseplatform/parse-server:5.4.1
ports:
- 1337:1337
environment:
- PARSE_SERVER_APPLICATION_ID=COOK_APP
- PARSE_SERVER_APPLICATION_NAME=COOK_NAME
- PARSE_SERVER_MASTER_KEY=MASTER_KEY_1
- PARSE_SERVER_DATABASE_URI=mongodb://admin:admin#mongo/parse_server?authSource=admin
- PARSE_SERVER_URL=http://10.0.2.2:1337/parse
- PARSE_SERVER_MOUNT_GRAPHQL=true
links:
- database:mongo
volumes:
- ${HOME}/_DOCKER_DATA_/server:/data/server
networks:
- my_network
dashboard:
image: parseplatform/parse-dashboard:5.0.0
ports:
- "4040:4040"
depends_on:
- server
environment:
- PARSE_DASHBOARD_APP_ID=COOK_APP
- PARSE_DASHBOARD_MASTER_KEY=MASTER_KEY_1
- PARSE_DASHBOARD_USER_ID=admin
- PARSE_DASHBOARD_USER_PASSWORD=admin
- PARSE_DASHBOARD_ALLOW_INSECURE_HTTP=true
- PARSE_DASHBOARD_SERVER_URL=http://localhost:1337/parse
- PARSE_DASHBOARD_GRAPHQL_SERVER_URL=http://localhost:1337/graphql
volumes:
- ${HOME}/_DOCKER_DATA_/dashboard:/data/dashboard
networks:
- my_network
graphql_mesh:
build:
context: .
dockerfile: Dockerfile_graphql_mesh
volumes:
- ./work/.meshrc.yaml:/work/.meshrc.yaml
ports:
- "4000:4000"
stdin_open: true
tty: true
networks:
- my_network
networks:
my_network:
driver: bridge
The Dockerfile_graphql_mesh:
FROM node:19.6.0-alpine3.17
COPY work /work
WORKDIR /work
RUN yarn add #graphql-mesh/cli
RUN yarn add graphql
RUN yarn add #graphql-mesh/graphql
RUN yarn add #graphql-mesh/runtime
RUN yarn add #envelop/auth0
CMD yarn run mesh dev
.meshrc.yaml:
sources:
- name: ParseServer_3
handler:
graphql:
endpoint: http://localhost:1337/graphql
schemaHeaders:
X-Parse-Application-Id: 'COOK_APP'
X-Parse-Master-Key: 'MASTER_KEY_1'
serve:
playground: true
I am trying to get the propper conection and generation of schema.grapql via the GraphQL Mesh.

docker compose phpmyadmin php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution

I am trying to set up a docker-pod with laravel, mariadb, nginx, redis and phpmyadmin. The laravel webspace works finde but if i switch to port 10081 like configured in the docker-compose.yml i am not able to login with the root account.
it sais " mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution"
i already tried to configure a "my-network" which links all of the container, but if i understand docker right there is already a "defaul" network which does this. It didnt change the error message anyway.
here is my full docker-compose file
version: "3.8"
services:
redis:
image: redis:6.0-alpine
expose:
- "6380"
db:
image: mariadb:10.4
ports:
- "3307:3306"
environment:
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: laravel
volumes:
- db-data:/var/lib/mysql
nginx:
image: nginx:1.19-alpine
build:
context: .
dockerfile: ./docker/nginx.Dockerfile
restart: always
depends_on:
- php
ports:
- "10080:80"
networks:
- default
environment:
VIRTUAL_HOST: cockpit.example.de
volumes:
- ./docker/nginx.conf:/etc/nginx/nginx.conf:ro
- ./public:/app/public:ro
php:
build:
target: dev
context: .
dockerfile: ./docker/php.Dockerfile
working_dir: /app
env_file: .env
restart: always
expose:
- "9000"
depends_on:
- composer
- redis
- db
volumes:
- ./:/app
- ./docker/www.conf:/usr/local/etc/php-fpm.d/www.conf:ro
links:
- db:mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
ports:
- 10081:80
restart: always
environment:
PMA_HOST : db
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: secret
depends_on:
- db
#user: "109:115"
links:
- db:mysql
node:
image: node:12-alpine
working_dir: /app
volumes:
- ./:/app
command: sh -c "npm install && npm run watch"
composer:
image: composer:1.10
working_dir: /app
#environment:
#SSH_AUTH_SOCK: /ssh-auth.sock
volumes:
- ./:/app
#- "$SSH_AUTH_SOCK:/ssh-auth.sock"
- /etc/passwd:/etc/passwd:ro
- /etc/group:/etc/group:ro
command: composer install --ignore-platform-reqs --no-scripts
volumes:
db-data:
Make sure you have defined all attributes correctly for phpmyadmin container, in the current case there was the absence of -network definition
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: phpmyadmin
restart: always
ports:
# 8080 is the host port and 80 is the docker port
- 8080:80
environment:
- PMA_ARBITRARY:1
- PMA_HOST:mysql
- MYSQL_USERNAME:root
- MYSQL_ROOT_PASSWORD:secret
depends_on:
- mysql
networks:
# define your network where all containers are connected to each other
- laravel
volumes:
# define directory path where you shall store your persistent data and config
# files of phpmyadmin
- ./docker/phpmyadmin
Maybe your container cannot start because its volume contains incompatible data. It can happen if you downgrade the version of mysql or mariadb image.
You can resolve the problem if you remove the volume and import the database again. Maybe you have to create a backup first.

Docker file wait for postgres container

Hello I have the following error in my node project:
(node:51) UnhandledPromiseRejectionWarning: Error: getaddrinfo
ENOTFOUND ${DB_HOST}
I'm thinking the problem is that my postgress is not yet started when my project starts
and so I'm not able to think of a solution on how to start my container after my postgres is ready, I read something about dockerize, but I'm not able to imagine how to apply
my docker file:
FROM node:lts-alpine
RUN mkdir -p /home/node/api/node_modules && chown -R node:node /home/node/api
WORKDIR /home/node/api
COPY ormconfig.json .env package.json yarn.* ./
USER node
RUN yarn
COPY --chown=node:node . .
EXPOSE 4000
CMD ["yarn", "dev"]
my docker compose:
version: '3.7'
services:
ci-api:
build: .
container_name: ci-api
volumes:
- .:/home/node/api
- /home/node/api/node_modules
ports:
- '${SERVER_PORT}:${SERVER_PORT}'
depends_on:
- ci-postgres
networks:
- ci-network
ci-postgres:
image: postgres:12
container_name: ci-postgres
ports:
- '${DB_PORT}:5432'
environment:
- ALLOW_EMPTY_PASSWORD=no
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASS}
- POSTGRES_DB=${DB_NAME}
volumes:
- ci-postgres-data:/data
networks:
- ci-network
volumes:
ci-postgres-data:
networks:
ci-network:
driver: bridge
and this is my .env
SERVER_PORT=4000
DB_HOST=ci-postgres
DB_PORT=5432
DB_USER=spirit
DB_PASS=api
DB_NAME=emasa_ci
You can reference the below docker-compose.yml in which depends_on, healthcheck and links are added as web service depends on db service.
Reference:
Postgresql Container is not running in docker-compose file - Why is this?
version: "3"
services:
webapp:
build: .
container_name: webapp
ports:
- "5000:5000"
links:
- postgres
depends_on:
postgres:
condition: service_healthy
postgres:
image: postgres:11-alpine
container_name: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_DB=tmp
- POSTGRES_USER=tmp
- POSTGRES_PASSWORD=tmp_password
volumes: # Persist the db data
- database-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
volumes:
database-data:

DB connection error Error: connect ECONNREFUSED 127.0.0.1:5432?

Running a docker container with postgres and npm produces this error, while running them separetly
(npm and docker) doesn't. What seems to be the error here?
My docker-compose.yml:
version: "3.5"
services:
db:
image: postgres:12.1
ports:
- 5432:5432
environment:
- FLYWAY_URL=jdbc:postgresql://db:5432/
- POSTGRES_USER=myuser
- POSTGRES_PASSWORD=mypass
- POSTGRES_DB=mydb
volumes:
- ./db/data:/var/lib/postgresql/data
migrate:
image: boxfuse/flyway
entrypoint: ["sh", "-c", "/flyway/wait-for.sh db:5432 -- flyway migrate"]
depends_on:
- db
volumes:
- ./common/migrations/:/flyway/sql:rw
- ./common/scripts/wait-for.sh:/flyway/wait-for.sh:rw
environment:
# - FLYWAY_LOCATIONS=classpath:/common/migrations/
- FLYWAY_PASSWORD=mypass
- FLYWAY_USER=myuser
- FLYWAY_URL=jdbc:postgresql://db:5432/mydb?user=myuser&password=mypass
- FLYWAY_CONNECT_RETRIES=30
networks:
default:
name: mydb-local
services:
example-service:
build: .
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- 3000:3000
- 9229:9229
command: npm start
Thing I have checked:
postgresql.conf & pg_hba.conf accepts connections.
DB credentials are correct.
Db is running