Skip to content

Commit 6b5bd0f

Browse files
authored
PR #28 from psaux-it/host.docker
Host.docker
2 parents d44149c + b0df612 commit 6b5bd0f

File tree

5 files changed

+98
-41
lines changed

5 files changed

+98
-41
lines changed

.env

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export NGINX_CACHE=nginx_cache
2525
export NGINX_CONF=./nginx/nginx.conf
2626
export NPP_NGINX_CONF=./nginx/default.conf
2727
export NPP_NGINX_PARAMS_CONF=./nginx/fastcgi_params
28-
export NPP_NGINX_IP=172.19.84.1
2928
export NPP_HTTP_HOST=localhost
3029

3130
# WP-CLI Settings
@@ -56,14 +55,15 @@ export NPP_GID_=33749
5655

5756
# NPP Development environment
5857
export NPP_DEV_ENABLED_=1
58+
export NPP_HACK_HOST_=1
5959
export NPP_DEV_PLUGIN_NAME_=fastcgi-cache-purge-and-preload-nginx
6060
export NPP_DEV_PLUGIN_DIR_="${NPP_WEB_ROOT_}/wp-content/plugins/${NPP_DEV_PLUGIN_NAME_}"
6161
export NPP_DEV_TMP_CLONE_DIR_="/tmp/${NPP_DEV_PLUGIN_NAME_}"
6262
export NPP_DEV_PLUGIN_FILE_="${NPP_DEV_PLUGIN_DIR_}/${NPP_DEV_PLUGIN_NAME_}.php"
6363
export NPP_DEV_GITHUB_REPO_="https://github.com/psaux-it/nginx-fastcgi-cache-purge-and-preload.git"
6464

65-
# Plugins to auto Install (comma seperated) - Dev Tools
66-
export NPP_PLUGINS_="${NPP_DEV_PLUGIN_NAME_},query-monitor,plugin-check,wp-crontrol,health-check"
65+
# Plugins to auto Install (comma seperated)
66+
export NPP_PLUGINS_="${NPP_DEV_PLUGIN_NAME_}"
6767

6868
# Themes to auto Install (comma seperated)
6969
export NPP_THEMES_="blue-note"

docker-compose.yml

+5-8
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ services:
3333
if (defined('WP_CLI') && WP_CLI && !isset($$_SERVER['HTTP_HOST'])) $$_SERVER['HTTP_HOST'] = "${NPP_HTTP_HOST}";
3434
- NPP_WEB_ROOT=${NPP_WEB_ROOT_}
3535
- NPP_NGINX_CACHE_PATH=${NPP_NGINX_CACHE_PATH_}
36-
- NPP_NGINX_IP=${NPP_NGINX_IP}
3736
- NPP_HTTP_HOST=${NPP_HTTP_HOST}
3837
- NPP_USER=${NPP_USER_}
3938
- NPP_UID=${NPP_UID_}
4039
- NPP_GID=${NPP_GID_}
4140
- NPP_PLUGINS=${NPP_PLUGINS_}
4241
- NPP_THEMES=${NPP_THEMES_}
42+
- NPP_HACK_HOST=${NPP_HACK_HOST_}
4343
- NPP_DEV_ENABLED=${NPP_DEV_ENABLED_}
4444
- NPP_DEV_PLUGIN_NAME=${NPP_DEV_PLUGIN_NAME_}
4545
- NPP_DEV_PLUGIN_DIR=${NPP_DEV_PLUGIN_DIR_}
@@ -63,8 +63,10 @@ services:
6363
- /dev/fuse:/dev/fuse
6464
networks:
6565
npp_network:
66+
extra_hosts:
67+
- "host.docker.internal:host-gateway"
6668
post_start:
67-
- command: /scripts/wp-cli.sh
69+
- command: /scripts/wp-post.sh
6870
working_dir: ${NPP_WEB_ROOT_}
6971
user: root
7072
mem_limit: "2GB"
@@ -132,7 +134,7 @@ services:
132134
depends_on:
133135
- wordpress
134136
volumes:
135-
- ${WORDPRESS_HOME}:${NPP_WEB_ROOT_}
137+
- ${WORDPRESS_HOME}:${NPP_WEB_ROOT_}:ro
136138
- ${NGINX_CACHE}:${NPP_NGINX_CACHE_PATH_}
137139
- ${NGINX_CONF}:/etc/nginx/nginx.conf
138140
- ${NPP_NGINX_CONF}:/etc/nginx/conf.d/default.conf
@@ -150,7 +152,6 @@ services:
150152
- MOUNT_DIR=${MOUNT_DIR_}
151153
networks:
152154
npp_network:
153-
ipv4_address: 172.19.84.1
154155
mem_limit: "1.5GB"
155156
mem_reservation: "1GB"
156157
cpus: "1.0"
@@ -178,7 +179,3 @@ networks:
178179
name: npp-wp
179180
driver: bridge
180181
enable_ipv6: false
181-
ipam:
182-
config:
183-
- subnet: "172.19.0.0/16"
184-
gateway: "172.19.0.1"

