0

I have the following docker-compose.yml file

version: '3'
services:
    postgres:
        image: 'postgres:latest'
    redis:
        image: 'redis:latest'
    nginx:
        restart: always
        build:
            dockerfile: Dockerfile.dev
            context: ./nginx
        ports:
            - '3050:80'
    api:
        build:
            dockerfile: Dockerfile.dev
            context: ./server
        volumes:
            - /app/node_modules
            - ./server:/app
        environment: 
            - REDIS_HOST=redis
            - REDIS_PORT=6379
            - PGUSER=postgres
            - PGHOST=postgres
            - PGDATABASE=postgres
            - PGPASSWORD=postgres_password
            - PGPORT=5432
    client:
        build:
            dockerfile: Dockerfile.dev
            context: ./client
        volumes:
            - /app/node_modules
            - ./client:/app
    worker:
        build:
            dockerfile: Dockerfile.dev
            context: ./worker
        volumes:
            - /app/node_modules
            - ./worker:/app

When I run docker-compose up --build, everything works fine apart from postgres, which provides me with the error below. I've tried applying the accepted answer for this question but the error message won't go away; not sure what I'm missing here.

postgres_1  | Error: Database is uninitialized and superuser password is not specified.
postgres_1  |        You must specify POSTGRES_PASSWORD to a non-empty value for the
postgres_1  |        superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
postgres_1  | 
postgres_1  |        You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
postgres_1  |        connections without a password. This is *not* recommended.
postgres_1  | 
postgres_1  |        See PostgreSQL documentation about "trust":
postgres_1  |        https://www.postgresql.org/docs/current/auth-trust.html

I've got this keys file inside of my server folder, which I think was used on the course I'm following to provide the environment parameters of the api in the docker-compose file:

module.exports = {
    redisHost: process.env.REDIS_HOST,
    redisPort: process.env.REDIS_PORT,
    pgUser: process.env.PGUSER,
    pgHost: process.env.PGHOST,
    pgDatabase: process.env.PGDATABASE,
    pgPassword: process.env.PGPASSWORD,
    pgPort: process.env.PGPORT,
}

1 Answer 1

2

As per documentation, the postgres image needs the environment variable POSTGRES_PASSWORD to be set.

Something like this:

postgres:
  image: 'postgres:latest'
  environment:
    POSTGRES_PASSWORD: example

There's not much more to it. You can try to create a docker-compose.yml with just that code and see if it starts correctly with docker compose up (it should).

If it still doesn't there might be something wrong with the cached postgres:latest image. Might wanna try a docker image rm postgres and then calling the docker compose command again.

Warning: make sure you didn't have important data in the db before doing that.

Sign up to request clarification or add additional context in comments.

1 Comment

for future reference: my confusion was adding the POSTGRES_PASSWORD to the api environment, and not creating an environment for postgres in the dockerfile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.