postgis / docker-postgis

Docker image for PostGIS
https://hub.docker.com/r/postgis/postgis/
MIT License
1.35k stars 462 forks source link

Docker-compose example #320

Open latot opened 1 year ago

latot commented 1 year ago

Hi all, I don't know why, but run postgis with docker run is so easy, but with docker compose so hard u.u

This works great:

docker run -d -p 5432:5432 -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -e POSTGRES_DB=db postgis/postgis

But the next one fails!

  postgis:
    image: postgis/postgis
    volumes:
      - ./postgis/etc:/etc
    environment:
      # If you need to create multiple database you can add coma separated databases eg gis,data
      - POSTGRES_DB=db
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5432:5432"
    restart: on-failure

with:

docker logs ca84d57458df
find: ‘postgres’ is not the name of a known user
find: ‘postgres’ is not the name of a known user
find: ‘postgres’ is not the name of a known user
find: ‘postgres’ is not the name of a known user
find: ‘postgres’ is not the name of a known user
find: ‘postgres’ is not the name of a known user
find: ‘postgres’ is not the name of a known user

I really don't get why one works and the other not, maybe there is a tricky thing about docker-compose.

Well, there is the chance, this problem is in postgres image and not postgis, but lets start :)

Thx!

toficzak commented 1 year ago

Hey.

I am no expert at postgis/postgres/docker/docker-compose, but I got itchy reading your problem.

The only difference between your docker command and docker-compose file is volume.

When I commented it out:

postgis:
  image: postgis/postgis
  #volumes:
  #  - './postgis/etc:/etc'
  environment:
    - POSTGRES_DB=${POSTGRES_DB}
    - POSTGRES_USER=${POSTGRES_USER}
    - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
  ports:
    - '5432:5432'
#  restart: on-failure

Container starts normally. To ensure I am not working on any cached/volumed version, I recreated container each time with:

docker-compose down; sudo rm -rf postgis; docker-compose up --force-recreate

I googled a bit and found out you might just want to create named volume of postgres' data directory. Such docker-compose.yml works as well.

postgis:
  image: postgis/postgis
  volumes:
    - db-data:/var/lib/postgresql/data
  environment:
    - POSTGRES_DB=db
    - POSTGRES_USER=postgres
    - POSTGRES_PASSWORD=postgres
  ports:
    - '5432:5432'
#  restart: on-failure

I am still not sure what is the original root of problem, whether setting volume in current directory or setting it to whole /etc of container, but the result is container did not have env variables passed on.

I don't have more time right now, but it's interesting for me, maybe I will dig deeper later on :)

If you actually found out what's wrong, please answer, I would appreciate it.

latot commented 1 year ago

D: Still can't do it works!

Without volume works, with that folder works, but with etc end in..

If the folder is detected as valid:

Starting docker_postgis_1 ... done
Attaching to docker_postgis_1
postgis_1  | find: ‘postgres’ is not the name of a known user
docker_postgis_1 exited with code 1

If docker says its no valid

ERROR: Named volume "postgis/etc:/etc:rw" is used in service "postgis" but no declaration was found in the volumes section.

I have tested every solution here:

https://github.com/ClusterHQ/dvol/issues/67 https://stackoverflow.com/questions/71762103/docker-compose-no-declaration-was-found-in-the-volumes-section

toficzak commented 1 year ago

Hey @latot, I actually encountered the same problem with my other project, so I dove a little deeper.

There are two types of mounting filesystem to docker container, namely:

Now, bind mounts have following syntax:

And volumes:

Now we thought we were using volumes, but we were using bind mounts:

But when I provided alternative solution:

Finally, this is docker-compose.yml with named volume:

version: '3.9'
services:
  postgis:
    image: postgis/postgis
    volumes:
      - postgis_etc:/etc
    environment:
      # If you need to create multiple database you can add coma separated databases eg gis,data
      - POSTGRES_DB=db
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5432:5432"
    restart: on-failure

volumes:
   postgis_etc:

And running this creates named volume:

[~]$ docker volume ls
DRIVER    VOLUME NAME
local     ce8c2f7a50b28dfd2df4f1c94c0bd178cb69ba28e73c1a2b95734c870efedf9e
local     postgis_etc
[~]$ docker inspect postgis_etc
[
    {
        "CreatedAt": "2023-03-25T12:39:18+01:00",
        "Driver": "local",
        "Labels": {
            "com.docker.compose.project": "",
            "com.docker.compose.version": "2.16.0",
            "com.docker.compose.volume": "postgis_etc"
        },
        "Mountpoint": "/var/lib/docker/volumes/postgis_etc/_data",
        "Name": "postgis_etc",
        "Options": null,
        "Scope": "local"
    }
]
[~]$ sudo ls -l /var/lib/docker/volumes/postgis_etc/_data
total 376
-rw-r--r-- 1 root root    2981 Feb 27 01:00 adduser.conf
drwxr-xr-x 2 root root   12288 Mar 25 12:39 alternatives
drwxr-xr-x 7 root root    4096 Mar 25 12:39 apt
(...)

So that solves it I guess :)