wordpress/Dockerfile

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ RUN apt-get update && apt-get install -y \
4343
# Install dependencies for service health checks
4444
mariadb-client \
4545
netcat-traditional \
46+
iputils-ping \
4647
# Install common dependencies
4748
curl \
4849
# Install dev deploy dependencies
@@ -104,11 +105,11 @@ RUN install-php-extensions ${PHP_EXTENSIONS}
104105

105106
# Copy the scripts into the container
106107
COPY wordpress/entrypoint-wp.sh /entrypoint-wp.sh
107-
COPY wordpress/wp-cli.sh /scripts/wp-cli.sh
108+
COPY wordpress/wp-post.sh /scripts/wp-post.sh
108109

109110
# Make sure scripts are executable
110111
RUN chmod +x /entrypoint-wp.sh
111-
RUN chmod +x /scripts/wp-cli.sh
112+
RUN chmod +x /scripts/wp-post.sh
112113

113114
# Remove build time dependencies to keep image consistent
114115
RUN apt-get purge -y \

wordpress/entrypoint-wp.sh

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ for var in \
4444
NPP_GID \
4545
NPP_NGINX_CACHE_PATH \
4646
NPP_USER \
47-
NPP_DEV_ENABLED \
4847
MOUNT_DIR; do
4948
if [[ -z "${!var:-}" ]]; then
5049
echo -e "${COLOR_RED}${COLOR_BOLD}NPP-WP-FATAL:${COLOR_RESET} Missing required environment variable(s): ${COLOR_LIGHT_CYAN}${var}${COLOR_RESET} - ${COLOR_RED}Exiting...${COLOR_RESET}"

wordpress/wp-cli.sh renamed to wordpress/wp-post.sh

+87-27
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ for var in \
6464
NPP_UID \
6565
NPP_GID \
6666
NPP_DEV_ENABLED \
67-
NPP_NGINX_IP \
6867
NPP_HTTP_HOST \
68+
NPP_HACK_HOST \
6969
NPP_DEV_PLUGIN_NAME \
7070
NPP_DEV_PLUGIN_DIR \
7171
NPP_DEV_TMP_CLONE_DIR \
@@ -90,37 +90,97 @@ done
9090
# We need to sure '/var/www/html' exists for 'wp-cli'
9191
wait_for_service "wordpress" 9001
9292

