Davide
Davide

Reputation: 95

Rename volume in docker compose, with docker derived from postgres

I have this 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:

#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:

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. How is it possible solve this issue?

Upvotes: 0

Views: 2351

Answers (1)

Aldo Perez
Aldo Perez

Reputation: 21

I had the same problem as you and I solved it by editing the docker-compose.yml the following way:

  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:
- 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. 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.

Hope this helps, as it worked for me.

Upvotes: 1

Related Questions