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
LANDO_INFO inside a container not always matches lando info
#2565
Comments
I tried seeing if could spot the issue in the code. It definitely looks like LANDO_INFO is set somehow on the container and not run when |
After much more in depth review and some talks in Slack with @pirog, I still believe this is an issue but not something that can be sorted by the lando side of things. I found related issues around browser-sync and I think I come up with a neat workaround as noted on BrowserSync/browser-sync#505 (comment) That also improved on the guide above in making forms work (at least on Drupal) so I'll try to contribute to that at some point. Let you review this and see if you want to follow up anything more on this. |
I'm also affected by this. I'm trying to set the base URL in Drush but I'm getting the wrong port number in |
It seems this is populated only on initialisation (
Should this be run maybe also on |
Very annoying when you want the up-to-date localhost port. Is there a work around, something to force the update (beside a full rebuild)? |
You should NEVER use the localhost port internally. |
Thank you for the advice but that does not really answer the question, does it? I did not want to go off topic either by explaining to a full extend why I need it. But thangs anyway. Your remark provoked me to look further and I found what I needed. |
@com2 can you provide more information on how you fixed it? |
@pirog I suppose you have some use cases in mind for which the localhost port is not suitable, but there are legitimate use cases where this is needed. For example CLI tools and testing tools can use this information to communicate URLs to the end user. Some examples:
|
You should not be using the "randomly generated" external localhost port for anything except for viewing the site in the browser. Using it for anything programmatic is going to be inherently brittle. There are numerous other networking mechanisms in Lando and Docker at your disposal that are MUCH better suited for the use cases you describe. This is the point. And to be clear, this is not to say that |
Yes that is clear, that is what this issue is about. LANDO_INFO outputs URLs with the wrong port number and this prevents us from viewing the site in the browser.
Can you please elaborate on this? I'm not clear on how these use cases can be solved if we cannot get the correct port number from inside the container. All three cases I mentioned are outputting absolute URLs generated by PHP code that is running inside the container. Since the port number is not available to them, they output incorrect URLs. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions and please check out this if you are wondering why we auto close issues. |
Not stale. |
Here's how I worked around this issue for a WordPress install. WP-CLI, like Drupal's Drush, sometimes needs to know "what host does the web version of me run on?" if (getenv('LANDO_INFO')) {
$lando_info = json_decode(getenv('LANDO_INFO'));
$database_config = $lando_info->database;
define('DB_NAME', $database_config->creds->database);
define('DB_USER', $database_config->creds->user);
define('DB_PASSWORD', $database_config->creds->password);
define('DB_HOST', $database_config->internal_connection->host);
/**
* This is nasty.
*
* Lando does not keep LANDO_INFO up-to-date:
* https://github.com/lando/lando/issues/2565
*
* So when running via a web browser, we can sniff the HTTP_HOST
* and write that to a file. Then, when not running via web browser
* (like when running WP-CLI), we can read the host from the file
* and use that.
*/
$_lando_host_file = __DIR__ . '/.lando.host';
if (isset($_SERVER['HTTP_HOST'])) {
$_host = 'https://' . $_SERVER['HTTP_HOST'];
$_lando_host = @file_get_contents($_lando_host_file);
if ($_host !== $_lando_host) {
file_put_contents($_lando_host_file, $_host);
}
} else {
$_host = @file_get_contents($_lando_host_file);
}
defined('WP_HOME') || define('WP_HOME', $_host);
defined('WP_SITEURL') || define('WP_SITEURL', $_host);
unset($_host, $lando_host, $_lando_host_file, $lando_info, $database_config);
} TL;DR: the only way I can reliably get the "host name it is running on" is when it is accessed via the web server. I then write this value to a file if it has changed. The CLI then reads from this file when running not as a web server. I know. I know. But it unblocked me. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions and please check out this if you are wondering why we auto close issues. |
Not stale. |
Any update on this? I run Magento with Lando and Magento needs the base URL configured in its configuration. And because the URL might change after a |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions and please check out this if you are wondering why we auto close issues. |
Not stale please. In my case I'm trying to get the If |
@DuaelFr You can do that! .lando.yml
After a |
Thanks a lot @AaronFeledy! I thought the proxy was sending requests to the external connection port. I should have read the documentation more carefully 🤦 |
@AaronFeledy I spoke too quick. It's not working. Maybe that's because the proxy is trying to route only http traffic, I don't know. I tried to use a simple mysql client (ie. In the meantime, if that can be of any use for someone, I wrote that bash script that I manually execute when I need it (it's working even if your PHPStorm is already in use): #!/bin/bash
if [ ! -f .idea/dataSources.xml ]; then
echo ".idea/dataSources.xml not found. You need to create the datasource in PHPStorm at least once."
exit 1
fi
LANDO_INFO=`lando info -s database --format=json`
if [[ "$LANDO_INFO" == "[]" ]]; then
echo "\"database\" lando service not found"
exit 2
fi
DB_HOST=$(echo $LANDO_INFO | jq -r '.[0].external_connection.host')
DB_PORT=$(echo $LANDO_INFO | jq -r '.[0].external_connection.port')
DB_NAME=$(echo $LANDO_INFO | jq -r '.[0].creds.database')
DB_USER=$(echo $LANDO_INFO | jq -r '.[0].creds.user')
JDBC_URL="<jdbc-url>jdbc:mysql://$DB_HOST:$DB_PORT/$DB_NAME</jdbc-url>"
JDBC_USER="<user-name>$DB_USER</user-name>"
sed -i "s#<jdbc-url>.*</jdbc-url>#$JDBC_URL#" .idea/dataSources.xml
sed -i "s#<user-name>.*</user-name>#$JDBC_USER#" .idea/dataSources.xml You need to have created a datasource once first so the |
v3.0.11 on OSX 10.15.6
Trying to explain this the best way I can. I have my local native webserver running on port 80 and port 443, so on
lando start
I get different ports.I have a proxy to my node container so that I can use browser sync, very similar setup to https://docs.lando.dev/guides/how-to-wire-up-browsersync-and-gulp-in-my-lando-app.html with some tweaks.
I am trying to get the lando external proxy urls by looking into LANDO_INFO env variable inside the node container. The first time I run this (after
lando rebuild -y
) this worked. I got port 8080 and port 4433 for SSL. I thenlando restart
ed it and this time I got the proxy URLs with port 8000 and 444. However, LANDO_INFO inside the node container still had the old ports (8080, and 4433) so obviously when I feed those to browser sync it fails.My first guess was that LANDO_INFO was created on build but not updated on start but this is not consistent, I believe at one point it did update.
Couple test commands (this example shows it the other way around as what I wrote above):
After one
lando restart
:After another
lando restart
: I got the same output as the first set above (wrong URLs)After another
lando restart
: I got the same output as the second set above (proper URLs)After another
lando restart
: I got the same output as the first set above (wrong URLs)It does seem like the LANDO_INFO inside the container never (or hardly ever) changes.
The text was updated successfully, but these errors were encountered: