create web app with php and docker - php

I have the following DOCKERFILE:
FROM php
RUN apt-get update && apt-get install -y git
RUN git clone https://github.com/marcosh/webinthemiddle.git
EXPOSE 80
CMD php -S localhost:80 webinthemiddle/index.php
that I would like to use to create a very basic app with php.
I build the image using sudo docker build -t marcosh/webinthemiddle .
and then I tried running the container using
sudo docker run -d -P marcosh/webinthemiddle
or
sudo docker run -d -P marcosh/webinthemiddle php -S localhost:80 webinthemiddle/index.php
Then I checked with sudo docker ps to which port was mapped the port 80 of the container and browser to localhost:#PORT#, but I found nothing there...
What am I doing wrong?

Two things will be helpful for you to debug this:
When debugging problems like this, start by running the container in the foreground interactively and with a TTY (-i and -t respectively). Your command would be something like sudo docker run -i -t -P marcosh/webinthemiddle php -S localhost:80 webinthemiddle/index.php.
Next, in the Docker container world, localhost is local to the container (not your host). Since the container is only listening on localhost, requests from other IP addresses (such as your host) won't get to the socket. You will want to listen on 0.0.0.0:80.

Related

PHP-FPM docker container stops responding after a while (in some cases weeks) on Digital Ocean with Caddy

