When I use Docker on my own machine, I want to access my system's Postgres. This is prior to deploying my images to AWS, where I want to use RDS.
I have followed the steps on Docker's Quickstart: Docker Compose and Rails page with the exception that, because I was working with an existing application, I didn't create the initial minimal Gemfile that just loads Rails.
Outside of the Docker container, Postgres is installed and working correctly:
$ which postgres
/Applications/Postgres.app/Contents/Versions/latest/bin/postgres
$ which pg_restore
/Applications/Postgres.app/Contents/Versions/latest/bin/pg_restore
$ which pg_dump
/Applications/Postgres.app/Contents/Versions/latest/bin/pg_dump
The app has been working fine without Docker for years. To test the app outside of the Docker container once more, I have comment out one line of Docker's recommended docker-compose.yml file as follows:
development: &default
adapter: postgresql
encoding: unicode
database: postgres
pool: 5
username: postgres
password:
# host: db
test:
<<: *default
database: myapp_test
When I do that, I can run rake db:test:clone (for example) outside of the container no worries:
But when I uncomment that line and connect to a TTY in the container, I'm in all sorts of pain:
/myapp# rake db:test:clone
pg_dump -s -x -O -f /myapp/db/structure.sql postgres
Please check the output above for any errors and make sure that `pg_dump` is installed in your PATH and has proper permissions.
/myapp# which postgres
/myapp# which pg_restore
/myapp# which pg_dump
/myapp#
What can I do?
The Dockerfile, based on Docker's instructions with a few extra binaries required by my existing app, is:
FROM ruby:2.3.1-slim
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
RUN apt-get update
RUN apt-get install -qq -y --no-install-recommends --fix-missing apt-utils
RUN apt-get install -qq -y --no-install-recommends --fix-missing build-essential
RUN apt-get install -qq -y --no-install-recommends --fix-missing git
RUN apt-get install -qq -y --no-install-recommends --fix-missing xvfb
RUN apt-get install -qq -y --no-install-recommends --fix-missing qt5-default
RUN apt-get install -qq -y --no-install-recommends --fix-missing libqt5webkit5-dev
RUN apt-get install -qq -y --no-install-recommends --fix-missing gstreamer1.0-plugins-base
RUN apt-get install -qq -y --no-install-recommends --fix-missing gstreamer1.0-tools
RUN apt-get install -qq -y --no-install-recommends --fix-missing gstreamer1.0-x
RUN apt-get install -qq -y --no-install-recommends --fix-missing nodejs
RUN apt-get install -qq -y --no-install-recommends --fix-missing libpq-dev
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
ADD . /myapp
Related: I can see redis-cli outside the container but not inside it. This will be an issue too once I solve the Postgres situation.
Update
When I go in like this...
docker-compose run web rake db:create
docker-compose run web rake db:test:clone
...it seems I can access Postgres, but not pg_dump and other utilities required for rake db:test:clone, rake db:test:load or similar.
Update II
I added the following to the Dockerfile:
RUN apt-get install -qq -y --no-install-recommends --fix-missing postgresql-client
Now, instead of giving me a "can't be found" error, it says:
pg_dump: server version: 9.5.4; pg_dump version: 9.4.9
pg_dump: aborting because of server version mismatch
This is an issue you can find discussed elsewhere, but not in the context of Docker, which is crucial here.
Would it help if I downgraded my Mac's Postgres to the version fetched by apt-get? Upgrading the version installed by apt-get appears not to be an option:
Step 15 : RUN apt-get install -qq -y --no-install-recommends --fix-missing postgresql-client-9.5
---> Running in 614b88701710
E: Unable to locate package postgresql-client-9.5
E: Couldn't find any package by regex 'postgresql-client-9.5'