I am using docker-compose.yaml
to define the services I need for my local development. Recently, I have added a new Traefik service to benefit from Domain Name Service Resolution capabilities that Traefik offers.
A typical Traefik service definition:
version: '3'
services:
traefik:
image: "traefik:v2.6.0"
container_name: coolstuff_solutions_traefik
command:
- "--log.level=DEBUG"
- "--providers.docker"
- "--api.dashboard=true"
- "--entrypoints.web.address=:80"
- "--accesslog.filepath=/data/access.log"
- "--providers.docker.exposedbydefault=false"
- "--providers.docker.network=coolstuff-solutions"
ports:
- "80:80"
- "8080:80"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
labels:
- "traefik.enable=true"
- "traefik.default.protocol=http"
- "traefik.http.routers.frontend.entrypoints=web"
- "traefik.http.routers.frontend.rule=Host(`coolstuff.localhost`)"
networks:
coolstuff-solutions:
aliases:
- traefikversion: '3'
services:
traefik:
image: "traefik:v2.6.0"
container_name: coolstuff_solutions_traefik
command:
- "--log.level=DEBUG"
- "--providers.docker"
- "--api.dashboard=true"
- "--entrypoints.web.address=:80"
- "--accesslog.filepath=/data/access.log"
- "--providers.docker.exposedbydefault=false"
- "--providers.docker.network=coolstuff-solutions"
ports:
- "80:80"
- "8080:80"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
labels:
- "traefik.enable=true"
- "traefik.default.protocol=http"
- "traefik.http.routers.frontend.entrypoints=web"
- "traefik.http.routers.frontend.rule=Host(`coolstuff.localhost`)"
networks:
coolstuff-solutions:
aliases:
- traefik
Let us say, that I already had an NGINX service running to support my development attempts through a specified port
; maybe designated through the docker-compose.yaml
configuration file. In my case I already had an NGINX dedicated service:
nginx:
image: nginx:alpine
container_name: coolstuff_solutions_nginx
volumes:
- ./app:/var/www
- ./sys/nginx:/etc/nginx/conf.d/
labels:
- "traefik.enable=true"
networks:
coolstuff-solutions:
aliases:
- nginx
It should be noted that the default.conf
file, for NGINX, refers to the php-fpm
service with fastcgi_pass app:9000;
.
In its own right, NGINX is a Reverse Proxy capable by nature. However, I want to take advantage of the Traefik capabilities.
Is it possible for me to mix Traefix, NGINX, and PHP-FPM if I still want to make use of FastCGI?
Below is the docker-compose.yaml
entry for the app
container and its related Dockerfile
and NGINX config.
#app service exposing API and any other pre-defined accessible endpoints
app:
build:
context: .
dockerfile: sys/php/Dockerfile
image: coolstuff_solutions:app
container_name: coolstuff_solutions_app
env_file:
- app/.env
environment:
APP_NAME: "CoolStuff Solutions"
working_dir: /var/www
depends_on:
- nginx
volumes:
- ./app:/var/www
- ./app/var/:/var/www/var:rw
- ./sys/php/default.ini:/usr/local/etc/php/conf.d/local.ini
labels:
- "traefik.enable=true"
- "traefik.default.protocol=http"
- "traefik.http.routers.app.entrypoints=web"
- "traefik.http.routers.app.rule=Host(`app.coolstuff.localhost`)"
- "traefik.http.services.app.loadbalancer.server.port=8080"
- "traefik.backend=app"
networks:
coolstuff-solutions:
aliases:
- app
#NGINX for PHP-FPM service
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: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;
}
}
# PHP-FPM Dockerfile
FROM php:8.2-fpm
ENV USER=www
ENV GROUP=www
# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends
git
curl
libldap2-dev
libpng-dev
libonig-dev
libxml2-dev
libgd-dev
zip
zlib1g-dev
librabbitmq-dev
unzip
ghostscript
fontconfig
graphviz
RUN pecl install amqp && docker-php-ext-enable amqp
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Set timezone
RUN rm /etc/localtime
&& ln -s /usr/share/zoneinfo/Africa/Harare /etc/localtime
&& "date"
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd intl
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
# Setup working directory
WORKDIR /var/www/
# Create User and Group
RUN groupadd -g 1000 ${GROUP} && useradd -u 1000 -ms /bin/bash -g ${GROUP} ${USER}
# Grant Permissions
RUN chown -R ${USER} /var/www
# Select User
USER ${USER}
# Copy permission to selected user
COPY --chown=${USER}:${GROUP} . .
EXPOSE 9000
CMD ["php-fpm"]
In my view, there should be means to bind Traefik to NGINX such that we can benefit from FastCGI but the fact that we speaking to two Proxy Server products, it seems like something is not adding up.