I have 3 different Ubuntu droplets with Docker on Digital Ocean. They are all running Caddy as proxy with 2 of them using PHP-FPM and 1 using PHP-FPM Alpine.
After the server has been running a while, sometimes a few days, sometimes 3-4 weeks, the request stops responding.
All has the same type of problem.
I can SSH into the droplet. The caddy seems to be running all fine. I can access everything through caddy that is not the PHP.
The interessting part from the Caddy Docker log is this:
{
"level": "error",
"logger": "http.log.error",
"msg": "dialing backend: dial tcp 172.30.0.3:9000: i/o timeout",
"duration": 3.014910236,
"status": 502,
"err_id": "j14574vxr",
"err_trace": "reverseproxy.statusError (reverseproxy.go:1196)"
}
If I do a docker logs -f PHP_CONTAINER all I can see is a valid request from the time before PHP stopped responding.
If i restart only the PHP container it starts working again. The strange part is that if I ONLY do a restart of CADDY container it will also start working.
I didn't seem to have this problem with NGINX earlier. So I my initial thought was that CADDY was the problem. But I do get connection to other services on the same Docker server. For instance PHP-FPM service doesn't respond, but my phpMyAdmin service is still working.
The caddy file is like this:
mydomain.com {
root * /var/www/public
php_fastcgi /* php:9000
file_server
}
The PHP-FPM Dockerfile is like this:
FROM php:8.1.6-fpm
ARG user
ARG uid
RUN apt-get update && apt-get install -y \
curl \
zip \
unzip
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
r
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
WORKDIR /var/www
USER $user
I have tried multiple versions of PHP-FPM, it still happening.
For my WordPress site, using the FPM-Alpine version, I basically use the same Dockerfile as this (some smaller modifications only)
https://github.com/docker-library/wordpress/blob/master/latest/php8.1/fpm-alpine/Dockerfile
Does anyone has tips to what I can try to figure out why this error keeps happening.

PHP-FPM is not being restarted properly from a Dockerfile

I am having this weird problem with PHP-FPM. First of all, I am not an expert on PHP. I'm builing an app, and PHP will only communicate with MySQL to push and pull data.
The problem:
I have a Dockerfile where I'm making some changed in PHP-FPM config, and one of the lines is to reload PHP-FPM. Actually when you install PHP-FPM, it is not running, so I do:
RUN service php7.3-fpm start
When the app starts and I check PHP-FPM, it is not running. I had this issue before (with php7-0-fpm), I had resolved it by doing:
service php7.0-fpm stop && service php7.0-fpm start
But now, when I do it, it is still stopped.
So, I started reading and someone advised to put it in the CMD command, along with the main command. I did, and it worked:
CMD service php7.3-fpm start && nginx -g "daemon off;"
Now, the problem is that it is not taking the new configuration. When I access the machine, and I manually do service php7.3-fpm reload it starts working.
I tried putting it in the Dockrfile as well, but no luck. Any idea? I would like to resolve the first issue as well (I don't want to restart PHP-FPM from the CMD. It would be preferable to do it with a RUN layer in docker).
EDIT
Another weird thing is that when I do service php7.3-fpm start manually, it doesn't work, but it works when I do /etc/init.d/php7.3-fpm start. Seems to work, when I do it in the CMD line:
CMD /etc/init.d/php7.3-fpm start && nginx -g "daemon off;"
Dockerfile
FROM debian:buster
LABEL maintainer="me"
RUN apt-get update && apt-get install -y \
nginx \
default-mysql-client \
php7.3-fpm \
php7.3-mysql
RUN sed -i.bak "s/;clear_env = no/clear_env = no/g" /etc/php/7.3/fpm/pool.d/www.conf && \
sed -i.bak "s/;php_flag\[\display_errors\]\ = off/php_flag\[\display_errors\]\ = on/g" /etc/php/7.3/fpm/pool.d/www.conf
COPY ./html/ /var/www/html/
RUN rm /var/www/html/index.nginx-debian.html
WORKDIR /var/www/html/
EXPOSE 80
CMD service php7.3-fpm start && nginx -g "daemon off;"
Execute a command or restart php-fpm using RUN directive will not effect because each layer runs in a separate shell.
The best way is to copy the config file from host, rebuild the image and then run the container.
for Example
FROM debian:buster
COPY config/php7.ini /usr/local/etc/php/conf.d/
COPY config/fpm/php-fpm.conf /usr/local/etc/
COPY config/fpm/pool.d /usr/local/etc/pool.d
Also better to run the separate container for each process, rule of thumb single process per container.
You CMD seem fine it starts PHP and Nginx both.
To verify process inside your container add
RUN apt-get update && apt-get install procps -y
then run
docker exec -it your_container_id bash -c "ps -aux"

DOCKERFILE: Running multiple CMD. (Starting NGINX and PHP) [duplicate]

This question already has answers here:
Why can't I use Docker CMD multiple times to run multiple services?
(5 answers)
Closed 4 years ago.
I have a dockerfile that sets up NGINX, PHP, adds a Wordpress Repository. I want at boot time, to start PHP and NGINX. However, I am failing to do so. I tried adding the two commands in the CMD array, and I also tried to put them in a shell file and starting the shell file. Nothing worked. Below is my Dockerfile
FROM ubuntu:16.04
WORKDIR /opt/
#Install nginx
RUN apt-get update
RUN apt-get install -y nginx=1.10.* php7.0 php7.0-fpm php7.0-mysql
#Add the customized NGINX configuration
RUN rm -f /etc/nginx/nginx.conf
RUN rm -f /etc/nginx/sites-enabled/*
COPY nginx/nginx.conf /etc/nginx/
COPY nginx/site.conf /etc/nginx/sites-enabled
#Copy the certificates
RUN mkdir -p /etc/pki/nginx
COPY nginx/certs/* /etc/pki/nginx/
RUN rm -f /etc/pki/nginx/placeholder
#Copy the build to its destination on the server
RUN mkdir -p /mnt/wordpress-blog/
COPY . /mnt/wordpress-blog/
#COPY wp-config.php
COPY nginx/wp-config.php /mnt/wordpress-blog/
#The command to run the container
CMD ["/bin/bash", "-c", "service php7.0-fpm start", "service nginx start"]
I tried to put the commands in the CMD in a shell file, and run the shell file in the CMD command. It still didn't work. what am i missing?
start.sh
#!/bin/bash
/usr/sbin/service php7.0-fpm start
/usr/sbin/service nginx start
tail -f /dev/null
Dockerfile
COPY ["start.sh", "/root/start.sh"]
WORKDIR /root
CMD ["./start.sh"]
With this, you can put more complex logic in start.sh.
You can replace the CMD line for some like ...
CMD ["/bin/bash", "-c", "/usr/sbin/service php7.0-fpm start && nginx -g 'daemon off;'"]
TL;DR: You don't have an entry point.
Main idea in the Docker is to have one responsibility per container. So, in order to keep running a Docker container you have to start a program in foreground upon container boot.
However, in your Dockerfile, there is no entrypoint to start a program in foreground. So, just after your container boot, your container exits.
So, in order to prevent your container from exiting, just start a program in foreground.
Nginx for instance.
Example scenario:
entrypoint.sh content:
#!/bin/bash
service php7.0-fpm start
nginx -g 'daemon off;
somewhere in Dockerfile:
COPY [ "./entrypoint.sh", "/root/entrypoint.sh" ]
at the end of the Dockerfile:
ENTRYPOINT /root/entrypoint.sh

Nginx in Docker container won't run PHP

I'm beginner with Docker, and I'm trying to build my own image: Ubuntu + Nginx + PHP.
So, I have a directory called test. Inside directory two other directories, app and sites-enabled. Also, there's a Dockerfile, with content:
FROM ubuntu:trusty
RUN apt-get update && \
apt-get install -y nginx php5-fpm php5-mysql php-apc php5-imagick php5-imap php5-mcrypt php5-gd libssh2-php && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
ADD sites-enabled/ /etc/nginx/sites-enabled/
ADD app/ /app/
EXPOSE 80
CMD ["php5-fpm", "-c", "/etc/php5/fpm"]
CMD ["/usr/sbin/nginx"]
I build this image successfully. Then I create container with docker run -d image_name. I get the ID, and then I run docker inspect -f "{{.NetworkSettings.IPAddress}}" ID in order to get the IP address of the container.
I need this IP address, because I also run HAProxy in another container, so I can configure it to point to the right localtion.
So, both HAProxy and container with PHP app are running OK. HAProxy is pointing at the right application. PHP application files are uploaded at the right location inside the container.
But, Nginx doesn't execute PHP. Instead, when I try to access the application, I just get a downloaded file with my index.php PHP code.
What could be the problem? Please help.
My first guess was that I'm doing something wrong in Dockerfile when I run php5-fpm. I've tried few different ways, but non of them seem to work.
One CMD only.
If you need "services", look into supervisord or runit.

Docker with php built-in server

I'm trying to run php built-in server (php -S localhost:8080) via docker, I cannot access site from the host though - I always end up with Connection reset.
Here's a simple Dockerfile I build on:
FROM centos:centos6
RUN rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
RUN rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
RUN rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
RUN yum --enablerepo=remi,remi-php55 install -y php php-opcache php-cli php-pear php-common && yum clean all
RUN php -r "readfile('https://getcomposer.org/installer');" | php
RUN echo "date.timezone = Europe/Prague" >> /etc/php.ini
RUN mv composer.phar /usr/bin/composer
RUN php -r "eval('?>'.file_get_contents('http://backend.bolt80.com/piecrust/install'));"
RUN mv piecrust.phar /usr/bin/chef
CMD ["/bin/bash"]
Is it even possible to run this server with docker? While trying to make it work, I found out that when nginx was installed and set to listen on this very port, it is accessible from the host. PHP built-in server seems to be hidden from the host, thus not able to serve any requests though.
Anyone was successful making this work?
If from within the docker container you start your webserver with php -S localhost:8080 then the webserver will only accept connections originating from the docker container itself.
To be able to communicate with your webserver from the docker host you need to make two changes:
in your Dockerfile, add EXPOSE 8080, or when running the container add -p 8080 to the docker run command line. This will tell the docker host that your container has a program which expects communication on port 8080
start the webserver with php -S 0.0.0.0:8080 so it also accepts connections from outside of the docker container itself
Once you have a container running with those changes, open up a new terminal on the docker host and use the docker ps command to see what port on the docker host is forwarded to port 8080 in the container. For instance:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fbccf4058b07 test:latest "php -S 0.0.0.0:8080 4 minutes ago Up 4 minutes 0.0.0.0:49153->8080/tcp sad_hawking
In this example port 49153 of the docker host is to be used. Then query your webserver to validate you can communicate with it:
$ curl http://localhost:49153
For docker compose
services:
api:
build: ./api
command: php -S 0.0.0.0:80
ports:
- 80:80
with dockerfile, placed in ./api folder
FROM php:apline
WORKDIR /
COPY . .

Categories