0

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.

enter image description here

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

1 Answer 1

1

If you're comfortable with distributing a tar file as the basis for this, you can include a working Docker Compose setup and its data. The docker-compose.yml file can be a very typical Compose setup:

version: '3.8'
services:
  postgres:
    image: postgres:12
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    # ports: ['5432:5432']
  app: { ... }

The volumes: block tells Docker to mount the local pgdata directory into the container on the PostgreSQL data path. The host content will replace that directory in the image, so you can distribute a preloaded data directory. Conversely, when the database writes back to the image, it will overwrite the host directory content and those changes will be persisted for future runs (but you can always delete it and start over from the tar file).

tar cf myapp.tar \
  myapp/docker-compose.yml \
  myapp/pgdata

This doesn't redistribute the postgres image per se, it just reuses the standard Docker Hub image. Unless you're in an isolated network environment this shouldn't be a practical problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.