0
votes

I made docker+postgresql+nginx environment.
I could docker-compose up and ran containers.
So I tried rails db:migrate then error is happen.
But it seems postgres is working. Why I can't migrate?

I tried these commands
docker ps
docker-compose restart
rm /usr/local/var/postgres/postmaster.pid
ln -s /tmp/.s.PGSQL.5432 /var/lib/postgresql/.s.PGSQL.5432
docker-compose exec app /bin/sh & bundle exec rails db:migrate

error

could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Couldn't create 'se_videos_dev' database. Please check your configuration.
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 "/tmp/.s.PGSQL.5432"?
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/pg-1.2.3/lib/pg.rb:58:in `initialize'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/pg-1.2.3/lib/pg.rb:58:in `new'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/pg-1.2.3/lib/pg.rb:58:in `connect'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/postgresql_adapter.rb:46:in `postgresql_connection'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:887:in `new_connection'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `checkout_new_connection'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:910:in `try_to_checkout_new_connection'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:871:in `acquire_connection'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:593:in `checkout'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:437:in `connection'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1119:in `retrieve_connection'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_handling.rb:221:in `retrieve_connection'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_handling.rb:189:in `connection'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/postgresql_database_tasks.rb:12:in `connection'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/postgresql_database_tasks.rb:21:in `create'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:126:in `create'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:185:in `block in create_current'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:479:in `block (2 levels) in each_current_configuration'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:476:in `each'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:476:in `block in each_current_configuration'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:475:in `each'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:475:in `each_current_configuration'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:184:in `create_current'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/railties/databases.rake:39:in `block (2 levels) in <main>'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.4/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.4/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.4/lib/rails/command.rb:48:in `invoke'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.4/lib/rails/commands.rb:18:in `<main>'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `block in require'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:291:in `load_dependency'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `require'
/Users/user/Desktop/docker-forrails/rails_test/bin/rails:9:in `<top (required)>'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `load'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `call'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/spring-2.1.1/lib/spring/client/command.rb:7:in `call'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/spring-2.1.1/lib/spring/client.rb:30:in `run'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/spring-2.1.1/bin/spring:49:in `<top (required)>'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `load'
/Users/user/Desktop/docker-forrails/rails_test/vendor/bundle/ruby/2.6.0/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `<top (required)>'
/Users/user/Desktop/docker-forrails/rails_test/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:create
(See full trace by running task with --trace)

Postgres status

PostgreSQL Database directory appears to contain a database; Skipping initialization
2021-01-17 08:06:27.801 UTC [1] LOG: starting PostgreSQL 13.1 (Debian 13.1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2021-01-17 08:06:27.802 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2021-01-17 08:06:27.802 UTC [1] LOG: listening on IPv6 address "::", port 5432
2021-01-17 08:06:27.804 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2021-01-17 08:06:27.809 UTC [27] LOG: database system was shut down at 2021-01-17 08:06:26 UTC
2021-01-17 08:06:27.814 UTC [1] LOG: database system is ready to accept connections

docker ps

CONTAINER ID   IMAGE            COMMAND                  CREATED          STATUS          PORTS                        NAMES
e337b5d7aaef   postgres         "docker-entrypoint.s…"   39 minutes ago   Up 39 minutes   0.0.0.0:5432->5432/tcp       rails_test_postgres_1
0d6674cb3430   nginx            "/docker-entrypoint.…"   45 minutes ago   Up 45 minutes   80/tcp, 0.0.0.0:90->90/tcp   rails_test_nginx_1
69d4f22351d9   rails_test_app   "bundle exec rails s…"   45 minutes ago   Up 45 minutes   0.0.0.0:4004->3000/tcp       rails_test_app_1

docker-compose.yml

version: '3'
services:
nginx:
    tty: true
    image: nginx
    depends_on:
    - app
    volumes_from:
    - app
    volumes:
    - nginx.conf
    ports:
    - "90:90"

app:    
    tty: true
    build: .
    entrypoint: bundle exec
    command: rails server -p 3000
    volumes:
    - .:/app
    ports:
    - "4004:3000"
    depends_on: 
    - postgres

postgres:
    tty: true
    env_file: ./.env
    image: postgres
    environment:
    - POSTGRES_PASSWORD=${DB_PASSWORD}
    ports:
    - "5432:5432"
    volumes: 
    - /var/lib/postgresql/data

Dockerfile

# From one of the official ruby images
FROM ruby:2.6.3-alpine
ARG APP_PATH='/app'
ARG PORT=3000
ENV RAILS_ENV='production'
ENV RAKE_ENV='production'

RUN apk --update add --no-cache \
    build-base \
    tzdata \
    git \
    postgresql \
    postgresql-dev \
    nodejs \
    imagemagick

RUN mkdir -p $APP_PATH
WORKDIR $APP_PATH

RUN gem install bundler
COPY Gemfile ./
COPY Gemfile.lock ./
RUN bundle install --jobs 20 --retry 5 --without development test

ENTRYPOINT ["entrypoint.sh"]
EXPOSE $PORT
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]

Nginx.conf

upstream puma {
    server app:3000;
}

server {

    root /app/public;

    access_log /app/log/nginx.access.log;
    error_log /app/log/nginx.error.log;

    location ~ /\. {
    deny all;
    }

location ~* ^.+\.(rb|log)$ {
    deny all;
}

    location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
    try_files $uri @rails;

    access_log off;
    gzip_static on; # to serve pre-gzipped version

    expires max;
    add_header Cache-Control public;

    add_header Last-Modified "";
    add_header ETag "";
    break;
    }

    location / {
    try_files $uri @rails;
    }

    location @rails {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://puma;
    }
}

database.yml

default: &default
adapter: postgresql
encoding: unicode
database: test
pool: 5
username: username
password: password
port: 5432

development:
&lt&lt: *default
database: test_dev

production:
&lt&lt: *default
database: test_pro

test:
&lt&lt: *default
database: test_tes
1
Your database.yml is missing host: postgresThanh
I added host and migrated.Then error occurred, "could not translate host name "postgres" to address: nodename nor servname provided, or not known Couldn't create 'db' database. Please check your configuration." But I could fix & migrate. Thanks.ume
how do you fix it? so can I add this to answer?Thanh
I added host as "localhost" in database.yml. I could migrate so you can add answer.ume

1 Answers

0
votes

database.yml file is missing a host: postgres config so that app service can know which database to run migration.