简体   繁体   English

无法从 docker-compose 启动 postgres docker 容器

[英]Unable to start postgres docker container from docker-compose

I am trying to start a postgresql docker container which is of version 10.5 .我正在尝试启动版本为10.5postgresql docker 容器。

But before that I have used 9.6 version in the same docker-compose.yml file and there is no data populated in the database.但在此之前,我在同一个 docker-compose.yml 文件中使用了9.6版本,并且数据库中没有填充任何数据。

And now after changing the version of postgres container , I'm not able to run the docker-compose up .现在更改postgres 容器的版本后,我无法运行docker-compose It is throwing the below error.它抛出以下错误。

FATAL: database files are incompatible with server FATAL:数据库文件与服务器不兼容

DETAIL: The data directory was initialized by PostgreSQL version 9.6, which is not compatible with this version 10.5 (Debian 10.5-2.pgdg90+1)详细信息:数据目录由 PostgreSQL 版本 9.6 初始化,与此版本 10.5 (Debian 10.5-2.pgdg90+1) 不兼容

This is how the docker-compose.yml file looks like.这就是 docker-compose.yml 文件的样子。

version: '2'

services:

  postgres_service:
   container_name: postgresql_container
   restart: always
   image: postgres:10.5
   volumes:
     - postgres-data:/var/lib/postgresql/data
     - ./postgresql/init:/docker-entrypoint-initdb.d
   ports:
     - "5432:5432"
   environment:
     - POSTGRES_USER=admin
     - POSTGRES_PASSWORD=password
volumes:
  postgres-data:
    driver: local

Can someone please let me know where the issue is.有人可以让我知道问题出在哪里吗? Where am I making mistake?我在哪里犯错? Do I need to delete any volumes before proceeding with the new postgres version?在继续使用新的 postgres 版本之前,我是否需要删除任何卷?

I also have postgresql installed in my local.我的本地也安装了 postgresql。

postgres=# select version();
                                                               version                                                               
-------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 10.10 (Ubuntu 10.10-1.pgdg18.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0, 64-bit
(1 row)

Will this cause any issue?这会导致任何问题吗?

The problem caused because the volume which your compose created to store your database still keep old data which initiated by PostgreSQL 9.6 .导致问题的原因是您为存储数据库创建的卷仍然保留由PostgreSQL 9.6启动的旧数据。 That volume name is postgres-data which created when you use named volume on your docker-compose.yml .该卷名是您在docker-compose.yml上使用命名卷时创建的postgres-data So simply to get rid of this, you can use some ways below:因此,为了摆脱这种情况,您可以使用以下一些方法:

  1. Using docker-compose command:使用docker-compose命令:

Run docker-compose down -v , this will stop all your container inside that compose and remove all named volume inside that compose.运行docker-compose down -v ,这将停止该组合内的所有容器并删除该组合内的所有命名卷。

You could take a look at docker-compose down command你可以看看docker-compose down 命令

  1. Using docker volume command:使用docker volume命令:

Run docker volume ls to get list of current volumes on your machine, I think you will see your volume on that list too:运行docker volume ls以获取您机器上当前卷的列表,我想您也会在该列表中看到您的卷:

DRIVER              VOLUME NAME
local               postgres-data

Run docker volume rm postgres-data to remove that volume, if your container still running and you couldn't remove it then you can use -f to force remove it运行docker volume rm postgres-data以删除该卷,如果您的容器仍在运行并且您无法删除它,那么您可以使用-f强制删除它

Hope that helps!希望有帮助!

What worked for me was deleting pgdata folder inside the root of my project and running docker-compose build -d .对我有用的是删除项目根目录中的pgdata文件夹并运行docker-compose build -d It then showed me然后它告诉我

/usr/local/bin/docker-entrypoint.sh: /docker-entrypoint-initdb.d/create-multiple-postgres-databases.sh: /bin/bash: bad interpreter: Permission denied

To fix it, I ran为了修复它,我跑了

chmod +x pg-init-scripts/create-multiple-postgresql-databases.sh

Notice that the.sh file name should match the one you have.请注意,.sh 文件名应该与您拥有的文件名相匹配。 And finally, docker-compose up -d .最后, docker-compose up -d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM