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

@wordpress/env: Loopback requests fail #20569

Open
noisysocks opened this issue Mar 2, 2020 · 13 comments
Open

@wordpress/env: Loopback requests fail #20569

noisysocks opened this issue Mar 2, 2020 · 13 comments
Labels
[Tool] Env /packages/env [Type] Bug An existing feature does not function as intended

Comments

@noisysocks
Copy link
Member

Describe the bug
WordPress isn't able to complete a loopback request when running under wp-env.

To reproduce

  1. npx wp-env start
  2. Navigate to http://localhost:8888/wp-admin/site-health.php

There's two errors related to this:

  • The REST API encountered an error
  • Your site could not complete a loopback request

Expected behavior
There should only be one critical error (the one about debug mode.)

Additional context
WordPress is trying to make a request to http://localhost:8888 from the underlying Docker container but this fails because Apache is listening on port 80. One solution would be to set up the docker-compose.yml config to map localhost to the host machine.

@noisysocks noisysocks added [Type] Bug An existing feature does not function as intended [Tool] Env /packages/env labels Mar 2, 2020
@tbclark3
Copy link

tbclark3 commented Apr 2, 2020

I am dealing with the same issue. Wordpress attempts to make a loopback request to its own hostname (not to localhost) on port 443 which fails because the container doesn't provide SSL. If there were a way to force loopback requests to use http instead of https, that might solve the problem, but I haven't found one yet.

@adrianduffell
Copy link

This issue seems to impact WP Cron for me. It looks like the scheduled events can't run because it uses loopback requests to create a background process.

I am able to work around this with the ALTERNATE_WP_CRON config.

// .wp.env.json

"config": {
  "ALTERNATE_WP_CRON": true
}

@glendaviesnz
Copy link
Contributor

Two possible solutions to this that I have found are:

  • Access wp-env instance through an external domain tunnel like https://ngrok.com/ in which case the callback will be made to that domain rather than localhost.
  • Add a hosts entry on the local machine for 127.0.0.1 host.docker.internal and access wp-env on http://host.docker.internal:8888 - within a docker container host.docker.internal resolves to itself.

For both of the above solutions you will need to change your WP config/settings to accept that domain name instead of the default localhost that wp-env sets.

@miminari
Copy link
Member

I reproduced this at @wordpress/[email protected] in M1 Mac

@jmichaelward
Copy link

This issue seems to impact WP Cron for me. It looks like the scheduled events can't run because it uses loopback requests to create a background process.

I am able to work around this with the ALTERNATE_WP_CRON config.

// .wp.env.json

"config": {
  "ALTERNATE_WP_CRON": true
}

@adrianduffell I'm wondering whether you're able to elaborate on your workaround for this a bit more? I've encountered this same issue while working on a plugin I'm developing that registers a custom cron task, and I've attempted to test the cron using the following commands:

  • wp-env run cli bash - to shell into the container
  • wp cron test <my_event_name> - to test the event.

This results in this error, whether or not ALTERNATE_WP_CRON is set to true in .wp-env.json:

Warning: The ALTERNATE_WP_CRON constant is set to true. WP-Cron spawning is not asynchronous.
Error: WP-Cron spawn failed with error: cURL error 7: Failed to connect to localhost port 8888 after 0 ms: Connection refused

It would be wonderful if we're able to find a solution for this issue. I've just started using wp-env for the first time and it's a terrific utility.

@ryelle
Copy link
Contributor

ryelle commented Aug 23, 2022

This issue also prevents loading editor styles from plugins (or themes if you're not paying attention).

For example, I have a plugin that sets up some font files, and it's added to the editor by add_editor_style( plugins_url( 'style.css', __FILE__ ) );

When the editor styles are loaded, it tries to wp_remote_get the file contents, but it's prevented by "cURL error 7: Failed to connect to localhost port 8888: Connection refused".

I had also added the theme's editor styles with add_editor_style( get_template_directory_uri() . '/build/editor.css' ); causing the same issue, but that's easily fixed by switching to add_editor_style( '/build/editor.css' );

@Sam-Smyth
Copy link

@noisysocks It looks like this was never resolved and I'm running into this issue. Your solution of editing the docker-compose.yml file doesn't seem to work since wp-env start overwrites that file, right? That's what I'm finding.

@noisysocks
Copy link
Member Author

Yeah, I was referring to making it so that wp-env writes a docker-compose.yml file that maps localhost to the host machine.

I don't use wp-env anymore (lately I've been trying https://ddev.com) so haven't been prioritising work on it. Feel free to open a PR in this repo containing a fix.

@mikey242
Copy link

I was having this issue too and one solution that I settled on was to change the default port number from 8888 to 80. This obviously won't work if you are using port 80 for another service but it is a relatively simple fix to implement.

You can either add "port": 80 to your .wp-env.json file or prepend your wp-env command like this: WP_ENV_PORT=80 wp-env start

@celtislab
Copy link

I created a plugin to prevent loopback request errors in WordPress environments that use Docker, such as wp-env.

localhost2host-docker-internal

Give it a try!

@rmorse
Copy link
Contributor

rmorse commented Aug 1, 2024

Thanks @celtislab , I dropped this into my mu-plugins and its working great right now.

@BrianHenryIE
Copy link
Contributor

Potentially, wp-env could script some changes to the Apache config as described here: docker-library/wordpress#611 (comment)

I tried to manually do it but it didn't seem to work

sed -i 's/:80/:80 *:8888/g' /etc/apache2/sites-enabled/000-default.conf
sed -i 's/80/80\nListen 8888/g' /etc/apache2/ports.conf

@collinr3
Copy link

I got it to work using _ as the delimiter instead of / ie
sed -i 's_80_80\nListen 8888_g' /etc/apache2/ports.conf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Tool] Env /packages/env [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests