在Docker中,pg_dump服务器和pg_dump版本不匹配

6
当我在railsApp容器中运行命令psql --version时,我得到的版本号是9.4.12,而当我在postgres容器中运行相同的命令时,我得到的版本号是9.6.2。如何使这两个版本号匹配?
在Rails App上执行pg_dump sql导入时,我遇到了以下错误。
pg_dump: server version: 9.6.2; pg_dump version: 9.4.12
pg_dump: aborting because of server version mismatch
rails aborted!

以下是我的Docker-compose.yml文件:
version: "2.1"
services:
  railsApp:
    build:
      context: ./
    ports:
      - "3000:3000"
    links:
      - postgres
    volumes:
      - .:/app
  postgres:
    image: postgres:9.6
    ports:
      - "5432:5432"
    volumes:
      - ./.postgres:/var/lib/postgresql

Dockerfile文件:
FROM ruby:2.3.3

# setup /app as our working directory
RUN mkdir /app
WORKDIR /app

# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

# Set debconf to run non-interactively
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections

# Install base dependencies

FROM ruby:2.3.3

# setup /app as our working directory
RUN mkdir /app
WORKDIR /app

# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

# Set debconf to run non-interactively
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections

# Install base dependencies
RUN apt-get update && apt-get install -y -q --no-install-recommends \
        apt-transport-https \
        build-essential \
        ca-certificates \
        curl \
        git \
        libssl-dev \
        python \
        rsync \
        software-properties-common \
        wget \
        postgresql-client \
        graphicsmagick \
    && rm -rf /var/lib/apt/lists/*

# Install node and npm with nvm
RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash

ENV NVM_DIR=/root/.nvm
ENV NODE_VERSION v7.2.1

ENV NODE_PATH $NVM_DIR/versions/node/$NODE_VERSION
ENV PATH      $NODE_PATH/bin:./node_modules/.bin:$PATH


RUN source $NVM_DIR/nvm.sh \
    && nvm install $NODE_VERSION \
    && nvm alias default $NODE_VERSION \
    && nvm use default

# Install our ruby dependencies
ADD Gemfile Gemfile.lock /app/
RUN bundle install

# copy the rest of our code over
ADD . /app

ENV RAILS_ENV development
ENV SECRET_KEY_BASE a6bdc5f788624f00b68ff82456d94bf81bb50c2e114b2be19af2e6a9b76f9307b11d05af4093395b0471c4141b3cd638356f888e90080f8ae60710f992beba8f

# Expose port 3000 to the Docker host, so we can access it from the outside.
EXPOSE 3000

# Set the default command to run our server on port 3000
CMD ["rails", "server", "-p", "3000", "-b", "0.0.0.0"]
2个回答

2

我也遇到了同样的问题,我使用了另一种方法来进行转储。

首先,我访问终端并在docker中运行pd_dump,然后将文件从docker复制到主机。以下是命令:

docker exec -it <container-id> /bin/bash # accessing docker terminal

pg_dump > ~/dump # taking dump inside the docker

docker cp <container-id>:/root/dump ~/dump #copying the dump files to host

希望上述解决方案能够帮到您。

在使用docker exec命令后,我的目录变成了/而不是/root。非常好用,谢谢! - James Bridgewater

1
最简单的方法是在docker-compose中使用正确的postgres版本。更改为:
  postgres:
    image: postgres:9.6

致:

  postgres:
    image: postgres:9.4.2

所有可用版本在这里


1
我遇到了这个错误:详细信息:数据目录是由PostgreSQL 9.6版本初始化的,与此版本9.4.2不兼容。 - Suthan Bala
我删除了.postgres文件夹,但是当我运行docker-compose up postgres时,它会再次创建并抛出相同的错误。 - Suthan Bala
请您再仔细检查一下?这不应该是同样的错误。 - Robert
对我来说也是同样的问题,我使用了另一种方式来进行转储。 - Arun Wilson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接