I'm making a cURL request
curl -X 'POST' \
'http://localhost:8013/api/superheros' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"name": "Clark Kent",
"slug": "supermman",
"featured": true,
"createdAt": "2021-10-11T11:48:22.366Z"
}'
to an API endpoint on my local above. I receive a 502 bad gateway error.
I'm using Docker for all intents and purposes.
docker-compose.yml
version: '3'
services:
#PHP Service
app1:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: app1
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app1
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
webserver1:
image: nginx:alpine
container_name: webserver1
restart: unless-stopped
tty: true
ports:
- "8013:80"
- "443:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db1:
image: mysql:5.7.22
container_name: db1
restart: unless-stopped
tty: true
ports:
- "3318:3306"
environment:
MYSQL_DATABASE: rest_api
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
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-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"]
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 app1: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;
}
}
I've read people saying that PHP-FPM isn't running but my Dockerfile clearly uses it. Are you able to offer some clues?
You could try to pass arguments to php-fpm in your Dockerfile as below, and make sure 9000 port is configured in local.ini
CMD ["php-fpm", "-F", "-y", "/usr/local/etc/php/conf.d/local.ini"]
-F force php-fpm running in foreground
-y sepcify the config path
After Dockerfile modified, you need to run docker-compose build to rebuild docker image
Related
Hi im trying to lunch multiple project with my docker-compose each image was created by me from scratch FROM debian:11 my react and express runing perfectly but i don't know why ngnix and symfony can't connect to each other if someone know what is the problem i get a 502 Bad Gateway
nginx/1.23.2 and i don't have any error message when i build with my docker compose in the terminal.
Dockerfile -
app/symfony/Dockerfile
FROM debian:11
RUN apt update && \
apt upgrade -y && \
apt install dirmngr ca-certificates software-properties-common gnupg gnupg2 apt-transport-https curl librabbitmq-dev -y && \
curl -sSL https://packages.sury.org/php/README.txt | bash -x && \
apt update && \
apt upgrade -y && \
apt install php8.1 \
php8.1-fpm \
php8.1-mysql \
php8.1-pdo \
php8.1-gd \
php8.1-cli \
php8.1-dom \
php8.1-mbstring \
php8.1-simplexml \
php8.1-phar \
php8.1-dev \
php8.1-amqp \
php8.1-redis \
unzip \
wget \
git -y && \
mkdir /run/php
WORKDIR /var/www/symfony
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
php composer-setup.php && \
php -r "unlink('composer-setup.php');" && \
mv composer.phar /usr/local/bin/composer
RUN curl -1sLf 'https://dl.cloudsmith.io/public/symfony/stable/setup.deb.sh' | bash &&\
apt install symfony-cli
COPY composer.json composer.lock symfony.lock ./
COPY . .
RUN composer install
CMD ["php-fpm8.1", "-F"]
EXPOSE 9000
docker-compose of my project -
version: "3.8"
services:
nginx:
restart: unless-stopped
image: nginx:latest
ports:
- 1999:80
volumes:
- ./app/symfony:/var/www/symfony
- ./conf/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php-fpm
- db
php-fpm:
restart: always
build:
context: ./app/symfony
ports:
- 9000:9000
volumes:
- ./app/symfony:/var/www/symfony
depends_on:
- db
healthcheck:
test: [ "CMD", "php", "-v" ]
timeout: 30s
interval: 2s
retries: 30
db:
image: mysql:8
ports:
- 3306:3306
restart: always
volumes:
- ./database:/var/lib/mysql
environment:
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: docker
MYSQL_USER: user
MYSQL_PASSWORD: user_password
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- 8888:80
depends_on:
- db
node_express:
image: tlugat/debian_express
build:
context: ./app/express
restart: unless-stopped
ports:
- 8081:8081
react:
image: tlugat/debian_react
build:
context: ./app/react
restart: unless-stopped
ports:
- 3000:3000
maildev:
image: maildev/maildev
ports:
- 1080:1080
.default.conf-
conf/ngnix/default.conf
server {
listen 80;
index index.php;
server_name localhost;
root /var/www/symfony/public;
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\\.php(/|$) {
fastcgi_pass php-fpm:9000;
fastcgi_split_path_info ^(.+\\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
internal;
}
location ~ \\.php$ {
return 404;
}
}
I find the solution of my problem when you use docker to connect ngnix with php-fpm it's like you have vm in your vm so docker finds it, but don't know what to do when you pass him: fastcgi_pass php-fpm: 9000;
You just need to redirect ngnix to PHP-fpm like this docker understand his need to connect them together
I'm fairly new to docker and trying to understand a few things about it.
Here's my scenario, I want to have my application in it's own image and then use docker compose to blend it together (since I've read that you shouldn't be mixing nginx & php together, according to best practices).
I made a small PoC and so far it's not working, but I'm hoping it's just more me missing something important.
when I type in docker-compose up, everything loads but nginx seems to not know anything about the php service as it just loads the default nginx page and trying to access any php code from the php service gives me a 404.
this is what my compose yaml file looks like:
services:
web:
image: nginx:mainline-alpine
depends_on:
- php
ports:
- "80:80"
volumes_from:
- php
volumes:
- ./nginx.conf:/etc/nginx/conf.d/nginx.conf
php:
image: myprivaterepo.com/poc/php-test:latest
volumes:
- ./app:/app
and the dockerfile is currently simply:
FROM php:fpm
WORKDIR /app
COPY . .
CMD ["php-fpm"]
the idea is the php service has the php code and the web service would be able to access the files and it'd work.
I'm fairly sure I'm just missing something important but since I'm still new to this, I'm not sure what that is.
The goal would be to have the application all packed up and just spin up without a lot of hassle.
What I do for docker is
First Dockerfile with everything I need; change docker file as you desire.
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 \
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
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
Create mysql folder in root and my.cnf in that directory with the following content
[mysqld]
general_log = 1
general_log_file = /var/lib/mysql/general.log
Now create nginx/conf.d folder with app.conf with the following
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;
}
}
Now just create php/local.ini folder and leave it as it is for now
Now create docker-compose.yml file with following
version: "3"
services:
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: app-docker
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:
- projectdocker
webserver:
image: nginx:alpine
container_name: nginx-docker
restart: unless-stopped
tty: true
ports:
- "8081:80"
- "444:444"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- projectdocker
db:
image: mysql:5.7.22
ports:
- "6001:3306"
container_name: db-docker
restart: unless-stopped
tty: true
environment:
MYSQL_DATABASE: docker
MYSQL_ROOT_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql
- ./mysql/my.cnf:/etc/mysql/my.cnf
networks:
- projectdocker
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "7000:80"
links:
- db:db-docker
environment:
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
networks:
- projectdocker
volumes:
dbdata:
driver: local
networks:
projectdocker:
driver: bridge
now run docker-compose up -d –build
link to localhost will be http://localhost:8081/
link to phpmyadmin will be http://localhost:7000/
Problem with Laravel, once launched with PHP-FPM and NGINX no file is accessible except index.php.
Once my application is launched all the application is executed successfully except that the CSS and my JS scripts present in the public folder of Laravel are not accessible and therefore it is not applied on the application.
My application is deployed with Docker and works like this:
A container for the laravel application launched with PHP-FPM
A NGINX container as web server
A Mysql container
File docker-compose.yml :
version: '3'
services:
#PHP Service
app-dockerTag:
image: appName
build:
context: .
dockerfile: Dockerfile
container_name: app-dockerTag
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www/html/
#Nginx Service
webserver-dockerTag:
image: nginx:alpine
container_name: webserver-dockerTag
restart: unless-stopped
tty: true
ports:
- "portApp:80"
volumes:
- ./:/var/www
- ./build/nginx/:/etc/nginx/conf.d/
#MySQL Service
db:
image: mysql:5.7.22
container_name: db
restart: unless-stopped
tty: true
ports:
- dbPort:dbPort
volumes:
- ./mysql/:/docker-entrypoint-initdb.d
- ./api:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=dbRootPassword
- MYSQL_DATABASE=dbDatabase
- MYSQL_USER=dbUsername
- MYSQL_PASSWORD=dbPassword
- MYSQL_HOST_ROOT=%
- MYSQL_TCP_PORT=dbPort
File conf nginx :
server {
listen 80;
root /var/www/html/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app-dockerTag: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 ~ /\.(?!well-known).* {
deny all;
}
}
File DockerFiles for laravel :
FROM php:7.3-fpm
# Set working directory
WORKDIR /var/www/html
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
mariadb-client \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
libzip-dev
# 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
COPY composer.json composer.json
#COPY composer.lock composer.lock /var/www/
COPY . .
RUN composer install
RUN composer dump-autoload
# 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/html
# Copy existing application directory permissions
COPY --chown=www:www . /var/www/html
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm", "-F"]
My problem does not appear when I work locally with php artisan serve and once docked, all files normally accessible in public generate 404 errors.
I am trying to run Laravel 8 (with the database!) in Google Cloud. Deployment is done via Cloud Run with GitHub CI Trigger
What I have done so far:
I created all necessary Docker files for building the Docker image and container like the Dockerfile and docker-composer.yml in my apps doc root.
Because my app needs a backend and a database, I created an nginx.conf-file and an int_db.sql file, to seed the database.
Dockerfile
FROM php:7.4-fpm
# Arguments defined in docker-compose.yml
ARG user=hannes
ARG uid=1002
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Get the latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Create system user to run Composer and Artisan Commands
#RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN useradd -u 1002 -G www-data,root -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
# Set working directory
WORKDIR /var/www
USER $user
docker-compose.yml
version: "3.7"
services:
app:
build:
args:
user: hannes
uid: 1002
context: ./
dockerfile: Dockerfile
image: feedbackapp
ports:
- 8080
environment:
# /run/docs/reference/container-contract
PORT: ${PORT:-8080}
K_SERVICE: feedbackapp
K_REVISION: 0
K_CONFIGURATION: feedbackapp
container_name: feedbackapp-container
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- feedbackapp
db:
image: mysql:5.7
container_name: feedbackapp-db
restart: unless-stopped
environment:
MYSQL_DATABASE: homestead
MYSQL_ROOT_PASSWORD: homestead
MYSQL_PASSWORD: secret
MYSQL_USER: homestead
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker-compose/mysql:/docker-entrypoint-initdb.d
networks:
- feedbackapp
nginx:
image: nginx:alpine
container_name: feedbackapp-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d/
networks:
- feedbackapp
networks:
feedbackapp:
driver: bridge
feedbackapp.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: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;
}
}
In my local Docker environment, the Laravel 8 app gets successfully built and is working well, if i request the app via http://localhost:8000/.
So I created a Cloud Run Service in Google Cloud with a CI trigger of my GitHub Repo. So every time I push my branch, my Cloud Run Service is triggered and builds my app.
The build is finished successfully in Cloud Build.
Problem: if an open the apps link, I get a 503 Service Unavailable error
Web
Network Status
What the log tells me:
Log
Any help is much appreciated. Stay safe.
There is a big confusion.
Cloud Run needs Dockerfile and that should expose proper HTTP service on port 8080.
So docker-compose is not used by Cloud Run.
Make sure for Cloud Run that your Dockerfile expose port 8080.
make sure your container also has the HTTP server and the PHP executables.
What I see at first is that your Dockerfile doesn't have a webserver and doesn't expose port 8080.
docker-compose is only for local development.
I have a Laravel application, and able to up docker with these config files, while I am deployed to eb but keep getting error 502. Please let me know if you any other information from me. Thank you very much.
Here are my config files:
docker-compose.yml
version: '3'
services:
php:
build:
context: .
dockerfile: Dockerfile
image: php
depends_on:
- mysql
- redis
volumes:
- ./:/var/www
networks:
- app-network
mysql:
image: mysql:8
command: --default-authentication-plugin=mysql_native_password
ports:
- "3306:3306"
environment:
- MYSQL_DATABASE=app
- MYSQL_ROOT_PASSWORD=password
- MYSQL_USER=app
- MYSQL_PASSWORD=password
volumes:
- db-data:/var/lib/mysql
networks:
- app-network
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./docker/default.conf:/etc/nginx/conf.d/default.conf
- ./:/var/www
depends_on:
- php
networks:
- app-network
redis:
image: redis:alpine
ports:
- "6379:6379"
networks:
- app-network
volumes:
db-data:
#Docker Networks
networks:
app-network:
driver: bridge
Dockerfile
FROM php:7.2-fpm
ARG envfile
# Copy source code
RUN rm -rf /var/www/*
ADD . /var/www/
# Setup laravel
WORKDIR /var/www
RUN apt-get update && apt-get install -y \
git \
libzip-dev \
zip \
unzip \
cron \
sed
RUN docker-php-ext-configure zip --with-libzip
RUN docker-php-ext-install pdo_mysql zip
RUN docker-php-ext-install pcntl
RUN curl --silent --show-error https://getcomposer.org/installer | php && \
mv composer.phar /usr/local/bin/composer
## Configure nginx
RUN rm -rf /var/www/.env && \
composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader && \
touch /var/log/cron.log && \
chown www-data:www-data /var/log/cron.log && \
echo '* * * * * www-data php /var/www/artisan schedule:run >> /var/log/cron.log 2>&1' > /etc/cron.d/laravel-cron && \
chmod 0644 /etc/cron.d/laravel-cron && \
touch /etc/cron.d/laravel-cron && \
chown -R www-data:www-data /var/www/ && \
chown -R www-data:www-data /var/www/storage && \
chown -R root:www-data /var/www/storage/logs && \
chmod -R 775 /var/www/storage && \
chmod -R 775 /var/www/bootstrap/cache && \
crontab /etc/cron.d/laravel-cron
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
/docker/default.conf
server {
listen 0.0.0.0:80;
index index.php index.html index.htm;
root /var/www/public; # default Laravel's entry point for all requests
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php?$args;
}
location ~ \.php$ {
fastcgi_index index.php;
fastcgi_pass php:9000; # address of a fastCGI server
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
Cat my ec2 Nginx error log file
2019/09/15 05:21:18 [error] 4445#0: *9 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 202.184.195.155, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.17.0.3:9000/favicon.ico", host: "xxx.riaxasvw2y.ap-southeast-1.elasticbeanstalk.com", referrer: "http://xxx.riaxasvw2y.ap-southeast-1.elasticbeanstalk.com/"
[ec2-user#ip-172-31-43-158 ~]$ cat /etc/nginx/nginx.conf