Why caddy + php-fpm configuration in docker-compose doesn't work? - php

I'm trying to set up a development environment on my local machine which consists of caddy + PHP.
docker-compose.yaml:
version: '3'
services:
app:
build:
context: .
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
volumes:
- ./:/var/www
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
webserver:
image: caddy
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/srv
- ./docker/caddy/Caddyfile/:/etc/caddy/Caddyfile
db:
image: mysql:8.0
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_USER: pingr
MYSQL_DB: pingr
MYSQL_PASSWORD: pingr
MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
volumes:
- dbdata:/var/lib/mysql
volumes:
dbdata:
driver: local
Dockerfile for app service:
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 \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
libzip-dev \
libicu-dev
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql zip exif pcntl intl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
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"]
Caddyfile:
:80 {
root * /srv/public
php_fastcgi app:9000
file_server /uploads/*
encode gzip zstd
}
This results in "File not found" when I open http://localhost.
I'm 100% sure that the problem is because root in caddyfile and volumes in app and webserver serivces are different.
When I make them the same, looks like it works. Moreover, it even doesn't matter which path I choose, I can have /var/bar for example.
But then I don't understand how it works.
In caddy image they say that the working directory is /srv. So as I get it, this is where site files should be placed.
In the Caddyfile I specify that Caddy should look at /srb/public (because I use Laravel)..
In the php-fpm image, they say that the working directory is /var/www. Which means that.. I'm not sure what does it mean. If I don't use Docker, then in my local machine I'd have caddy + php, only caddy "knows" where the source .php files are located. In case the requests asks for a .php file Caddy proxies it to php. Maybe I don't need a volume here at all?

Related

Opening Docker container port on localhost in browser returns empty response

I'm trying to run a web project from a Docker container,
when I Dockerize the application on a macBook with an intel chip, everything runs fine and I can make a call to the Docker container. But when I run the same project, with the same setup on my M2 MacBook Air, the browser returns an empty response.
("this page isn't working" --> in Chrome)
This happens even though the containers appear to be running...
(Both containers are green lit up --> in Docker Desktop)
The container makes use of an Nginx service and a php service. The .yml file looks as below:
Docker-compose.yml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: php:8.0.6-fpm
container_name: Asset-Service
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: Asset-Service
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:
image: nginx:stable
container_name: Asset-Web-Server
restart: unless-stopped
tty: true
ports:
- "8087:80"
- "4487:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
My Dockerfile is the following, even though I don't think that this file causes the problem:
Dockerfile
FROM php:8.0.6-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install dependencies
USER root
RUN apt-get update && apt-get install -y \
mariadb-client-10.3 \
libcurl4-openssl-dev \
pkg-config \
libssl-dev \
libpng-dev \
libzip-dev \
libonig-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
nano
RUN pecl uninstall mongodb
RUN pecl install mongodb
RUN echo "extension=mongodb.so" >> /usr/local/etc/php/conf.d/mongodb.ini
# Clear cache
## RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install gd
#RUN docker-php-ext-enable mongodb
# Install composer
## RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -fg 1000 www
RUN id -u 1000 >/dev/null 2>&1 || 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"]
The container logs look normal and the ports in the inspect tab show 0.0.0.0:8087 and 0.0.0.0:4487.
Is this a recurring issue with the apple silicon version of Docker,
and is there anything I can do about it?
I have already tried to replicate the issue on an Intel macBook, but got the desired result instead of the empty response.
On my M2 I tried reinstalling Docker and rebuilding the containers but this didn't seem to fix anything...
Nevermind this issue.
This problem occurred when trying to build the containers from the Desktop folder.

Setup docker to use php-fpm + Laravel + supervisor in one container

I need to have supervisor for my laravel queues. But supervisor starts from root user, and I want to start php from another user for safety. I could not find solution to start php in another way, like a standart user from image - www-data.
And I also have files with backend owner. I read that it's safer to have php files with one owner and start php-fpm with another
Question: is it normal to work in producation with www-data user for php-fpm or I have to have another user for it. Or maybe I can unite php-fpm with supervisor(and cron in future) in another way? And if I have to change user how to start php with another user?
Dockerfile
FROM php:8.1-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install system dependencies
RUN apt-get update && apt-get install -y \
build-essential \
git \
curl \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
libonig-dev \
libxml2-dev \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
libpq-dev \
zlib1g-dev \
libzip-dev \
supervisor \
sudo
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
RUN pecl install -o -f redis \
&& rm -rf /tmp/pear \
&& docker-php-ext-enable redis
# Install PHP extensions
RUN docker-php-ext-install intl pdo pdo_pgsql pgsql mbstring exif pcntl bcmath gd zip
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Add user for laravel application
RUN groupadd -g 1000 backend
RUN useradd -u 1000 -ms /bin/bash -g backend backend
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=backend:backend . /var/www
RUN ["chmod", "+x", "./my_wrapper_script.sh"]
RUN ["chown", "-R", "www-data:www-data", "./storage/framework"]
RUN ["chown", "-R", "www-data:www-data", "./storage/logs"]
COPY --chown=root:root docker-compose/app/supervisor.conf /etc/supervisor/conf.d/supervisord.conf
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ./my_wrapper_script.sh
Docker-compose
version: 3.7
services:
app:
build:
context: ./
dockerfile: Dockerfile
image: didido
container_name: didido-app
restart: unless-stopped
working_dir: /var/www/
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
volumes:
- ./:/var/www
networks:
- didido
db:
image: postgis/postgis:14-3.1
restart: always
container_name: didido-db
networks:
- didido
environment:
- POSTGRES_DB=${DB_DATABASE}
- POSTGRES_USER=${DB_USERNAME}
- POSTGRES_PASSWORD=${DB_PASSWORD}
volumes:
- ../2. Init Database:/docker-entrypoint-initdb.d
- ./data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -d didido"]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
nginx:
image: nginx:1.17-alpine
container_name: didido-nginx
restart: unless-stopped
tty: true
ports:
- 80:80
depends_on:
- nodejs
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d
networks:
- didido
networks:
didido:
driver: bridge
my_wrapper_script.sh
#!/bin/bash
# Start the first process
php-fpm &
# Start the second process
supervisord &
# Wait for any process to exit
wait -n
# Exit with status of process that exited first
exit $?

Docker Laravel PostgreSQL - QueryException: could not find driver

I've been trying to run my new REST api calls but i get this error on postman:
Illuminate\Database\QueryException: could not find driver (SQL: insert into "companies" ("name", "establishment", "updated_at", "created_at") values (Tekmon, 2021-08-10, 2021-08-31 19:29:31, 2021-08-31 19:29:31) returning "id") in file /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 671
I have succesfully run my migrations to create the database tables.
phpinfo on docker container does not have pdo_pgsql extension on the list.
Dockerfile:
FROM php:8.0.7-fpm
COPY composer.lock composer.json /var/www/
#Set working directory
WORKDIR /var/www
#Install dependecies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
libzip-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
postgresql-dev
#Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
#Install Extensions
RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql
RUN docker-php-ext-install pdo pdo_pgsql pgsql zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
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 app
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 file:
version: '3'
services:
#POSTGRES Service
db:
image: postgres
container_name: db
restart: unless-stopped
tty: true
ports:
- "5432:5432"
environment:
POSTGRES_DB: tekmondb
POSTGRES_PASSWORD: tekmonpass
POSTGRES_USER: tekmon
volumes:
- dbdata:/var/lib/postgresql/data
networks:
- app-network
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
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:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#Docker networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
Laravel db environment: DB_CONNECTION=pgsql
config/database.php: 'default' => env('DB_CONNECTION', 'pgsql'),
I've searched many questions for some hours now but nothing worked.
Thanks.
As Sammitch below mentioned, and a video on YouTube by BPKodes, I needed to rebuild the container, which I didn't in the beginning, with libpq-dev instead of postgresql-dev. Hope this helps anyone.

Docker PHP Image when using Redis extension exited on signal 2 (SIGINT)

I have the following DockerFile and docker-compose.yml for an Laravel application. I have just added Redis today and this appeared in my terminal in the end of docker-compose up when I tried to connect and use Redis from PHP.
zinc_app | [23-Dec-2019 11:31:27] ALERT: oops, unknown child (32) exited on signal 2 (SIGINT). Please open a bug report (https://bugs.php.net).
zinc_app | [23-Dec-2019 11:31:27] ALERT: oops, unknown child (31) exited on signal 2 (SIGINT). Please open a bug report (https://bugs.php.net).
zinc_app | [23-Dec-2019 11:32:09] ALERT: oops, unknown child (20) exited on signal 9 (SIGKILL). Please open a bug report (https://bugs.php.net).
I am pretty sure the error came from the PHP Redis extension, because it didn't have the problem before. Have anyone encountered this problem before? Have I configured anything wrong?
I have tested this on another computer, but it didn't exhibit the same behavior. I have tried docker system prune -a and build and up again, but my computer still has the same problem.
As for the code I have run. I have run this in artisan tinker.
$redis = new Redis();
$redis->connect('zinc_redis', 6379);
$redis->get('1'); // The Oops message originated from this line.
Docker Compose:
version: '3.6'
services:
#PHP Service
zinc_app:
build:
context: .
dockerfile: Dockerfile
image: zinc/php
container_name: zinc_app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: zinc_app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- /tmp:/tmp #For CS Fixer
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
zinc_webserver:
image: nginx:alpine
container_name: zinc_webserver
restart: unless-stopped
tty: true
ports:
- "8080:80"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#Postgres Service
zinc_db:
image: postgres:12-alpine
container_name: zinc_db
restart: unless-stopped
tty: true
ports:
- "5432:5432"
environment:
POSTGRES_DB: zinc
POSTGRES_PASSWORD: admin
SERVICE_TAGS: dev
SERVICE_NAME: postgres
volumes:
- dbdata:/var/lib/postgresql
- ./postgres/init:/docker-entrypoint-initdb.d
networks:
- app-network
zinc_redis:
image: redis:5.0-alpine
container_name: zinc_redis
restart: unless-stopped
tty: true
ports:
- "6379:6379"
volumes:
- ./redis/redis.conf:/usr/local/etc/redis/redis.conf
- redisdata:/data
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
redisdata:
driver: local
FROM php:7.4-fpm
# Switch to root user
USER root
# Set working directory
WORKDIR /var/www
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
RUN chown www:www /var/www
# Copy composer
COPY --chown=www:www composer.json composer.lock /var/www/
# Install dependencies
RUN apt-get update && apt-get install --no-install-recommends -y \
build-essential \
libpq-dev \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
libzip-dev \
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 pgsql pdo_pgsql zip exif pcntl
# Enable igbinary & PHP Redis ext using igbinary
RUN pecl install -o -f igbinary && y | pecl install -o -f redis && docker-php-ext-enable redis igbinary
RUN rm -rf /tmp/pear
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
COPY --chown=www:www . /var/www/
# Change current user to www
USER www
# Install packages
RUN composer global require hirak/prestissimo && composer install
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

Docker with Laravel

I am setting up a development environment with the docker. In it I need
NGINX (webservice)
PHP 7.2 (app)
MARIADB 10.3 (database)
In the app container I must install composer, I will work using laravel.
Using RUN composer install andCOPY .env.example .env in dockerfile to install vendor and configure.env, I did not get any error messages but no files were created either.
docker-compose.yml
version: '3'
services:
app:
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
build:
context: .
dockerfile: Dockerfile
image: php:7.2
container_name: app
restart: unless-stopped
tty: true
working_dir: /var/www
networks:
- app-network
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
networks:
- app-network
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
db:
image: mariadb:10.3
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: db
MYSQL_ROOT_PASSWORD: pass
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- app-network
volumes:
- dbdata:/var/lib/mysql
networks:
app-network:
driver: bridge
volumes:
dbdata:
driver: local
dockerfile
FROM php:7.2-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 \
mariadb-client-10.3 \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
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 mbstring 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-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
# Run composer install
RUN composer install
# Create the .env from the .env.example
COPY .env.example .env
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
I have created a Setup to deploy a Laravel Application with the help of NGINX Web Server and MYSQL Database at Github.
It should be pretty easy to set up. If you encounter any issues, please feel free to raise an issue at Github, or ask me here.

Categories