Skip to content

Instantly share code, notes, and snippets.

@eliashussary
Created December 11, 2018 16:45
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save eliashussary/379e44a99e2389bd6a8ea6a23c2d5af8 to your computer and use it in GitHub Desktop.
Save eliashussary/379e44a99e2389bd6a8ea6a23c2d5af8 to your computer and use it in GitHub Desktop.
A docker-compose file for metabase with postgres.
version: "3"
services:
postgres-db:
image: postgres
restart: always
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: postgres
PGDATA: /var/lib/postgresql/data
volumes:
# declare your mount volume /host/dir:/container/dir
- /home/app/pgdata:/var/lib/postgresql/data
metabase-app:
image: metabase/metabase
restart: always
ports:
- 3001:3000
volumes:
# declare your mount volume /host/dir:/container/dir
- /home/app/metabase-data:/metabase-data
environment:
MB_DB_TYPE: postgres
MB_DB_DBNAME: metabase
MB_DB_PORT: 5432
MB_DB_USER: metabase
MB_DB_PASS: postgres
MB_DB_HOST: postgres-db
depends_on:
- postgres-db
links:
- postgres-db
@elalemanyo
Copy link

Hi,
Thanks for your docker-compose config file!
I have just one question: why do you need the metabase-data volume? For me is always empty... I am doing something wrong?

@oskar-fagerfjall
Copy link

I guess that is a developmental leftover from before migrating to postgres. Check out the meta base docker documentation: https://www.metabase.com/docs/latest/operations-guide/running-metabase-on-docker.html.
My understanding is that metabase by default uses a H2 database to store its configuration. In order to not lose your config on restart you need to mount a volume to store it. The next step up is to move the config to an external database by setting the environment variables like in this compose file. In this case you do not need the volume mount anymore because H2 is not used.

@binbin
Copy link

binbin commented Jun 8, 2020

Hi,
Thanks for your docker-compose config file!
I have just one question: why do you need the metabase-data volume? For me is always empty... I am doing something wrong?

add "MB_DB_FILE:/metabase-data/metabase.db" in environment

@renjithpaul27
Copy link

did this work for anyone ? i added like below in compose file.

volumes:
- ./metabase-data:/metabase-data
environment:
MB_DB_FILE: /metabase-data/metabase.db

but its creating an empty folder metabase.db. Am i doing something wrong?

@eliashussary
Copy link
Author

eliashussary commented Jun 19, 2020

did this work for anyone ? i added like below in compose file.

volumes:

  • ./metabase-data:/metabase-data
    environment:
    MB_DB_FILE: /metabase-data/metabase.db

but its creating an empty folder metabase.db. Am i doing something wrong?

@renjithpaul27

this is quite an old docker-compose file that i had for personal use as @oskar-fagerfjall pointed out, it attempts to fallback to an h2 database; which is a file based database.

i just attempted to run this docker-compose and i get a bunch of errors in the metabase container log.

org.postgresql.util.PSQLException: FATAL: database "metabase" does not exist
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2497)
        at org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2618)
        at org.postgresql.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:135)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:250)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
        at org.postgresql.Driver.makeConnection(Driver.java:458)
        at org.postgresql.Driver.connect(Driver.java:260)
        at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
        at clojure.java.jdbc$get_driver_connection.invokeStatic(jdbc.clj:271)
        at clojure.java.jdbc$get_driver_connection.invoke(jdbc.clj:250)
        at clojure.java.jdbc$get_connection.invokeStatic(jdbc.clj:411)
        at clojure.java.jdbc$get_connection.invoke(jdbc.clj:274)
        at clojure.java.jdbc$db_query_with_resultset_STAR_.invokeStatic(jdbc.clj:1111)
        at clojure.java.jdbc$db_query_with_resultset_STAR_.invoke(jdbc.clj:1093)
        at clojure.java.jdbc$query.invokeStatic(jdbc.clj:1182)
        at clojure.java.jdbc$query.invoke(jdbc.clj:1144)
        at clojure.java.jdbc$query.invokeStatic(jdbc.clj:1160)
        at clojure.java.jdbc$query.invoke(jdbc.clj:1144)
        at metabase.driver.sql_jdbc.connection$can_connect_QMARK_.invokeStatic(connection.clj:161)
        at metabase.driver.sql_jdbc.connection$can_connect_QMARK_.invoke(connection.clj:156)
        at metabase.driver.sql_jdbc$fn__73046.invokeStatic(sql_jdbc.clj:36)
        at metabase.driver.sql_jdbc$fn__73046.invoke(sql_jdbc.clj:34)
        at clojure.lang.MultiFn.invoke(MultiFn.java:234)
        at metabase.driver.util$can_connect_with_details_QMARK_$fn__22394.invoke(util.clj:30)
        at metabase.util$do_with_timeout$fn__9213.invoke(util.clj:310)
        at clojure.core$binding_conveyor_fn$fn__5754.invoke(core.clj:2030)
        at clojure.lang.AFn.call(AFn.java:18)
        at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.base/java.lang.Thread.run(Unknown Source)
