Connecting dockers to non-Docker database

Hello! I am working on getting an instance of OpenMRS running with the O3 frontend. This is for educational use. I followed these instructions: https://openmrs.atlassian.net/wiki/spaces/docs/pages/150930190/Set+Up+an+Instance+of+O3#Using-Docker which worked great. I would prefer to connect to a MariaDB database on the server rather than in a docker. I am trying to follow the instructions here: Setting up an instance of O3 – O3 Docs … but this does not work. The dockers are running. /spa/home loads, but has no data. /initialsetup gives a 502 bad gateway error. I can see that there are no tables in the openmrs database and if I connect to the /backend server I can see that there is no openmrs.runtime.properties file created. For now, I am working on a clean Ubuntu24 install with an empty database. The only change I’ve made to the configruation is to run on port 8080 rather than localhost, because this is a server with no GUI. However, this works just fine.

My modified docker-compose.yml: docker-compose.yml - Pastebin.com

In the same folder as docker-compose.yml, I’ve created a file named .env with the contents:

OPENMRS_DB_USER=admin

OPENMRS_DB_PASSWORD=PASSWORD

I can log in to the database with these credentials from the command line.

I expect that I need to enter a connection string or port somewhere, but I don’t know where. I’ve also tried adding the variables DB_DATABASE, etc., as shown here: Use docker with external database - #2 by mksd but that didn’t work either. I’m not sure if localhost/127.0.0.1 would correct, though, since I guess that would be the container’s local host and not the server’s local host.

Any ideas?

1 Like

Is your DB running on host that runs Docker? If so please see How to access host port from docker container - Stack Overflow

Then use the host.docker.internal in OMRS_DB_HOSTNAME env in docker-compose instead of localhost.

1 Like

Thank you! Yes, it is running on the same host that runs the dockers. I will try this. Thanks!

1 Like

Thanks for sharing @ace! It’s really helpful to get feedback on those Docs :smiley: Please let us know if your problem is solved by Raff’s instructions :smiley:

Hi @ace how are things going now? FWIW I’ve taken your experience here and updated the Wiki docs, so that under “Troubleshooting with Docker” there’s now this tip:

Hi Grace,

Thanks! This did work, with the additional step of adding the docker localhost IP address to the MariaDB configuration file, and restarting MariaDB to flush the changes through. This step will vary a bit depending on the database and host system, but for MariaDB on Ubuntu 24, the steps were:

  1. Find the docker localhost IP (using ip a).
  2. Edit /etc/mysql/mariadb.conf.d/50-server.cnf file on the host system. Add docker localhost IP to the bind-address, e.g.:
bind-address          = 127.0.0.1,172.17.0.1
  1. Restart the MariaDB service to flush these changes through.
  2. Create the openmrs user in the database. In my case the user is named “admin”, so:
CREATE USER 'admin'@'%' IDENTIFIED BY 'PASSWORD';
GRANT ALL PRIVILEGES ON * . * TO 'admin'@'%';
FLUSH PRIVILEGES;
  1. In the docker-compose.yml file, make the following changes:

a. Set OMRS_CONFIG_CONNECTION_SERVER: host.docker.internal (Specifying it in the .env file and adding the reference to the .yml file didn’t work; it had to be added direclty to the .yml file.)

b. Add these lines to the backend configuration in the .yml file:

extra_hosts:
  - "host.docker.internal:host-gateway"

c. Remove lines 31-32, 46-62, and 66 from the docker-compose.yml file

  1. Create a file named .env in the same folder as the docker-compose.yml file which contains the environment variables, e.g.
OMRS_DB_USER=admin
OMRS_DB_PASSWORD=PASSWORD
  1. Start the dockers, following the instructions described on the wiki: https://openmrs.atlassian.net/wiki/spaces/docs/pages/150930190/Set+Up+an+Instance+of+O3#Using-Docker

Thanks for your help, and hopefully my notes will help the next person who’s trying it. :slight_smile:

1 Like