0

Still getting my docker sea legs, I am trying to create a container for postgres 14 with alpine linux.

this is my Dockerfile so far:

FROM alpine:3.15.5
EXPOSE 5432

# update repo, install postgres 14
RUN apk update
RUN apk add gcc make
RUN apk add postgresql14 postgis

# data dir
RUN mkdir /var/lib/postgresql/data
RUN chmod 0700 /var/lib/postgresql/data
RUN chown postgres:postgres /var/lib/postgresql/data
VOLUME /var/lib/postgresql/data

# create db cluster as postgres user
USER postgres:postgres
RUN initdb -D /var/lib/postgresql/data

# temp
ENTRYPOINT [ "top" ]

The issue I am running into is when I build and run (docker-compose up --build), the initdb command runs perfectly, no errors, and the output makes sense, however there is no data in the /var/lib/postgresql/data dir which should have all the default postgres configs and db files.

The weird thing is, if I attach the container shell while it is running, and run initdb -D /var/lib/postgresql/data it works...

Please can you tell me what I am doing wrong/missing

Here is the docker-compose.yml as well for total coverage:

version: '3.9'

services:
  postgres:
    build: .
    ports:
      - "5432:5432"
    volumes:
      - ./postgres:/var/lib/postgresql/data
Callum
  • 1
  • 2
  • This happens because you are overriding the content of it with your mount: `- ./postgres:/var/lib/postgresql/data`. I have explained the `docker-compose up` process and its implication with mounts in this answer, if this is of any interest for you: https://stackoverflow.com/questions/43769756/composer-install-doesnt-install-packages-when-running-in-dockerfile/46458246#46458246. – β.εηοιτ.βε Aug 05 '22 at 07:05
  • This said, I hope, this trial is to make your hand on docker because there is already a postgres alpine image: https://hub.docker.com/_/postgres/?tab=tags&page=1&name=alpine – β.εηοιτ.βε Aug 05 '22 at 07:27
  • And so, If you want more insight, just look at what they are doing in their _Dockerfile_ to overcome this: https://github.com/docker-library/postgres/tree/2f6878ca854713264ebb27c1ba8530c884bcbca5/14/alpine. Hint: it's all about the entrypoint. – β.εηοιτ.βε Aug 05 '22 at 07:29
  • Thanks a stack @β.εηοιτ.βε you were right I the volume was clearing it. Going to have a look at the postgres link you shared, thank you again :) – Callum Aug 06 '22 at 04:51

1 Answers1

0

as @β.εηοιτ.βε has pointed out:

This happens because you are overriding the content of it with your mount: - ./postgres:/var/lib/postgresql/data. I have explained the docker-compose up process and its implication with mounts in this answer.

treckstar
  • 1,956
  • 5
  • 21
  • 26
Callum
  • 1
  • 2