06-19 12:57:12 ERROR metabase.core :: Metabase Initialization FAILED
java.lang.Exception: Looks like the database name is incorrect.
        at metabase.driver.util$can_connect_with_details_QMARK_.invokeStatic(util.clj:35)
        at metabase.driver.util$can_connect_with_details_QMARK_.doInvoke(util.clj:18)
        at clojure.lang.RestFn.invoke(RestFn.java:442)
        at clojure.lang.Var.invoke(Var.java:393)
        at metabase.db$fn__21334$verify_db_connection__21343$fn__21346$fn__21347.invoke(db.clj:309)
        at metabase.db$fn__21334$verify_db_connection__21343$fn__21346.invoke(db.clj:308)
        at metabase.db$fn__21334$verify_db_connection__21343.invoke(db.clj:300)
        at metabase.db$fn__21334$verify_db_connection__21343$fn__21344.invoke(db.clj:303)
        at metabase.db$fn__21334$verify_db_connection__21343.invoke(db.clj:300)
        at metabase.db$setup_db_BANG__STAR_$fn__21382.invoke(db.clj:345)
        at metabase.util$do_with_us_locale.invokeStatic(util.clj:649)
        at metabase.util$do_with_us_locale.invoke(util.clj:635)
        at metabase.db$setup_db_BANG__STAR_.invokeStatic(db.clj:344)
        at metabase.db$setup_db_BANG__STAR_.invoke(db.clj:339)
        at metabase.db$setup_db_from_env_BANG__STAR_.invokeStatic(db.clj:354)
        at metabase.db$setup_db_from_env_BANG__STAR_.invoke(db.clj:351)
        at metabase.db$setup_db_BANG_$fn__21393.invoke(db.clj:368)
        at metabase.db$setup_db_BANG_.invokeStatic(db.clj:366)
        at metabase.db$setup_db_BANG_.invoke(db.clj:361)
        at metabase.core$init_BANG_.invokeStatic(core.clj:77)
        at metabase.core$init_BANG_.invoke(core.clj:56)
        at metabase.core$start_normally.invokeStatic(core.clj:123)
        at metabase.core$start_normally.invoke(core.clj:117)
        at metabase.core$_main.invokeStatic(core.clj:156)
        at metabase.core$_main.doInvoke(core.clj:150)
        at clojure.lang.RestFn.invoke(RestFn.java:397)
        at clojure.lang.AFn.applyToHelper(AFn.java:152)
        at clojure.lang.RestFn.applyTo(RestFn.java:132)
        at metabase.core.main(Unknown Source)
