3

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.

sentenza
  • 1,608
  • 3
  • 29
  • 51
  • Why not use postgres in its own docker image? – pme Jul 02 '19 at 08:46
  • I would like to create and attach a DB image using `sbt docker:publishLocal`. Is that doable? Otherwise, could you please point me in the right direction? Should I define a docker compose file with a PostgreSQL image in it? – sentenza Jul 02 '19 at 08:50

1 Answers1

1

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.

pme
  • 14,156
  • 3
  • 52
  • 95
  • I suppose I might start from [this example](https://github.com/Tapad/sbt-docker-compose/tree/master/examples/basic-native-packager). Could you please add an example of what this compose file should look like? – sentenza Jul 02 '19 at 09:09
  • see my adjusted example – pme Jul 02 '19 at 09:23
  • apparently the container keeps restarting or stops immediately after the dockerComposeUp. Do you know why that happens? – sentenza Jul 02 '19 at 12:05
  • have you checked the logs? the Docker-Log of postres docker image – pme Jul 02 '19 at 12:06
  • Checking the logs I found that `- POSTGRES_INITDB_ARGS="--encoding=UTF8 --locale=en_US.UTF8"` is not a valid server encoding name. Changing it to just `encoding=UTF8` seems to have fixed the issue. Thank you for your support. I accepted your response. – sentenza Jul 02 '19 at 12:39