dockerdocker-composemastodon

postgres won't start when installing docker mastodon


I am attempting to install the dockerized version of mastodon and although I've referred to several guides (including official documentation), I'm consistently getting an error connecting to the postgres database.

Here the relevant section of my docker-compose.yml:

  db:                                                                                                                                                
     restart: always                                                                                                                                  
     image: postgres:9.6-alpine                                                                                                                       
     shm_size: 256mb                                                                                                                                  
     networks:                                                                                                                                        
       - internal_network                                                                                                                             
     healthcheck:                                                                                                                                     
       test: ["CMD", "pg_isready", "-U", "postgres"]                                                                                                  
     volumes:                                                                                                                                         
       - ./postgres:/var/lib/postgresql/data 

... and my .env.production:

# PostgreSQL                                                                                                                                         
 # ----------                                                                                                                                         
 DB_HOST=/var/run/postgresql                                                                                                                          
 DB_USER=mastodon                                                                                                                                     
 DB_NAME=mastodon_production                                                                                                                          
 DB_PASS=[MY PASSWORD]   <--- (I've also tried leaving this blank with no difference in the result)                                                                                                                           
 DB_PORT=5432

But when I run (for example): $ docker-compose run --rm web rails db:migrate

I get the following output:

    rails aborted!
    PG::ConnectionBad: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

and $ docker logs mastodon_db_1 gives me:

Error: Database is uninitialized and superuser password is not specified.
       You must specify POSTGRES_PASSWORD to a non-empty value for the
       superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".

       You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
       connections without a password. This is *not* recommended.

       See PostgreSQL documentation about "trust":
       https://www.postgresql.org/docs/current/auth-trust.html

I'm relatively new to docker but (I think) I understand the basic concepts. However, a lot of Googling has me thinking that I'm the only one who's having this issue and I can't see what I'm missing.


Solution

  • The second error is because of missing ENV that is required for Postgres container.

    Error: Database is uninitialized and superuser password is not specified.
           You must specify POSTGRES_PASSWORD to a non-empty value for the
           superuser
    

    POSTGRES_PASSWORD

    This environment variable is required for you to use the PostgreSQL image. It must not be empty or undefined. This environment variable sets the superuser password for PostgreSQL. The default superuser is defined by the POSTGRES_USER environment variable

    You need to specify POSTGRES_PASSWORD ENV

      db:
        image: postgres
        restart: always
        environment:
          POSTGRES_PASSWORD: example
          POSTGRES_DB: mastodon_production
          POSTGRES_USER: mastodon
    
    

    postgres-docker-Environment Variables

    While the second error seems the host is invalid or you not consuming properly from the Environment variable.

    PG::ConnectionBad: could not connect to server: No such file or directory
    

    change the dot env file to

     DB_HOST=db                                                                                                                          
     DB_USER=mastodon                                                                                                                                     
     DB_NAME=mastodon_production                                                                                                                          
     DB_PASS=example  <--- (the one which is set in DB)                                                                                                                           
     DB_PORT=5432
    

    DB_HOST should be a service name to connect from another container to DB.