I am trying to migrate my Docker image which uses library/wordpress:php8.2-apache as base image to library/wordpress:php8.3-fpm-alpine and nginx:stable-alpine as base images. Here is my Dockerfile
# Stage 1: Build the WordPress application
FROM library/wordpress:php8.3-fpm-alpine AS build
# Install wp-cli
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar &&
chmod +x wp-cli.phar &&
mv wp-cli.phar /usr/local/bin/wp
# Use different mirrors
RUN sed -i -e 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# Install Postfix
RUN apk update &&
apk add --no-cache postfix &&
postconf -e myhostname=mydomain.com &&
postconf -e relayhost="mailfwd.mydomain.com" &&
postfix start &&
postfix reload
# Configure PHP and create directory if it doesn't exist
RUN mkdir -p /usr/local/etc/php/conf.d/ &&
echo "sendmail_path=sendmail -t -i" >> /usr/local/etc/php/conf.d/sendmail.ini
# Install packages: wget, bash
RUN apk add --no-cache wget bash
# Install net-tools
RUN apk add --no-cache net-tools
# Copy built files to WordPress files system
COPY default/ /var/www/html/wp-content/themes/default/
# Set permissions for the wp-install-plugins.sh script
COPY wp-install-plugins.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/wp-install-plugins.sh
# Grant permissions to the www-data user
RUN chown www-data:www-data /usr/local/bin/wp-install-plugins.sh
# Stage 2: Build the NGINX server
FROM nginx:stable-alpine AS nginx-server
# Install bash in current image
RUN apk update && apk add --no-cache bash
# Check if www-data group exists, if not, add it
RUN if ! getent group www-data >/dev/null; then addgroup -g 1000 www-data; fi
# Add www-data user with existing www-data group
RUN adduser -D -H -u 1000 -G www-data -s /bin/bash www-data
# Copy the NGINX configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf
# Copy default index.php file
COPY default/index.php /var/www/html/
# Copy static content from WordPress build stage
COPY --from=build --chown=www-data:www-data /var/www/html/wp-content /var/www/html/wp-content
# Set permissions for static content
RUN find /var/www/html/wp-content -type f -exec chmod 644 {} ; &&
find /var/www/html/wp-content -type d -exec chmod 755 {} ;
# Expose port 80 to allow incoming connections
EXPOSE 80
# Command to start NGINX when the container starts
CMD ["nginx", "-g", "daemon off;"]
My nginx.conf file is:
server {
listen 80; # Listen on IPv4 address only
access_log off;
root /var/www/html;
index index.php;
server_tokens off;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}
# pass the PHP scripts to FastCGI server listening on wordpress:9000
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass wordpress:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
I am able to successfully build the image and run the container and the container is healthy as well,but for some reason when I go inside the container and run any command it just shows permission denied
docker exec -it my_container bash
my_container:/# ls -l
bash: /bin/ls: Permission denied
I also see the following error when I check the logs of my container by docker logs -f my_container
while trying to hit the url of my application
2024/02/29 03:06:59 [error] 21#21: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.2, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://172.18.0.13:9000", host: "myapp.mydomain.com"
2024/02/29 03:06:59 [warn] 21#21: *1 upstream server temporarily disabled while connecting to upstream, client: 172.18.0.2, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://172.18.0.13:9000", host: "myapp.mydomain.com"
Is there anything wrong with my Dockerfile and the configuration? May I please know what I am doing wrong or missing so that I can fix these issues?
2
Answers
The issue is I am not using the docker-compose to build the image. But I am using it to do the deployment. It is as follows:
I am using nginx as a reverse proxy for this wp-apps and also other apps running in the same server. Would you please recommend any modifications to the above answer to favor this setup?
There seem to be a few components missing from your question. The problem seems to be, however, with NGINX being unable to find the service at
wordpress:9000
.This is a working setup which should at least get you started.
🗎
Dockerfile
(I have stripped out the stuff relating topostfix
andsendmail
because it’s unclear what role they play in your project and don’t seem to be germane to the problem anyway.)🗎
docker-compose.yml
(This uses theDockerfile
above to build images for both thewordpress
andnginx
services.)🗎
nginx.conf
is the same as provided in question.Build and run.