This is the env file code that I am using to run the docker container:
POSTGRES_HOST="localhost"
POSTGRES_PORT=5432
POSTGRES_USERNAME="postgres"
POSTGRES_PASSWORD="123"
POSTGRES_DATABASE="netabe"
This is the docker.compose.yml file:
postgres:
container_name: postgres_container
image: 'postgres:latest'
environment:
POSTGRES_USER: ${POSTGRES_USERNAME}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DATABASE}
ports:
- 5432:5432
networks:
app_network:
ipv4_address: 172.26.0.10
restart: unless-stopped
healthcheck:
test: ['CMD', 'pg_isready -U postgres']
interval: 5s
timeout: 5s
retries: 5
volumes:
- postgres:/data/postgres
It runs the postgres in docker container successfully but when I run the nest app then it says [Nest] 13620 - 05/01/2023, 6:56:41 pm ERROR [SequelizeModule] Unable to connect to the database. Retrying (1).... I don't understand why this is happening. My postgres is also up and running and the database is also created in postgres but in the console it is throwing an error. The start:dev command: NODE_ENV=development nest start --watch
The postgresqlProvider is a different module and then I imported this in my app.module.ts file.
the postgresqlprovider module code:
#Module({
imports: [
SequelizeModule.forRootAsync({
imports: [PostgresConfigModule],
useFactory: async (postgresConfigService: PostgresConfigService) => ({
dialect: 'postgres' as string,
host: postgresConfigService.host as string,
port: postgresConfigService.port,
username: postgresConfigService.username,
password: postgresConfigService.password,
database: postgresConfigService.database,
keepConnectionAlive: true,
synchronize: true,
autoLoadModels: true,
}),
inject: [PostgresConfigService],
} as SequelizeModuleAsyncOptions),
],
})
export class PostgresDatabaseProviderModule {}
It has nothing to do with the docker however the problem I found is with the synchronize option. Whenever it is false it works just fine and does not throw any error but when it is set to true then it gives the error that I mentioned above. I don't understand why this is happening.
Related
I try to connect to my Postgres database built by docker in DataGrip but I get connection error.
Here is my application.yml file:
spring:
jpa:
database: POSTGRESQL
show-sql: true
hibernate:
ddl-auto: update
datasource:
url: jdbc:postgresql://db:5432/postgis_db?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true
username: postgres
password: postgres
Here is docker-compose.yml file:
version: '3.8'
services:
db:
image: postgres:latest
restart: always
volumes:
- db:/var/lib/postgresql/data
environment:
POSTGRES_DB: postgis_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
expose:
- 5432
networks:
- app-network
geolocation-service:
image: geolocation-service
build:
context: .
dockerfile: dockerfile
restart: always
ports:
- "8080:8080"
networks:
- app-network
volumes:
db:
networks:
app-network:
DataGrip connection settings:
And the error I get:
Does anybody knows how to solve this?
Connecting to this db by shell works fine:
docker exec -it a37 psql -U postgres postgis_db
Please check pg_hba.conf file, you have to allow connections from all hosts for this user, by default it is restricted to localhost.
I have a samle app I'm using docker-compose to run locally on my machine. The web app is in one container and the db (postgres) in another.
I am having an connection issue that I can't work through.
docker-compose
version: '3.8'
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: 'password'
POSTGRES_DB: 'postgres'
POSTGRES_USER: 'postgres'
volumes:
- ./postgres-db:/var/lib/postgresql/data
ports:
- '5432:5432'
app:
build:
context: .
dockerfile: app/Dockerfile
restart: always
environment:
APP_FRONTEND_PORT: '8080'
DB_PORT: '5433'
DB_HOST: 'db'
ports:
- '8080:8080'
depends_on:
- 'db'
volumes:
postgres-db:
Dockerfile
FROM golang:latest
WORKDIR /scratch
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /bin/frontend ./...
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /go/bin/
COPY --from=build /bin/frontend /go/bin/frontend
ENTRYPOINT ["/go/bin/frontend"]
Both containers are running and I'm able to log into the running postgres container and postgres us running.
When I try to run a update from the US I get a 500 error and it does not seem like the app container can communicate with the db container. I'm not sure what I'm missing
client side error when trying to make a call to update date:
encountered err: failed to begin transaction: failed to connect to `host=db user=postgres database=postgres`: dial error (dial tcp 172.29.0.2:5433: connect: connection refused)
docker ps yeilds:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19eeed869434 sample_app "/go/bin/frontend" 48 minutes ago Up 48 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sample_app_1
84804f00c751 postgres "docker-entrypoint.s…" 48 minutes ago Up 48 minutes 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp sample_app_db_1
$
As per stated in https://docs.docker.com/network/bridge/, you need to put both services into a user-defined bridge network for them to refer to each other by the container names. Here is how to do it inside docker-compose.yml:
Define a custom bridge network:
networks:
some-name:
driver: bridge
Put both services into that network:
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: 'password'
POSTGRES_DB: 'postgres'
POSTGRES_USER: 'postgres'
volumes:
- ./postgres-db:/var/lib/postgresql/data
ports:
- '5432:5432'
networks:
- some-name
app:
build:
context: .
dockerfile: app/Dockerfile
restart: always
environment:
APP_FRONTEND_PORT: '8080'
DB_PORT: '5433'
DB_HOST: 'db'
ports:
- '8080:8080'
depends_on:
- 'db'
networks:
- some-name
Force specific container names especially the one being referred to by the other, otherwise docker-compose will add prefix and suffix to the service name as the container name like sample_app_db_1:
services:
db:
container_name: db
image: postgres
environment:
POSTGRES_PASSWORD: 'password'
POSTGRES_DB: 'postgres'
POSTGRES_USER: 'postgres'
volumes:
- ./postgres-db:/var/lib/postgresql/data
ports:
- '5432:5432'
networks:
- some-name
I created a docker image with the following docker-compose.yml file
version: "3.7"
services:
db:
image: postgres:alpine
container_name: db
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_INITDB_ARGS: '-A md5'
volumes:
- ./pgdata:/var/lib/postgressql/data
ports:
- "5432:5432"
healthcheck:
test: ["CMD", "psql", "postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}#db/${POSTGRES_DB}"]
interval: 30s
timeout: 5s
retries: 5
api:
build: api
container_name: api
volumes:
- ./api/migrations:/migrations
ports:
- "8080:8080"
links:
- db
depends_on:
db:
condition: service_healthy
when I do docker-compose up everything is working fine. I am able to connect to Postgres, I can create tables. I can query those tables.
The only problem is that the ./pgdata directory is empty! why is that? since I have done volumes: - ./pgdata:/var/lib/postgressql/data I should have some files getting created in this directory as I create databases and tables right?
I did ls -al command in the pgdata directory and it shows nothing.
I entered the docker-hub-psql-site and checked the working directory.
https://hub.docker.com/_/postgres
In the case of postgresql, it was set as the PGDATA environment variable, and it was as follows.
Dockerfile
...
ENV PGDATA=/var/lib/postgresql/data
...
In other words, I have confirmed that there is a typo postgressql in your docker-compose.yaml, and it will be fixed if you modify it as follows.
version: "3.7"
services:
db:
image: postgres:alpine
container_name: db
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_INITDB_ARGS: '-A md5'
volumes:
# - ./pgdata:/var/lib/postgressql/data
- ./pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
healthcheck:
test: ["CMD", "psql", "postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}#db/${POSTGRES_DB}"]
interval: 30s
timeout: 5s
retries: 5
[NOTE]
Apart from the question, I know that the healthcheck option in docker-compose only supports version 2.
Please refer to the article below stackoverflow/docker-compose-healthcheck-does-not-work
after creating docker containers with docker compose file (below), I call
$ docker run myApp
However, I get
Error: getaddrinfo ENOTFOUND main_db
this only happens when both server and postgresql are in docker containers (I am able to connect to postgresql on localhost)
I'm running a NestJS app using TypeOrm to connect to a postgresql server
inside the app.module.ts where it boots up the connection my config should match my docker postgresql config. the host points to the container I created on docker main_db and I declared this as a dependency of my server, the main service. Everything should be on the same network webnet.:
TypeOrmModule.forRoot({
type: 'postgres',
host: 'main_db',
port: +process.env.POSTGRES_PORT,
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
autoLoadEntities: true,
synchronize: true,
logging: dbLogging,
}),
docker-compose.yml
version: '3.7'
services:
main:
container_name: main
build:
context: .
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- ${SERVER_PORT}:${SERVER_PORT}
- 9229:9229
command: npm run start:dev
env_file:
- .env
networks:
- webnet
depends_on:
- main_db
main_db:
container_name: main_db
image: postgres:12
restart: always
networks:
- webnet
environment:
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_USER: ${POSTGRES_USER}
PG_DATA: /var/lib/postgresql/data
ports:
- '${POSTGRES_PORT}:${POSTGRES_PORT}'
volumes:
- pgdata:/var/lib/postgresql/data
networks:
webnet:
volumes:
pgdata:
.env file
POSTGRES_PORT=5432
POSTGRES_USER=test
POSTGRES_PASSWORD=test
POSTGRES_DB=test
SERVER_PORT=3001
When creating a Dockerfile and a docker-compose.yml file and you call docker run myApp for the app defined inside of the Dockerfile instead of calling docker-compose up, you will see the app running; however, it will not start the containers defined in the docker-compose file. In the case of the NestJS server, it was running the server, but could not find the container with the database, since this container was not being spun up. Although the distinction between the app setup in the Dockerfile and the definition of the containers in the docker-compose.yml was clear, I didn't realize that the docker command didn't reference the docker-compose.yml. Thus, posting here in case anyone else has a similar confusion.
I'm trying to dockerize a NodeJS / PostgreSQL app, but i can't run knex migrations, I'm getting the following error :
Error: connect ECONNREFUSED 172.18.0.2:15432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1161:14)
Here is my docker-compose.yml :
version: "3"
services:
app:
build: .
depends_on:
- db
links:
- db
ports:
- "3000:3000"
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: users-microservice
DB_HOST: db
db:
image: postgres:10.4-alpine
expose:
- "5432"
ports:
- "15432:5432"
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: users-microservice
The Dockerfile for the 'app' service :
FROM node:10.1-alpine
EXPOSE 3000 9229 15432
COPY . /home/app
WORKDIR /home/app
RUN npm install
RUN npm install -g knex
CMD ./scripts/start.sh
and in the start.sh, the following command works :
until PGPASSWORD=$POSTGRES_PASSWORD psql -h "$DB_HOST" -U "$POSTGRES_USER" -c '\d'; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - executing command"
So I can connect to postgres through the CLI, but knex can't, why is that ? Am I getting something wrong, I'm new to Docker ?
Best regards,
I assume you're trying to run a command inside the service app when you see this error message (but maybe you can specify)?
My guess then would be that you're trying to connect to db:15432 from within app. Note, that saying
ports:
- "15432:5432"
only makes sure that you can call the service from your host machine on port 15432. But if you want to call the service db from within app, you still have to use db:5432.
The answer from #the-bass saved me. I'm going to include my docker-compose.yml and knexfile.js files to hopefully make this clearer for people with this issue who find this post.
What I was doing wrong was that I was not setting the host environment variable correctly in my app container. Once I changed it to just use db (the name of my postgres container) and made sure to include the port in my knexfile, all worked.
knexfile.js
require("dotenv").config();
module.exports = {
development: {
client: "pg",
connection: {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
port: 5432,
},
pool: {
min: 2,
max: 10,
},
migrations: {
directory: "./data/migrations",
},
},
production: {
client: "pg",
connection: {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
port: 5432,
},
pool: {
min: 2,
max: 10,
},
migrations: {
directory: "./data/migrations",
},
},
};
docker-compose.yml
version: "3.7"
services:
db:
image: postgres:12.2
restart: always
ports:
- 5432:5432
volumes:
- my-postgres-data:/var/lib/postgresql/data
environment:
- POSTGRES_USER=me
- POSTGRES_PASSWORD=password
- POSTGRES_DB=my-data
node-app:
image: node:12.16.2
command: sh -c "npm install && npm install -g knex && knex migrate:latest && npm start"
ports:
- 3001:3001
working_dir: /home/node/app
volumes:
- ./:/home/node/app
environment:
- NODE_ENV=development
- DB_HOST=db
- DB_USER=HG
- DB_PASSWORD=password
- DB_DATABASE=my-data
depends_on:
- db
links:
- db
volumes:
my-postgres-data: