Docker - Compose I can't create postgress serv

2020-04-27 10:24发布

问题:

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

回答1:

The cause of the problem can be seen in the log that you provided.

2020-03-17 23:48:08.890 UTC [81] FATAL: data directory "/var/lib/postgresql/data" has wrong ownership

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

services:
  emasa-postgres:
    ...
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
    - postgres-compose-network

volumes:
  pgdata:

I still have doubts about my compose which localhost to use in pgadmin

In order to connect to your emasa-postgres from pgadmin, you can set the hostname and network alias for the emasa-postgres container like so:

services:
    emasa-postgres:
        hostname: my_super_cool_postgres
        image: postgres
        networks:
            postgres-compose-network:
                aliases:
                    - my_super_cool_postgres

Instead of using localhost or db you should then be able to connect to the database via the host my_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.

services:
    teste-pgadmin-compose:
        image: dpage/pgadmin4
        links:
            - "emasa-postgres:my_super_cool_postgres"

This time, you have to set it for the teste-pgadmin-compose (or from wherever you need to access to the emasa-postgres container). The host my_super_cool_postgres and port 5432 should then be accessible.



回答2:

Your containers are part of a bridge network for automatic DNS resolution as you defined in the docker-compose.yml and use postgres-compose-network wich you defined to be a bridge

networks:
  postgres-compose-network:
    driver: bridge

Try this :

  1. Run 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 like name-of-project-directory_default this should be your network.
  2. Next run docker network inspect [name]_default to inspect the network, it will print something like this :

    [
    {
        "Name": "dockerizing_default",
        "Id": "98f8a8ef5153fbe91acc28ed0688179f75e08b321b72aab0abe7bf57e1880cc8",
        "Created": "2020-03-07T14:13:49.446907044+01:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "dockerizing",
            "com.docker.compose.version": "1.25.4"
        }
    }
    ]
    
  3. find the IPv4Address , you should be using this ip address instead of localhost , it could be 172.18.0.2 I hope this will solve the issue



回答3:

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



回答4:

Try hostname is 127.0.0.1:4532. Also make sure postgres container running use docker ps

UPDATE: This should be worked

volumes:
      - ./pgdata:/var/lib/postgresql/data/pgdata
ports:
    - "5432:5432"
environment:
    PGDATA: /var/lib/postgresql/data/pgdata