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

How to use .ddev/docker-compose.mongo-extra.yaml to expose mongo port to host #15

Open
bfuzze opened this issue Sep 9, 2023 · 6 comments

Comments

@bfuzze
Copy link

bfuzze commented Sep 9, 2023

There might be a better way, but I added this to docker-compose.mongo.yaml in order to make the instance accessible outside of DDev environments.

services:
    mongo:
       ...
      ports:
         - "127.0.0.1:27017:27017"

In addition, I had to adjust connection credentials to use separate auth-database 'admin' and auth-protocol to SCRAM-SHA1.
image

The README states the default database is 'api', but I didn't see a database with this name, so I used 'local'.

Otherwise, very easy to setup.
Thanks.

@julienloizelet
Copy link
Collaborator

julienloizelet commented Sep 11, 2023

Hi @bfuzze , thanks for your message.

Regarding the MONGODB_DB=api, I'm not sure it is still relevant to speak about it in the README. Only source I found mentioning it is "https://api-platform.com/docs/core/mongodb/#enabling-mongodb-support".
@rfay : do you think we could remove this from the README ?

To access a mongodb UI, did you try the provided mongo-express ? (run ddev describe and you will see the url of the mongo-express UI.).

By the way, if your modification is ok for you, you should create a docker-compose.some-name.yaml instead of modifying the docker-compose.mongo.yaml file directly: this way, your modifications won't be lost if you run a ddev get ddev/ddev-mongo and did not remove the #ddev-generated comment.

For example, create a docker-compose.mongo-custom.yaml with just the following content:

services:
    mongo:
      ports:
         - "127.0.0.1:27017:27017"

Thanks

@rfay
Copy link
Member

rfay commented Sep 11, 2023

in order to make the instance accessible outside of DDev environments

Are you saying that you want a process on the host to access your mongo-in-container ?

Mongo uses a non-http protocol, so can't be proxied like HTTP can, so the ddev-router can't be used. So yes, if you're doing this, you'll need to expose the port, and the way you're doing it is correct, but as @julienloizelet says, use a separate override file to do it, and then you won't be preventing your ddev-mongo add-on from ever being upgraded again.

@julienloizelet I agree that MONGODB_DB=api is probably just very misleading to people.

@bfuzze
Copy link
Author

bfuzze commented Sep 11, 2023

@rfay

Are you saying that you want a process on the host to access your mongo-in-container ?

That is correct. I have a Node script running outside the container which requires access to the MongoDB instance, so I exposed the port (similar to other db container conventions).

@julienloizelet

For example, create a docker-composer.mongo-custom.yaml with just the following content:...
Perfect. Thanks!

@rfay
Copy link
Member

rfay commented Sep 11, 2023

I guess I don't think it's "normal" to add this kind of access, so maybe the docs don't need any update. Note that doing this means that you can't run two projects at the same time that use this, although each could have a different port specified.

@rfay rfay changed the title Feature request: Expose port to host environment and update documentation. How to use .ddev/docker-compose.mongo-extra.yaml to expose mongo port to host Sep 11, 2023
@stasadev
Copy link
Member

stasadev commented Mar 27, 2024

A small hook (requires jq) to see the connection string without using ports:

hooks:
    post-start:
        - exec-host: echo "Connect to MongoDB with mongodb://db:db@$(docker inspect $(ddev status -j | jq -r .raw.services.mongo.full_name) | jq -r '.[0].NetworkSettings.Networks."ddev-'$(ddev status -j | jq -r '.raw.name')'_default".IPAddress'):27017"

Without jq:

hooks:
    post-start:
        - exec-host: echo "Connect to MongoDB with mongodb://db:db@$(docker inspect $(ddev status -j | docker run -i --rm ddev/ddev-utilities jq -r .raw.services.mongo.full_name) | docker run -i --rm ddev/ddev-utilities jq -r '.[0].NetworkSettings.Networks."ddev-'$(ddev status -j | docker run -i --rm ddev/ddev-utilities jq -r '.raw.name')'_default".IPAddress'):27017"

Can be useful if you don't use mongo-express and want to access the database only from time to time (the IP changes every time you restart the project).

This is something similar to our database scripts (e.g. ddev dbeaver), and can be used to pass this connection string to some program that accepts this syntax.

@rfay
Copy link
Member

rfay commented Mar 27, 2024

In places where jq may not be available, use docker-t exec ddev/ddev-utilities jq

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

No branches or pull requests

4 participants