Hello i'm trying to create a postgres compose docker pg admin 4 and node
but I'm having difficulties and I can't create my server:
I've tried with all possible names and I don't know what I'm wrong:
my docker compose:
version: "3.7"
services:
emasa-postgres:
image: postgres
environment:
POSTGRES_PASSWORD: emasa@
POSTGRES_USER: postgres
POSTGRES_DB: emasa
volumes:
- ./pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
networks:
- postgres-compose-network
web:
image: emasa-web
depends_on:
- emasa-postgres
ports:
- "4000:4000"
networks:
- postgres-compose-network
teste-pgadmin-compose:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: "emasa@hotmail.com"
PGADMIN_DEFAULT_PASSWORD: "emasa"
ports:
- "16543:80"
depends_on:
- emasa-postgres
networks:
- postgres-compose-network
networks:
postgres-compose-network:
driver: bridge
DockerFile:
FROM node as builder
WORKDIR usr/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM node
WORKDIR usr/app
COPY package*.json ./
RUN npm install --production
COPY --from=builder /usr/app/dist ./dist
COPY ormconfig.docker.json ./ormconfig.json
COPY .env .
expose 4000
CMD node dist/src/index.js
my orm config:
{
"type": "postgres",
"host": "db",
"port": 5432,
"username": "postgres",
"password": "emasa@",
"database": "postgres",
"synchronize": true,
"logging": false,
"entities": ["src/entity/**/*.ts"],
"migrations": ["src/migration/**/*.ts"],
"subscribers": ["src/subscriber/**/*.ts"],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
}
my docker-compose loggers:
Attaching to back-end_web_1, back-end_emasa-postgres_1, back-end_teste-pgadmin-compose_1
emasa-postgres_1 | The files belonging to this database system will be owned by user "postgres".
emasa-postgres_1 | This user must also own the server process.
emasa-postgres_1 |
emasa-postgres_1 | The database cluster will be initialized with locale "en_US.utf8".
emasa-postgres_1 | The default database encoding has accordingly been set to "UTF8".
emasa-postgres_1 | The default text search configuration will be set to "english".
emasa-postgres_1 |
emasa-postgres_1 | Data page checksums are disabled.
emasa-postgres_1 |
emasa-postgres_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
emasa-postgres_1 | creating subdirectories ... ok
emasa-postgres_1 | selecting dynamic shared memory implementation ... posix
emasa-postgres_1 | selecting default max_connections ... 20
emasa-postgres_1 | selecting default shared_buffers ... 400kB
emasa-postgres_1 | selecting default time zone ... Etc/UTC
emasa-postgres_1 | creating configuration files ... ok
emasa-postgres_1 | 2020-03-17 23:48:08.890 UTC [81] FATAL: data directory "/var/lib/postgresql/data" has wrong ownership
emasa-postgres_1 | 2020-03-17 23:48:08.890 UTC [81] HINT: The server must be started by the user that owns the data directory.
emasa-postgres_1 | child process exited with exit code 1
emasa-postgres_1 | initdb: removing contents of data directory "/var/lib/postgresql/data"
emasa-postgres_1 | running bootstrap script ... web_1 | Error: getaddrinfo ENOTFOUND db
my container ls:
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a20bcd4f105 emasa-web "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 0.0.0.0:4000->4000/tcp back-end_web_1
b377e650e772 dpage/pgadmin4 "/entrypoint.sh" 14 minutes ago Up 14 minutes 443/tcp, 0.0.0.0:16543->80/tcp back-end_teste-pgadmin-compose_1
[![enter image description here][2]][2]
I've really tried with the container names:
emasa-postgres_1 emasa-postgres
and to no avail
The cause of the problem can be seen in the log that you provided.
You are probably running the docker on a Windows machine? If so, it seems to be a common bug - check here. So, you might want to use a persistent docker volume instead, a la
In order to connect to your
emasa-postgres
frompgadmin
, you can set the hostname and network alias for theemasa-postgres
container like so:Instead of using
localhost
ordb
you should then be able to connect to the database via the hostmy_super_cool_postgres
.This way, you should be able to connect to your postgres container from pgadmin. If it still doesn't work for you, you can also try using the links instead, which is a legacy feature of Docker, but should work, e.g.
This time, you have to set it for the
teste-pgadmin-compose
(or from wherever you need to access to theemasa-postgres
container). The hostmy_super_cool_postgres
and port5432
should then be accessible.it’s not db, change this "host": "db"
To this
"host": "emasa-postgres”
emasa-postgres is the name you use instead of the ip address for the connection between services
Change that in your orm config
Your containers are part of a bridge network for automatic DNS resolution as you defined in the
docker-compose.yml
and usepostgres-compose-network
wich you defined to be a bridgeTry this :
docker network ls
to find out the networks created in your docker runtime all networks will be listed in the console, you will find a[name]_default
something likename-of-project-directory_default
this should be your network.Next run
docker network inspect [name]_default
to inspect the network, it will print something like this :find the
IPv4Address
, you should be using this ip address instead oflocalhost
, it could be172.18.0.2
I hope this will solve the issueTry hostname is
127.0.0.1:4532
. Also make sure postgres container running usedocker ps
UPDATE: This should be worked