2016-09-06 112 views
2

當我在自己的機器上使用Docker時,我想訪問我的系統的Postgres。這是將我的圖像部署到AWS的地方,我想使用RDS。如何從Docker訪問我的本地系統Postgres

我遵循Docker's Quickstart: Docker Compose and Rails page的步驟,除了因爲我正在使用現有的應用程序,我沒有創建剛加載Rails的初始最小Gemfile。

泊塢窗容器外,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-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# 

我能做些什麼?

的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:clonerake db:test:load或類似的其他實用程序。

更新II

添加以下到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的背景下討論,這在這裏非常重要。

如果我將我的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' 

回答

2

這不是「揭掉上來理解錯了」,這是非常棘手。我穿過碼頭介紹你鏈接,我不能得到它的工作。

這是我爲我們的應用程序所做的。我建立了我們的應用程序關閉應用程序後命名的基礎圖像。基礎圖像以應用程序命名。所以使用你的例子,它會被稱爲myapp_base。我沒有在這裏看到你的docker-compose.yml文件,所以我會做一些假設。

web: image: your_company/myapp_base links: - db volumes: - .:/myapp # ports: # - "3002:3002" # env_file: # - .myapp.env

港和env_file沒有必要的,但後來他們可能會派上用場。 Myapp_base內置了一個奇怪的工作流程,可以拉動Deps,但如果應用程序聲明爲需要postgres,則基本上會拋出postgres-client。這是廚師,但基本上是myapp runlist needs postgresql。我們實際上正在擺脫這種情況,並試圖將設置的東西放入腳本和Dockerfile本身。

所以...基本上只需將apt-get install postgres-client添加到您的Dockerfile中,將其標記爲myapp_base並在您的docker-compose.yml文件中使用該文件。然後運行docker-compose run web rake db東西,psql將被找到。

+1

謝謝。原則上這看起來應該起作用。 (但是,Docker文檔也是如此。)但最終我最終沒有使用Docker部署到ElasticBean。 –

相關問題