USE-CASE: The use case is that you have say a small 'example' database that you want to provide to a user to practice/test apps with. The user doesn't have postgres installation but they do have Docker. You want to provide the user with a painless way to access your tailored db without them having to install postgres. You can provide users with exactly the same db and easily update it when required
PROPOSED SOLUTION: I would like to bundle a postgres db and postgres docker image so that I could give a user say a tar.gz and (assuming they have Docker) they could unpack the tar.gz and have a running postgres db that they could connect to.
I have at least two challenges:
- From my searching so far it looks like I can tailor the postgres Docker image and that I can apparently define a data path for the postgres Docker image. What I haven't found is how to do that and whether I can specify the path as a directory in the unpacked tar.gz.
- After the bundle is unpacked, the User runs the postgres Docker container, they connect to the dB and say make some changes. Will those changes persist after the container is closed?
Question: Does this make sense? If so, am I going about it the right way or am I missing something?
POSTMORTEM:
In the end and building off @davidmaze reply, the following was successful.
version: "3"
services:
postgres:
image: postgres:12
container_name: testdb_container
volumes:
- ./pgdata:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- 5555:5432
volumes:
pgdata:
With the directory structure looking like this.
I haven't been able to successfully name the db anything else but 'postgres' but that's okay.
I tar.gz the 'test-comp' directory after I've created the db and populated it. The recipient unpacks it and runs 'start.sh' which is just a start message and 'docker-compose up -d'. The db is shut down with 'stop.sh'.
The 'init.sql' is set to produce an empty dB with a preset schema. I deliver a populated dB but if the person wants to start fresh then they can just empty the directory (best to delete and create a new empty one) and running 'start.sh' will invoke 'init.sql'.
