0

It's a pretty standard docker container/setup

enter image description here

index.php is a standard call to phpinfo(); only docker compose fails to serve this file in a browser?

enter image description here

Dockerfile

FROM php:7.4-fpm

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

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    libzip-dev \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql zip exif pcntl
#RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
#    PHP 7.4 ^^
RUN docker-php-ext-install gd

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

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www

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

# Change current user to www
USER www

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

docker-compose.yaml

version: '3'
services:

  #PHP Service
  app_a:
    build:
      context: .
      dockerfile: Dockerfile
    image: digitalocean.com/php
    container_name: app_a
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app_a
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network

  #Nginx Service
  webserver_a:
    image: nginx:alpine
    container_name: webserver_a
    restart: unless-stopped
    tty: true
    ports:
      - "8211:80"
      - "443:443"
    volumes:
      - ./:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network

  #MySQL Service
  db_a:
    image: mysql:5.7.22
    container_name: db_a
    restart: unless-stopped
    tty: true
    ports:
      - "3236:3306"
    environment:
      MYSQL_DATABASE: my_local
      MYSQL_ROOT_PASSWORD: password
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network

#Docker Networks
networks:
  app-network:
    driver: bridge

#Volumes
volumes:
  dbdata:
    driver: local

All the docker service containers are running fine and I'm able to log into them at the shell using docker exec -it 3a234d1f384e sh for example.

If you could assist me in serving the file in the browser?, that'd be great. Thanks in advance.

Edit

/nginx/conf.d/app.conf

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app_a:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

enter image description here

2
  • You're copying all the files to /var/www, but the standard directory for web servers is /var/www/html. Do you have a custom nginx configuration to serve files from /var/www ?
    – Haxiel
    Dec 14, 2021 at 13:12
  • I have a custom nginx (nginx/conf.d/app.conf) config, yes. Please see my edit.
    – cookie
    Dec 14, 2021 at 14:28

1 Answer 1

1

For my particular setup above. I think my mistake was index.php needs to be inside of a public folder and served from there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.