Caused by: org.postgresql.util.PSQLException: FATAL: database "metabase" does not exist
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2497)
        at org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2618)
        at org.postgresql.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:135)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:250)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
        at org.postgresql.Driver.makeConnection(Driver.java:458)
        at org.postgresql.Driver.connect(Driver.java:260)
        at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
        at clojure.java.jdbc$get_driver_connection.invokeStatic(jdbc.clj:271)
        at clojure.java.jdbc$get_driver_connection.invoke(jdbc.clj:250)
        at clojure.java.jdbc$get_connection.invokeStatic(jdbc.clj:411)
        at clojure.java.jdbc$get_connection.invoke(jdbc.clj:274)
        at clojure.java.jdbc$db_query_with_resultset_STAR_.invokeStatic(jdbc.clj:1111)
        at clojure.java.jdbc$db_query_with_resultset_STAR_.invoke(jdbc.clj:1093)
        at clojure.java.jdbc$query.invokeStatic(jdbc.clj:1182)
        at clojure.java.jdbc$query.invoke(jdbc.clj:1144)
        at clojure.java.jdbc$query.invokeStatic(jdbc.clj:1160)
        at clojure.java.jdbc$query.invoke(jdbc.clj:1144)
        at metabase.driver.sql_jdbc.connection$can_connect_QMARK_.invokeStatic(connection.clj:161)
        at metabase.driver.sql_jdbc.connection$can_connect_QMARK_.invoke(connection.clj:156)
        at metabase.driver.sql_jdbc$fn__73046.invokeStatic(sql_jdbc.clj:36)
        at metabase.driver.sql_jdbc$fn__73046.invoke(sql_jdbc.clj:34)
        at clojure.lang.MultiFn.invoke(MultiFn.java:234)
        at metabase.driver.util$can_connect_with_details_QMARK_$fn__22394.invoke(util.clj:30)
        at metabase.util$do_with_timeout$fn__9213.invoke(util.clj:310)
        at clojure.core$binding_conveyor_fn$fn__5754.invoke(core.clj:2030)
        at clojure.lang.AFn.call(AFn.java:18)
        at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.base/java.lang.Thread.run(Unknown Source)
06-19 12:57:12 INFO metabase.core :: Metabase Shutting Down ...
06-19 12:57:12 INFO metabase.server :: Shutting Down Embedded Jetty Webserver
06-19 12:57:12 INFO metabase.core :: Metabase Shutdown COMPLETE

You need to ensure that there is a database named metabase on that postgres instance.

You can either start the postgres container first, and execute the necessary scripts. Or you can programatically do it through some additional config. - Have a look at the Initialization Scripts part of the postgres image - https://hub.docker.com/_/postgres

@sgobotta
Copy link

I stepped into the same problem and started a simple project using docker-compose. It's available for anyone that feels like forking and/or contributing. I also started it for personal use in a project at work, but any improvement is welcomed.

@haranobuhardo
Copy link

I stepped into the same problem and started a simple project using docker-compose. It's available for anyone that feels like forking and/or contributing. I also started it for personal use in a project at work, but any improvement is welcomed.

This is a great repo! Thanks. Makes Metabase production ready much easier.

@mattiashem
Copy link

version: '3'
services:
  metabase:
    image: metabase/metabase
    ports:
      - 3000:3000
    environment:
      MB_DB_TYPE: postgres
      MB_DB_DBNAME: metabase
      MB_DB_PORT: 5432
      MB_DB_USER: metabase
      MB_DB_PASS: metabase
      MB_DB_HOST: postgres
  postgres:
    image: postgres:latest
    environment:
      POSTGRES_USER: metabase
      POSTGRES_DB: metabase
      POSTGRES_PASSWORD: metabase
    #command: tail -f /etc/fstab
    volumes:
      - ./pg:/var/lib/postgresql/data

@renolation
Copy link

version: '3'
services:
  metabase:
    image: metabase/metabase
    ports:
      - 3000:3000
    environment:
      MB_DB_TYPE: postgres
      MB_DB_DBNAME: metabase
      MB_DB_PORT: 5432
      MB_DB_USER: metabase
      MB_DB_PASS: metabase
      MB_DB_HOST: postgres
  postgres:
    image: postgres:latest
    environment:
      POSTGRES_USER: metabase
      POSTGRES_DB: metabase
      POSTGRES_PASSWORD: metabase
    #command: tail -f /etc/fstab
    volumes:
      - ./pg:/var/lib/postgresql/data

does it work ?

@jflores1
Copy link

Thank you for this @mattiashem! @renolation it worked like a charm for me.

@ngophuong
Copy link

ngophuong commented Sep 25, 2023

Yes it work for me as well with postgres 16.0 latest version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment