Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using unique service db names #515

Merged
merged 3 commits into from
Feb 13, 2025
Merged

Conversation

josep-tecnativa
Copy link
Contributor

@josep-tecnativa josep-tecnativa commented Jan 21, 2025

CC @Tecnativa TT54598

When multiple Odoo projects are deployed within the same Docker network, there is a risk that an instance of Odoo may inadvertently connect to the database of another project due to identical service names. This PR addresses the issue by ensuring unique service names, preventing unintended cross-project database connections and ensuring proper project isolation.

@josep-tecnativa josep-tecnativa force-pushed the using-unique-service-db-names branch 2 times, most recently from 629560c to 0ee4957 Compare January 28, 2025 12:20
@josep-tecnativa
Copy link
Contributor Author

@ap-wtioit How does this look to you?

@ap-wtioit
Copy link
Contributor

ap-wtioit commented Jan 28, 2025

When multiple Odoo projects are deployed within the same Docker network, there is a risk that an instance of Odoo may inadvertently connect to the database of another project due to identical service names.

This to me seems not just a problem with db service but with all services (e.g. smtp). I'm not sure if it is wise to run multiple Odoo projects/instances in the same network. (Or are you talking about the inverseproxy_shared network only?)

What i get from the changes:

  • DB_HOST is used to make sure that the odoo instance is connecting to a postgres service available under {{_key}}-db

When using this please make sure the value of _key is a valid hostname otherwise some key values might cause connection issues. E.g. _ is sometimes used in service names (and can be resolved by docker dns accessible on 127.0.0.11 inside a container within a bridge network) but a correct DNS resolvable hostname would not contain an _. Otherwise i fear that this might lead to issues later (if odoo, postgres or another tool begin to validate db_name / PG_HOST other than resolving it)

We are using a modified prod.yaml with and additional .docker/db-host.env file where PGHOST is defined and on deployment the file is created by symlinking to something like /path/to/postgres/containers/postgres_16.env. I think this should still be compatible with the proposed change.

To prevent multiple odoo (we do not have the problem with db as we are using one postgres container per DB_VERSION) from resolving to the same name in shared networks we usually use aliases and do not change the service name in docker compose:

services:
  odoo:
    ...
    networks:
      default: {}
      globalwhitelist_shared: {}
      inverseproxy_shared:
        aliases:
          - "${DB_NAME:-odoo}-${DOODBA_ENVIRONMENT:-prod}"
...

We use this for nginx providing access to Odoo from the web (instead of using traeffik).

So to sum it up, it looks ok. Only if someone already added PGHOST in db-access.env (or in our case new db-host.env) this could be a breaking change if environment: inside docker-compose.yaml (prod.yaml) wins over env files.

Edit: expected to need longer to find this. But indeed enviroment: overwrites env_file:: https://docs.docker.com/compose/how-tos/environment-variables/envvars-precedence/#simple-example

@josep-tecnativa
Copy link
Contributor Author

When multiple Odoo projects are deployed within the same Docker network, there is a risk that an instance of Odoo may inadvertently connect to the database of another project due to identical service names.

This to me seems not just a problem with db service but with all services (e.g. smtp). I'm not sure if it is wise to run multiple Odoo projects/instances in the same network. (Or are you talking about the inverseproxy_shared network only?)

What i get from the changes:

* DB_HOST is used to make sure that the odoo instance is connecting to a postgres service available under `{{_key}}-db`

When using this please make sure the value of _key is a valid hostname otherwise some key values might cause connection issues. E.g. _ is sometimes used in service names (and can be resolved by docker dns accessible on 127.0.0.11 inside a container within a bridge network) but a correct DNS resolvable hostname would not contain an _. Otherwise i fear that this might lead to issues later (if odoo, postgres or another tool begin to validate db_name / PG_HOST other than resolving it)

We are using a modified prod.yaml with and additional .docker/db-host.env file where PGHOST is defined and on deployment the file is created by symlinking to something like /path/to/postgres/containers/postgres_16.env. I think this should still be compatible with the proposed change.

To prevent multiple odoo (we do not have the problem with db as we are using one postgres container per DB_VERSION) from resolving to the same name in shared networks we usually use aliases and do not change the service name in docker compose:

services:
  odoo:
    ...
    networks:
      default: {}
      globalwhitelist_shared: {}
      inverseproxy_shared:
        aliases:
          - "${DB_NAME:-odoo}-${DOODBA_ENVIRONMENT:-prod}"
...

We use this for nginx providing access to Odoo from the web (instead of using traeffik).

So to sum it up, it looks ok. Only if someone already added PGHOST in db-access.env (or in our case new db-host.env) this could be a breaking change if environment: inside docker-compose.yaml (prod.yaml) wins over env files.

Edit: expected to need longer to find this. But indeed enviroment: overwrites env_file:: https://docs.docker.com/compose/how-tos/environment-variables/envvars-precedence/#simple-example

Hi Andreas,

First of all, thank you for your detailed feedback and suggestions! 😊

To clarify, yes, this issue occurs specifically with the inverseproxy_shared network. It's a very odd problem because it only happens on a couple of servers, while on the rest everything works just fine.

Also, regarding your setup, we actually use one PostgreSQL container per Odoo instance, rather than having one container per PostgreSQL version. This approach helps us avoid conflicts between instances, but the shared network issue seems to still persist in those specific cases (I think it cloud be a kind of bug on Docker Compose).

Our PGHOST is currently defined here. However, I need it to change dynamically based on the unique db service name, which is the reason behind my changes.

@josep-tecnativa josep-tecnativa force-pushed the using-unique-service-db-names branch 4 times, most recently from ca133d6 to a39a3e3 Compare January 29, 2025 06:53
@josep-tecnativa josep-tecnativa force-pushed the using-unique-service-db-names branch 12 times, most recently from d6765c0 to c5362b7 Compare February 4, 2025 16:48
@josep-tecnativa josep-tecnativa force-pushed the using-unique-service-db-names branch 7 times, most recently from 5e432a2 to 64f1ba8 Compare February 5, 2025 14:21
@josep-tecnativa josep-tecnativa force-pushed the using-unique-service-db-names branch 3 times, most recently from ff7d505 to 6fe3c9f Compare February 5, 2025 16:18
@josep-tecnativa josep-tecnativa force-pushed the using-unique-service-db-names branch from 6fe3c9f to 6d5d3e0 Compare February 12, 2025 17:31
@josep-tecnativa josep-tecnativa force-pushed the using-unique-service-db-names branch from 6d5d3e0 to ac1375c Compare February 13, 2025 06:48
@josep-tecnativa
Copy link
Contributor Author

Could you please, check this out @pedrobaeza ? I think it's ready to go.

@pedrobaeza pedrobaeza merged commit cb3e32a into main Feb 13, 2025
87 checks passed
@pedrobaeza pedrobaeza deleted the using-unique-service-db-names branch February 13, 2025 15:23
@PabloEForgeFlow
Copy link
Contributor

Hey @josep-tecnativa, it seems I'm a bit late to the party, but I just noticed this change after we ran an update on one of our projects and saw that scripts were failing due to the db service not existing anymore.

I personally believe that compose service names (together with volume and network names) should be treated as part of the template's "API" and should remain stable throughout (perhaps with the exception of major releases).

I've tried to replicate in various ways the issue this PR address and haven't been able to. You mentioned the issue only appears in a couple of servers, and just like @ap-wtioit said this does not appear to be a problem exclusive to the database service. Perhaps you could provide additional information on the specific setups that cause the issue so that we could address the compose issue instead?

In the meantime, do you think this same fix could be achieved by using network aliases instead of outright changing the default name of all database services? That way the fix would keep working while preventing potential disruptions to user scripts depending on the name of the database service.

@Tardo
Copy link
Contributor

Tardo commented Feb 24, 2025

For me, these changes break databases... :(

@pedrobaeza
Copy link
Member

For the record, this change doesn't break any database. If you do copier update on your scaffolding, your deployment tools may be affected, but nothing more.

The problem we faced was real, and reproduced already in 2 servers, having docker compose mixing the DBs from other versions of the same scaffolding (demo and prod for example). The conditions to get it are not clear though.

The options you have (apart from forking or freeze the copier updates, which I think are not adequate) are:

  • Adapt your tooling to this change. We needed to do it, but they have been very few changes.
  • Propose an alternative for having unique names for not having that mix. The network thing may work, but it seems everything gets more convoluted.

@Tardo
Copy link
Contributor

Tardo commented Feb 24, 2025

If I'm the only one with these problems... it must be because of how we update... What happened to me is that I got a PANIC: could not locate a valid checkpoint record.
I had to restore a copy, since I have not been able to repair the checkpoint.

@pedrobaeza
Copy link
Member

This is something only at docker compose level and how the DB service is called, so the checkpoint you are talking about should be another thing, and more if you didn't even update the scaffolding.

@Tardo
Copy link
Contributor

Tardo commented Feb 24, 2025

I have to investigate more what has happened to us... :/
It happened with the latest changes, but since we did not "attack" this repository, I was able to revert these changes and update again.

If I find anything that I think may be relevant to here I will comment :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants