docker-postgis
docker-postgis copied to clipboard
Docker-compose example
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!
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.
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
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:
- bind mounts - these are old and limited, they essentially mount path from host file system to container file system. In other words, they do not create new directory that can be used by both host and container, they force content of the host directory at path provided in container
- volumes - these are what we both thought of - basic directory reusable by host and container
Now, bind mounts have following syntax:
- path/on/host:/path/in/container
And volumes:
- [optional_name][:]path/in/container
Now we thought we were using volumes, but we were using bind mounts:
- ./postgis/etc:/etc - this is definition of bind mount (path from host), therefore we put host directory in place of container's /etc directory. Because on host this directory was empty - we replaced in container directory /etc with empty directory, making it impossible to work correctly.
But when I provided alternative solution:
- db-data:/var/lib/postgresql/data <-- this is named volume, that's why it works
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 :)