Score:1

Docker w/ PHP-FPM & Nginx - 502 Bad Gateway

de flag

I'm currently trying to dockerise a Laravel application, I've managed to create my Dockerfile and docker-compose.yml with the dependencies I need. However, I am struggling to get php-fpm to kick in that will show my application.

Dockerfile

# Set master image
FROM php:8.0-fpm-alpine

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/html/

# Set working directory
WORKDIR /var/www/html

# Install Additional dependencies
RUN apk update && apk add --no-cache \
    build-base shadow vim curl \
    php8 \
    php8-fpm \
    php8-common \
    php8-pdo \
    php8-pdo_mysql \
    php8-mysqli \
    php8-mbstring \
    php8-xml \
    php8-openssl \
    php8-json \
    php8-phar \
    php8-zip \
    php8-gd \
    php8-dom \
    php8-session \
    php8-zlib \
    nodejs \
    npm

# Add and Enable PHP-PDO Extenstions
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-enable pdo_mysql

# Install PHP Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Remove Cache
RUN rm -rf /var/cache/apk/*

# Add UID '1000' to www-data
RUN usermod -u 1000 www-data

# Copy existing application directory permissions
COPY --chown=www-data:www-data . /var/www/html

# Change current user to www
USER www-data

# Install app dependencies
COPY package.json /var/www/html/
RUN npm install
RUN npm run dev

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

docker-compose.yml

version: '3'
services:

  #Laravel App
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: heychazza/joinservers.com
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www/html
    expose:
      - "9000:80"
    volumes:
      - ./:/var/www/html
    networks:
      - mynet

  #Nginx Service
  nginx:
    image: nginx:alpine
    container_name: nginx
    restart: unless-stopped
    tty: true
    ports:
      - "8080:80"
    volumes:
      - ./:/var/www/html
      - ./deployment/nginx/conf.d/:/etc/nginx/conf.d/
      #- ./nginx/ssl/:/etc/nginx/ssl/
    depends_on:
      - app
    networks:
      - mynet

  #MySQL Service
  db:
    image: mariadb:latest
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "33060:3306"
    environment:
      MYSQL_DATABASE: laraveldb
      MYSQL_USER: laravel
      MYSQL_PASSWORD: laravelpassworddb
      MYSQL_ROOT_PASSWORD: rootpasswordmysql
      MYSQL_ROOT_HOST: '%'
    volumes:
      - mysqldata:/var/lib/mysql/
    networks:
      - mynet

#Docker Networks
networks:
  mynet:
    driver: bridge
#Volumes
volumes:
  mysqldata:
    driver: local

What am I doing? I'm running Docker from a MacBook, and are looking to deploy this onto a production Debian machine.

I'm still new to docker, so apologies if I've missed anything out.

Michael Hampton avatar
cz flag
What is the nginx configuration?
Score:1
de flag

Solved, turns out I needed to change the php location to.

    location ~ \.php$ {
        fastcgi_split_path_info  ^(.+\.php)(/.+)$;
        fastcgi_index            index.php;
        fastcgi_pass             app:9000;
        include                  fastcgi_params;
        fastcgi_param   PATH_INFO       $fastcgi_path_info;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.