Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach a PostgreSQL volume to a Docker image generated with SBT native packager?

I would like to be able to deploy my app in a pre-prod environment for integration testing using a Docker volume that will expose an instance of PostgreSQL. I'm using Scala v2.12.8 and Play v2.7.

Looking at the environment settings of the SBT native packager it seems possible to define dockerExposedVolumes in order to attach a DB.

Using a normal Docker compose file I would do something like that:

   version: "3"
   services:
     db:
       image: postgres
       environment:
         - POSTGRES_USER=postgres
         - POSTGRES_PASSWORD=postgress
         - POSTGRES_DB=postgres
       ports:
         - "5433:5432"
       volumes:
         - pgdata:/var/lib/postgresql/data
       networks:
         - suruse
   volumes: 
     pgdata:

This configuration has been taken from this SO answer.

I tried searching for config examples but I didn't find anything useful so far. Now I'm wondering how I should define a new docker volume and then expose it to the Docker image created by SBT exactly?


THE WORKING SOLUTION

The final version. I've fully tested it and it works exposing the DB on the TCP port 5433.

# https://docs.docker.com/samples/library/postgres/
version: "3"
services:
  app-pgsql:
    image: postgres:9.6
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=yourPasswordHere
      - POSTGRES_DB=yourDatabaseNameHere
      - POSTGRES_INITDB_ARGS="--encoding=UTF8"
    ports:
      - "5433:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data
volumes: 
  pgdata:
    driver: local

Launch the docker compose using sbt dockerComposeUp -useStaticPorts and then check if the containers have been actually exposed using docker ps -a. Also, check the log files using the command provided by dockerComposeUp or dockerComposeInstances.

like image 474
sentenza Avatar asked Jan 24 '26 10:01

sentenza


1 Answers

There is a sbt Plugin that helps you to achieve this:

sbt-docker-compose

With that you can add your database to a docker compose file and you can run everything within sbt.

This is a Docker standard. Here is an explaination how to do it for Postgres:

[run_postgresql_docker_compose][2]

The docker-compose.yml from that example:

version: '3'
services:
mydb:
image: postgres
volumes:
- db-data:/var/lib/postgresql/data
ports:
         - 5432:5432/tc

volumes:
db-data:
driver: local

As this is a standard way of Docker you will find more examples.

like image 129
pme Avatar answered Jan 27 '26 01:01

pme



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!