当我在自己的机器上使用 Docker 时,我想访问我系统的 Postgres。这是在将我的镜像部署到 AWS 之前进行的,我想在 AWS 中使用 RDS。
我已按照Docker's Quickstart: Docker Compose and Rails page上的步骤进行操作异常(exception)的是,因为我正在使用现有的应用程序,所以我没有创建仅加载 Rails 的初始最小 Gemfile。
在 Docker 容器之外,Postgres 已安装并正常工作:
$ 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
多年来,该应用程序在没有 Docker 的情况下运行良好。为了再次在 Docker 容器外部测试该应用程序,我注释掉了 Docker 推荐的 docker-compose.yml 文件中的一行,如下所示:
development: &default
adapter: postgresql
encoding: unicode
database: postgres
pool: 5
username: postgres
password:
# host: db
test:
<<: *default
database: myapp_test
当我这样做时,我可以在容器外部运行rake db:test:clone
(例如),不用担心:
但是当我取消注释该行并连接到容器中的 TTY 时,我感到各种痛苦:
/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#
我能做什么?
基于 Docker 的指令以及我现有应用程序所需的一些额外二进制文件,Dockerfile 是:
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
相关:我可以在容器外部看到 redis-cli
,但在容器内部看不到。一旦我解决了 Postgres 的情况,这也将是一个问题。
更新
当我这样进去的时候...
docker-compose run web rake db:create
docker-compose run web rake db:test:clone
...看来我可以访问 Postgres,但不能访问 pg_dump
以及 rake db:test:clone
、rake db:test: 所需的其他实用程序加载
或类似的。
更新二
我将以下内容添加到 Dockerfile 中:
RUN apt-get install -qq -y --no-install-recommends --fix-missing postgresql-client
现在,它不再给我“找不到”错误,而是说:
pg_dump: server version: 9.5.4; pg_dump version: 9.4.9
pg_dump: aborting because of server version mismatch
这个问题可以在其他地方找到讨论,但不能在 Docker 的上下文中讨论,而 Docker 在这里至关重要。
如果我将 Mac 的 Postgres 降级到 apt-get
获取的版本会有帮助吗?升级由 apt-get
安装的版本似乎不是一个选项:
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'
最佳答案
这不是“你做错了”,这很棘手。我浏览了您链接的 docker 介绍,但无法让它工作。
这是我为我们的应用程序所做的事情。我根据以应用程序命名的基础镜像构建了我们的应用程序。基础镜像以应用程序命名。因此,使用您的示例,它将被称为 myapp_base
。我在这里没有看到您的 docker-compose.yml 文件,因此我将做出一些假设。
web:
image: your_company/myapp_base
links:
- db
volumes:
- .:/myapp
# ports:
# - "3002:3002"
# env_file:
# - .myapp.env
Port 和 env_file 不是必需的,但它们稍后可能会派上用场。 Myapp_base
是用一个奇怪的工作流程构建的,它引入了 deps,但基本上抛出了 postgres-client
如果应用程序被声明需要 postgres。它在厨师中,但基本上是 myapp runlist needs postgresql
。我们实际上正在摆脱这种情况,并尝试将设置内容放入脚本和 Dockerfile 本身中。
所以...基本上只需添加 apt-get install postgres-client
到您的 Dockerfile,将其标记为 myapp_base
并在您的 docker-compose.yml
中使用它文件。然后运行docker-compose run web rake db
将会找到东西和 psql。
关于ruby-on-rails - 如何从 Docker 访问我的本地系统 Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39342369/