简体   繁体   English

在 docker compose 中重命名卷,docker 派生自 postgres

[英]Rename volume in docker compose, with docker derived from postgres

I have this docker-compose我有这个 docker-compose

 version: '3.7' services: app-db: build: context: ./ dockerfile: Dockerfile-pg image: app-pg:1.0.0 restart: always environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_DB: app volumes: - ./docker-entrypoint-initdb.d/init-user-db.sh:/docker-entrypoint-initdb.d/init-user-db.sh - v-app-pgdata:/var/lib/postgresql - v-app-pglog:/data/var/log/postgresql - v-app-pgconf:/etc/postgresql app-main: build: context: ./ dockerfile: Dockerfile-tar-cp image: app-main:1.0.0 restart: always ports: - 80:80 volumes: v-app-pgdata: name: v-app-pgdata v-app-pglog: name: v-app-pglog v-app-pgconf: name: v-app-pgconf

so an app container and a postgres derived container:所以一个应用程序容器和一个 postgres 派生容器:

 #docker build -t app-pg:1.0.0 -f Dockerfile-pg . #docker run -d --name appC-pg -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres app-pg:1.0.0 FROM postgres:12.1 MAINTAINER xxx #ARG A_DB_USER='postgres' #ARG A_DB_PASS='postgres' ARG A_DB_NAME='app' ARG A_TZ='Europe/Zurich' #ENV DB_USER=${A_DB_USER} #ENV DB_PASS=${A_DB_PASS} ENV DB_NAME=${A_DB_NAME} ENV TZ=${A_TZ} # Adjusting Timezone in the system RUN echo $TZ > /etc/timezone && \\ apt-get update && apt-get install -y tzdata && \\ rm /etc/localtime && \\ ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \\ dpkg-reconfigure -f noninteractive tzdata && \\ apt-get clean # install postgis RUN apt-get update && \\ apt-get install -y postgis && \\ apt-get clean USER postgres #Add password "postgres" to user postgres, create db, add .sql #RUN /etc/init.d/postgresql start && \\ # psql --command "ALTER USER ${DB_USER} WITH PASSWORD '${DB_PASS}'; SET TIME ZONE '${TZ}';" && \\ # createdb -O ${DB_USER} ${DB_NAME} -E UTF8 && \\ # psql -d ${DB_NAME} -c 'CREATE EXTENSION postgis' EXPOSE 5432

My problem is that postgres default dockerfile have this line:我的问题是 postgres 默认的 dockerfile 有这一行:

VOLUME /var/lib/postgresql/data

So even if I create a named Volumes with the same folder, my docker-compose create 4 and not 3 volumes, one unnamed due to that line.因此,即使我使用相同的文件夹创建了一个命名卷,我的 docker-compose 也会创建 4 个而不是 3 个卷,由于该行而未命名。 How is it possible solve this issue?怎么可能解决这个问题?

I had the same problem as you and I solved it by editing the docker-compose.yml the following way:我和你有同样的问题,我通过以下方式编辑docker-compose.yml解决了它:

  1. Create a volume for the data为数据创建卷
volumes:
    v-app-pgdata:
  1. In your declaration of the database service, in the volumes clause, you need to change - v-app-pgdata:/var/lib/postgresql to:在您声明的数据库服务中,在volumes 子句中,您需要将- v-app-pgdata:/var/lib/postgresql更改为:
- v-app-pgdata:/var/lib/postgresql/data

The problem was that you where not mounting the volume to the correct place, and therefore, it had to create another volume that is declared in the base image VOLUME /var/lib/postgresql/data .问题是您没有将卷安装到正确的位置,因此,它必须创建另一个在基本映像VOLUME /var/lib/postgresql/data If you run this container without attaching a volume to this mount point, it will automatically create it, and with a random name.如果您在没有将卷附加到此挂载点的情况下运行此容器,它将自动创建它,并使用随机名称。

However, it has been pointed out to me that if the OP strictly needs a volume in /var/lib/postgresql then this solution wont work.但是,有人向我指出,如果 OP 严格需要/var/lib/postgresql的卷,则此解决方案将不起作用。

Hope this helps, as it worked for me.希望这会有所帮助,因为它对我有用。

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

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