skip to Main Content

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


  1. Chosen as BEST ANSWER

    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:

    version: '3.1'
    services:
      wordpress:
        image: registry.io:5000/wp-image:current
        container_name: wordpress_1
        networks:
          - reverse-proxy
        restart: always
        hostname: my-wp-app.mydomain.io
        expose:
          - 80
        environment:
          WORDPRESS_DB_HOST: WP_HOST
          WORDPRESS_DB_USER: WP_DB_USER
          WORDPRESS_DB_PASSWORD: WP_DB_PWD
          WORDPRESS_DB_NAME: WP_DB_NAME
          VIRTUAL_HOST: my-wp-app.mydomain.io
          VIRTUAL_PORT: 80
        volumes:
          - /home/deploy/jenkins/my-wp-app/uploads:/var/www/html/wp-content/uploads
          - ${WORKSPACE}/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    networks:
      reverse-proxy:
        external:
          name: reverse-proxy
    

    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?


  2. 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.

    ├── default
    │   └── index.php
    ├── docker-compose.yml
    ├── Dockerfile
    ├── nginx.conf
    └── wp-install-plugins.sh
    

    🗎 Dockerfile (I have stripped out the stuff relating to postfix and sendmail because it’s unclear what role they play in your project and don’t seem to be germane to the problem anyway.)

    # STAGE 1: WORDPRESS --------------------------------------------------------------------
    
    FROM library/wordpress:php8.3-fpm-alpine AS build
    
    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
    
    RUN apk add --no-cache wget bash net-tools
    
    COPY default/ /var/www/html/wp-content/themes/default/
    COPY wp-install-plugins.sh /usr/local/bin/
    
    RUN chmod +x /usr/local/bin/wp-install-plugins.sh && 
        chown www-data:www-data /usr/local/bin/wp-install-plugins.sh
    
    # STAGE 2: NGINX ------------------------------------------------------------------------
    
    FROM nginx:stable-alpine AS nginx-server
    
    RUN apk update && apk add --no-cache bash
    
    RUN if ! getent group www-data >/dev/null; then addgroup -g 1000 www-data; fi
    
    RUN adduser -D -H -u 1000 -G www-data -s /bin/bash www-data
    
    COPY nginx.conf /etc/nginx/conf.d/default.conf
    
    COPY default/index.php /var/www/html/
    
    COPY --from=build --chown=www-data:www-data /var/www/html/wp-content /var/www/html/wp-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 80
    
    CMD ["nginx", "-g", "daemon off;"]
    

    🗎 docker-compose.yml (This uses the Dockerfile above to build images for both the wordpress and nginx services.)

    version: '3.8'
    
    services:
      wordpress:
        container_name: wordpress
        build:
          context: .
          target: build
        volumes:
          - wordpress-data:/var/www/html
        environment:
          - WORDPRESS_DB_HOST=db
          - WORDPRESS_DB_USER=wordpress
          - WORDPRESS_DB_PASSWORD=wordpress
          - WORDPRESS_DB_NAME=wordpress
        networks:
          - app-network
    
      nginx:
        container_name: nginx
        build:
          context: .
          target: nginx-server
        depends_on:
          - wordpress
        ports:
          - "80:80"
        volumes:
          - wordpress-data:/var/www/html
        networks:
          - app-network
    
      db:
        container_name: db
        image: mysql:5.7
        volumes:
          - db-data:/var/lib/mysql
        environment:
          MYSQL_ROOT_PASSWORD: somewordpress
          MYSQL_DATABASE: wordpress
          MYSQL_USER: wordpress
          MYSQL_PASSWORD: wordpress
        networks:
          - app-network
    
    volumes:
      wordpress-data:
      db-data:
    
    networks:
      app-network:
        driver: bridge
    

    🗎 nginx.conf is the same as provided in question.

    Build and run.

    docker-compose up --build
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search