93-
# To enable NPP - Nginx Cache Preload action:
94-
# #####################################################################
95-
# For Development Environment:
96-
# - Cause HTTP_HOST is localhost,
97-
# - Map the WordPress container's 'localhost' to Nginx's IP.
98-
# - Note: This is a tricky hack and only used for the development environment!
93+
# Resolve host
94+
resolve_host() {
95+
local host="$1"
96+
local ipv4=""
97+
local ip_fallback=""
98+
local result=()
99+
100+
# Try to get IPv4 address
101+
ipv4="$(ping -4 -c 1 "$host" | grep -oP '(?<=\()[^)]+' | head -n 1)"
102+
103+
# Fallback to find IP
104+
ip_fallback="$(getent hosts "${host}" | awk '{ print $1 }')"
105+
106+
# No IP found
107+
if [[ -z "${ipv4}" && -z "${ip_fallback}" ]]; then
108+
return 1
109+
# If both IPv4 and fallback IP are found
110+
elif [[ -n "${ipv4}" && -n "${ip_fallback}" ]]; then
111+
if [[ "${ipv4}" == "${ip_fallback}" ]]; then
112+
# If both IPs are equal, return only one
113+
result+=("${ipv4}")
114+
else
115+
# If both IPs are different, return both
116+
result+=("${ipv4}")
117+
result+=("${ip_fallback}")
118+
fi
119+
# If only one IP is found
120+
elif [[ -n "${ipv4}" ]]; then
121+
result+=("${ipv4}")
122+
else
123+
result+=("${ip_fallback}")
124+
fi
125+
126+
printf "%s\n" "${result[@]}"
127+
}
128+
129+
# To enable NPP Plugin Nginx Cache Preload action:
130+
# ##################################################################################################################
131+
# The NPP WordPress plugin uses "wget" with "WP_SITEURL" from inside the WordPress container to Preload Nginx Cache.
132+
# This means that if "WP_SITEURL" is set to "localhost", wget will attempt to fetch URLs from
133+
# the containers own loopback interface rather than reaching the Nginx server that handles
134+
# Cache Preload requests.
99135
#
100-
# For Production Environments: (Nginx sits on host or container)
101-
# - I assume you use a publicly resolvable FQDN for WordPress (WP_SITEURL & WP_HOME);
102-
# - Ensure outgoing traffic is allowed from the container.
103-
# - Verify that /etc/resolv.conf in the container is correctly configured.
104-
# - Verify that the container has internet access.
105-
# + That's all for Cache Preload works like a charm.
106-
#######################################################################
107-
if [[ "${NPP_DEV_ENABLED}" -eq 1 ]]; then
108-
IP="${NPP_NGINX_IP}"
109-
LINE="${IP} ${NPP_HTTP_HOST}"
136+
# To handle that;
137+
#
138+
# Development Environments:
139+
# - During "wp core install", the "--url" parameter is hardcoded as "https://localhost",
140+
# so WP_SITEURL ends up being "https://localhost" within the container.
141+
# - In this scenario, Nginx Cache Preload requests will try to access "https://localhost", which
142+
# incorrectly refers to the wordpress container itself.
143+
# - To work around this, we update the wordpress containers "/etc/hosts" file to remap "localhost" to either
144+
# "host.docker.internal" or the actual "Nginx container IP". This forces to retrieve resources
145+
# from the correct endpoint, enabling the Nginx Cache Preload action during development.
146+
# - Keep in mind! Below settings will not work here because of priority issue in /etc/hosts
147+
# extra_hosts:
148+
# - "localhost:Nginx_LAN_IP"
149+
#
150+
# Production Environment:
151+
# - WP_SITEURL is typically set to an FQDN (example.com) pointing to Nginx.
152+
# - If the WordPress container has WAN access, can resolve external domains, and allows outgoing traffic,
153+
# Cache Preload requests will correctly reach Nginx over the WAN route.
154+
# - If the wordpress container lacks WAN access, external DNS resolution, or outgoing traffic:
155+
# - WP_SITEURL (example.com) must resolve internally to Nginx LAN IP. (Nginx can sits on host or as a container)
156+
# - Solutions:
157+
# 1. Internal DNS resolver mapping WP_SITEURL to Nginx's LAN IP.
158+
# 2. Manually adding WP_SITEURL to /etc/hosts inside the wordpress container.
159+
# 3. Recommended docker way, edit wordpress service in docker-compose.yml,
160+
# extra_hosts:
161+
# - "example.com:Nginx_LAN_IP"
162+
###################################################################################################################
163+
if [[ "${NPP_HACK_HOST}" -eq 1 ]]; then
164+
# Create array
165+
mapfile -t ip_array < <(resolve_host host.docker.internal)
166+
167+
# Create temporary file
168+
TEMP_HOSTS="$(mktemp /tmp/hosts.XXXXXX)"
110169
HOSTS="/etc/hosts"
111170

112-
# Check if the Nginx static IP defined
113-
if ! grep -q "${IP}" "${HOSTS}"; then
114-
# Map localhost to Nginx Static IP
115-
echo -e "${LINE}\n$(cat ${HOSTS})" > /tmp/hosts.new
116-
cat /tmp/hosts.new > "${HOSTS}"
117-
rm -f /tmp/hosts.new
118-
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP:${COLOR_RESET} Mapped '${COLOR_LIGHT_CYAN}${NPP_HTTP_HOST}${COLOR_RESET}' to Nginx IP '${COLOR_LIGHT_CYAN}${IP}${COLOR_RESET}' in ${COLOR_LIGHT_CYAN}${HOSTS}${COLOR_RESET}."
119-
else
120-
echo -e "${COLOR_YELLOW}${COLOR_BOLD}NPP-WP:${COLOR_RESET} Mapping already exists: '${COLOR_LIGHT_CYAN}${NPP_HTTP_HOST}${COLOR_RESET}' -> '${COLOR_LIGHT_CYAN}${IP}${COLOR_RESET}'."
171+
# Hack /etc/hosts kindly, not make container upset
172+
# Map to host.docker.internal
173+
if (( ${#ip_array[@]} )); then
174+
for IP in "${ip_array[@]}"; do
175+
echo "${IP} ${NPP_HTTP_HOST}" >> "${TEMP_HOSTS}"
176+
done
177+
178+
cat "${HOSTS}" >> "${TEMP_HOSTS}"
179+
cat "${TEMP_HOSTS}" > "${HOSTS}"
180+
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP:${COLOR_RESET} ${COLOR_RED}Hacked!${COLOR_RESET} Mapped ${COLOR_LIGHT_CYAN}${NPP_HTTP_HOST}${COLOR_RESET} to host.docker.internal ${COLOR_LIGHT_CYAN}${ip_array[@]}${COLOR_RESET} in ${COLOR_LIGHT_CYAN}${HOSTS}${COLOR_RESET}."
121181
fi
122182
fi
123-
#######################################################################
183+
####################################################################################################################
124184

125185
# Check ownership of webroot for consistency
126186
check_ownership() {

0 commit comments

Comments
 (0)