RUN PHP 8, NGINX and PHP-fpm in single Docker container
Published: 2022-09-18 16:28:29


If you are looking to run PHP 8, NGINX and PHP-fpm in a single Docker container, then this article is for you. I will show you how to configure a Dockerfile and docker-compose file to build and run a PHP 8, NGINX and PHP-fpm in single container.

You can find nginx config and sample project here: [Github](https://github.com/mickalannz/backend) 

Full guide Article: [How to setup PHP 8, NGINX, PHP-FPM and Alpine with Docker](https://www.reviewcurious.com/how-to-setup-php-8-nginx-php-fpm-and-alpine-with-docker/)


Dockerfile

The first thing we need is a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. We will use the following Dockerfile to build our image.

```
# First, We need an Operating System for our docker. We choose alpine.
FROM alpine:3.16.2

# Next, Update Alpine OS
RUN apk update && apk upgrade

# Next, we need to install utilities inside alpine, we can achieve this by type RUN then, the alpine command.
# Install apline utilities and php depedencies
RUN apk add --no-cache \
    bash \
    php8 \ 
    php8-fpm \ 
    php8-opcache \
    php8-gd \
    php8-zlib \
    php8-curl \
    php8-bcmath \
    php8-ctype \
    php8-iconv \
    php8-intl \
    php8-json \
    php8-mbstring \
    php8-mysqlnd \
    php8-openssl \
    php8-pdo \
    php8-pdo_mysql \
    php8-pdo_pgsql \
    php8-pdo_sqlite \
    php8-phar \
    php8-posix \
    php8-session \
    php8-soap \
    php8-xml \
    php8-zip \
    libmcrypt-dev \
    libltdl 

# Next, Install nginx on alpine official guide in nginx official site. I just copied and paste what's on the site guide for installing nginx on alpine.
# https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/
RUN apk add openssl curl ca-certificates

# To set up the apk repository for stable nginx packages, run the command:
RUN printf "%s%s%s\n" \
"http://nginx.org/packages/alpine/v" \
`egrep -o '^[0-9]+\.[0-9]+' /etc/alpine-release` \
"/main" \
| tee -a /etc/apk/repositories

# Import an official nginx signing key so apk could verify the packages authenticity. Fetch the key:
RUN curl -o /tmp/nginx_signing.rsa.pub https://nginx.org/keys/nginx_signing.rsa.pub

# Verify that the downloaded file contains the proper key:
RUN openssl rsa -pubin -in /tmp/nginx_signing.rsa.pub -text -noout

# Move the key to apk trusted keys storage
RUN mv /tmp/nginx_signing.rsa.pub /etc/apk/keys/

# Now, install nginx
RUN apk add nginx

# Install PHP Composer, If you use composer, you can uncomment this one.
# RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# copy project file to nginx inside docker.
COPY ./src /usr/share/nginx/html/project

# Copy default config and paste it into nginx config path inside docker.
COPY ./nginx-configs/default.conf /etc/nginx/conf.d/default.conf

# Expose port to be visible outside the container.
EXPOSE 80
EXPOSE 443

STOPSIGNAL SIGTERM

# Execute startup command.
# Start php-fpm8 and nginx through bash terminal.
CMD ["/bin/bash", "-c", "php-fpm8 && chmod 755 /usr/share/nginx/html/* && nginx -g 'daemon off;'"]
```

Now we need docker-compose. 

Docker compose is a tool used to define and run multi-container applications. With compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. 

```
version: "1.0"

services:
  backend:
    # Specify Image name
    image: backend

    # Specify Container name
    container_name: backend

    # specify context and dockerfile to build
    build: 

      #Docker context is path where dockerfile is located.
      context: ./

      #specify Dockerfile to build.
      dockerfile: Dockerfile

    #expose ports
    ports:
      - "8082:80"
      - "443:43"

    #persistent volume
    volumes: 
      - ./src:/usr/share/nginx/html/project
```

Run the script in your project terminal.

```
docker-compose up -d
```

---

Site Source: [Review Curious](https://www.reviewcurious.com/)