Docker File Environment variable issue in PHP - php

I am using docker file for PHP Apache. I declare some environments variable in docker file but when I run the docker its only get Apache environment variable while my environment variable is not working.
here is my docker file.
# Install dependencies
From ubuntu:16.04
RUN apt-get update -y
RUN apt-get update && apt-get -y upgrade && DEBIAN_FRONTEND=noninteractive apt-get -y install \
vim apache2 php7.0 php7.0-mysql php7.0-gd php7.0-mbstring libapache2-mod-php7.0 curl lynx-cur
RUN service apache2 stop
# Install app
RUN rm -rf /var/www/html/*
ADD . /var/www/html
# Configure apache
RUN a2enmod php7.0
RUN a2enmod rewrite
RUN chown -R www-data:www-data /var/www/html/backend/runtime /var/www/html/backend/web/assets /var/www/html/uploads
RUN sed -i '/<Directory \/var\/www\/>/,/<\/Directory>/ s/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pidd
ENV test myenv_variable_value
EXPOSE 80
RUN service apache2 start
CMD ["apache2", "-D", "FOREGROUND"]
While I can easily print APACHE_PID_FILE but when I tried test its not working.
here is my PHP method
<?php
getenv('APACHE_PID_FILE');
$_ENV['test']
getenv('test');
?>
These both method are not working in case of test env.
Thanks

The problem is Apache doesn't expose environment variables which apache was launched with. You need to expose these environment variable using your config. Apache has a PassEnv directive that you need to use
https://httpd.apache.org/docs/current/mod/mod_env.html
PassEnv test
This will pass the environment test to your page. You need it for do it for every environment you want to expose.

Related

PHP-FPM does not start after container creation

I made a Dockerfile, but when I run it and enter the container, the php8.0-fpm service is not running.
How do I make it run at build time? Note that I run the command service php8.0-fpm start in the Dockerfile and even then it is not running.
What should I do for the php8.0-fpm service to start along with the container?
Below is the Dockerfile I made:
FROM ubuntu:jammy
ENV DEBIAN_FRONTEND=noninteractive
# Instalação Apache e PHP
RUN apt-get update && \
apt-get install software-properties-common -y && \
add-apt-repository ppa:ondrej/php -y && \
apt-get update && \
apt-get install -y \
apache2 \
libapache2-mod-php8.0 \
libapache2-mod-php \
php8.0-fpm \
libapache2-mod-fcgid \
# Alteração sequência index
COPY /src/dir.conf /etc/apache2/mods-enabled
# Commitando a nova configuração
RUN service apache2 restart
RUN service php8.0-fpm restart
# Inserindo página info.php
COPY /src/info.php /var/www/html
# Alterando módulos de multiprocessamento
RUN service apache2 stop && \
a2dismod php8.0 && \
a2dismod php8.1 && \
a2dismod mpm_prefork && \
a2enmod mpm_event && \
a2enconf php8.0-fpm && \
a2enmod proxy && \
a2enmod proxy_fcgi && \
service apache2 restart && \
service php8.0-fpm start
# Entrypoint para o conteiner iniciar o Apache
ENTRYPOINT ["apache2ctl", "-D", "FOREGROUND"]```
You need to run php fpm on startup. if you installed bash in your vm os, you can do it this way.
STOPSIGNAL SIGTERM
CMD ["/bin/bash", "-c", "php-fpm8 && include your apache here"]
Full guide: How to setup PHP 8, NGINX and PHP-FPM with docker
A Docker container only runs a single process. It doesn't "run services" per se. The image build doesn't preserve any running processes either. When you start the container, the image's ENTRYPOINT or CMD is the only thing that will be running in the container.
Avoiding the technical reasons, I'd broadly suggest that commands like service or systemctl just don't work in Docker. RUN service php8.0-fpm restart, for example, does nothing: PHP-FPM wasn't running before this command, and after the RUN command it won't be running either.
You'd typically restructure this into multiple separate containers, using a tool like Docker Compose to run them all together. Docker has a couple of official sample applications that demonstrate this. A Compose-based setup for this might look like
# docker-compose.yaml
version: '3.8'
services:
php:
build: . # using default Dockerfile
apache:
build:
context: .
dockerfile: Dockerfile.apache
ports:
- '8000:80'
Your Dockerfile would begin FROM php:8.0-fpm and contain only the PHP-related setup; Dockerfile.apache would begin FROM httpd:2.4 and only copy in the static assets and Apache configuration. Your proxy setup would need to reference the other container by name ProxyPass "/" "fcgi://php:9000"; see Networking in Compose in the Docker documentation for additional details.
Note that, even in this case, something like docker-compose exec apache service apache2 status still wouldn't show "a service is running", but if you docker-compose exec apache ps -e you'd see the HTTP daemon as process 1 within the container. This is normal.
I managed to leave it in just one container, PHP has an extension called Supervisor and with it installed we were able to start two or more services inside the container.
The Dockerfile looked like this:
FROM httpd:2.4-alpine
RUN apk update && \
apk add \
php \
php-fpm \
php-zip \
composer \
supervisor
COPY . /usr/local/apache2/htdocs
COPY httpd.conf /usr/local/apache2/conf/httpd.conf
COPY supervisor /etc/supervisor
WORKDIR /usr/local/apache2/htdocs
CMD ["supervisord","-n", "-c", "/etc/supervisor/supervisord.conf"]
And I created two configuration files for Supervisor.
apache.conf
[program:apache]
command=httpd -DFOREGROUND
autostart=true
autorestart=true
priority=10
startretries=1
startsecs=1
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
fpm.conf
[program:php-fpm]
command = php-fpm8 --nodaemonize
autostart=true
autorestart=true
priority=5
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
With that the two services started and it is working perfectly!

How to Create PHP Envirnoment in Docker?

I am trying to dockerize php web-app with apache2 server. any better way to create environment for php and apache2. I have search on internet cloud not able to find any solution.
I have used ubuntu:latest as base image,I have tried to create image in step if first one work than go ahead. I have already tried to create image without php and i was succesful to run docker container but without PHP there is no meaning to run than I have take next step to add PHP into image, now I am stuck.
There is image of Error below.
Here is my Dockerfile
#Download base image
FROM ubuntu:latest
#Install tzdata and set timezone.
#Update all packages run the 'apt update' command before installing any packages.
#we need to use -y flag otherwise it will ask for yes/no?
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata && apt-get install -y software-properties-common
#Install C/C++
RUN add-apt-repository -y ppa:ubuntu-toolchain-r/test
#RUN apt install build-essential
RUN apt-get update -y
RUN apt-get install -y gcc
RUN apt-get install -y g++
RUN ln -f -s /usr/bin/gcc /usr/bin/gcc
RUN ln -f -s /usr/bin/g++ /usr/bin/g++
RUN apt-get update -y
RUN add-apt-repository ppa:ondrej/php
# Install apache, PHP, and supplimentary programs. openssh-server, curl, and lynx-cur are for debugging the container.
RUN apt-get update && apt-get -y upgrade && DEBIAN_FRONTEND=noninteractive apt-get -y install \
apache2 php7.4 php7.4-mysql libapache2-mod-php7.4 curl lynx-common
# Enable apache mods.
RUN a2enmod php7.4
RUN a2enmod rewrite
#Remove any unnecessary files
RUN apt-get clean
#Setup Apache2 servers
#Debian configuration requires the environment variables APACHE_RUN_USER, APACHE_RUN_GROUP, and APACHE_PID_FILE to be set
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_PID_FILE /var/run/apache2.pid
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
#Expose ports
EXPOSE 80
#Change Permission
RUN chmod -R 777 /var/www/html/
#Copy files to webserver
COPY editor /var/www/html/
# Remove Default index.html
RUN rm /var/www/html/index.html
#Start services
CMD ["/usr/sbin/apache2ctl","-D","FOREGROUND"]
Thanks!!
The symbolic links for g++ & gcc are not good.
The following worked for me:
#Download base image
FROM ubuntu:latest
#Install tzdata and set timezone.
#Update all packages run the 'apt update' command before installing any packages.
#we need to use -y flag otherwise it will ask for yes/no?
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata && apt-get install -y software-properties-common
#Install C/C++
RUN add-apt-repository -y ppa:ubuntu-toolchain-r/test
#RUN apt install build-essential
RUN apt-get update -y && apt-get install -y gcc g++
#RUN apt-get install -y gcc
#RUN apt-get install -y g++
#RUN ln -f -s /usr/bin/gcc /usr/bin/gcc
#RUN ln -f -s /usr/bin/g++ /usr/bin/g++
#RUN apt-get update -y
RUN add-apt-repository ppa:ondrej/php
# Install apache, PHP, and supplimentary programs. openssh-server, curl, and lynx-cur are for debugging the container.
RUN apt-get update && apt-get -y upgrade && DEBIAN_FRONTEND=noninteractive apt-get -y install apache2 php7.4 php7.4-mysql libapache2-mod-php7.4 curl lynx-common
# Enable apache mods.
RUN a2enmod php7.4
RUN a2enmod rewrite
#Remove any unnecessary files
RUN apt-get clean
#Setup Apache2 servers
#Debian configuration requires the environment variables APACHE_RUN_USER, APACHE_RUN_GROUP, and APACHE_PID_FILE to be set
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_PID_FILE /var/run/apache2.pid
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
#Expose ports
EXPOSE 80
#Change Permission
RUN chmod -R 777 /var/www/html/
#Copy files to webserver
COPY editor /var/www/html/
# Remove Default index.html
RUN rm /var/www/html/index.html
#Start services
CMD ["/usr/sbin/apache2ctl","-D","FOREGROUND"]

unable to run Apache/php from docker container

My requirement is using openface i need to train the dataset(images) and test each input image from webinterface (PHP) and all this activity should run from docker container.
I am able to achieve the above requirment on ubuntu machine. we are trying to install the complete setup(apache/php & openface) in docker. currently we are unable to invoke the html files from apache server using docker
The following the docker file used to import the project into docker and install apache/PHP. Please let me know if any changes need to be done in the dockerfile.
FROM ubuntu:16.04
RUN apt-get update && \
apt-get -y install sudo
RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo
ADD opencv-3.0.0 /
ADD openface_setup.sh /
RUN /openface_setup.sh
ADD openface_work /
RUN apt-get update && apt-get -y upgrade && DEBIAN_FRONTEND=noninteractive apt-get -y install \
apache2 php7.0 libapache2-mod-php7.0 curl lynx-cur
RUN a2enmod php7.0
RUN a2enmod rewrite
RUN sed -i "s/short_open_tag = Off/short_open_tag = On/" /etc/php/7.0/apache2/php.ini
RUN sed -i "s/error_reporting = .*$/error_reporting = E_ERROR | E_WARNING | E_PARSE/" /etc/php/7.0/apache2/php.ini
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
EXPOSE 8080
VOLUME /var/www/html # **my PHP/html files are located here. In the docker container the html/php files are not reflecting**
ADD apache-config.conf /etc/apache2/sites-enabled/000-default.conf
CMD /usr/sbin/apache2ctl -D FOREGROUND
Once the container is started i want the test.html(located in /var/www/html) to be running.
FYI :
command to created docker image
sudo docker build -t myname/apache-test .
command to start the docker container
docker run -p 8080:80 -d <imageid>
I'd suggest to use the official PHP image with a pre-installed Apache installation.
Your project might look like this:
.
├── Dockerfile
└── src
└── index.php
while your Dockerfile consists of this:
FROM php:7.1-apache
# now RUN here your commands to install openface etc.
and your index.php could look like this:
<?php phpinfo();
Then build the image:
docker build -t myapache .
docker run --rm -p 8080:80 -v $(pwd)/src:/var/www/html myapache
http://localhost:8080 shows the php-info page.
You can extend the image to your needs and it's much simpler than your approach. Hope this might help.
If you do not need to install anything else, you can directly use the php:7.1-apache image when creating a new container.
try typing docker ps to get all the processes running in containers.
Then just type docker run -it container-id
It will start the apache server and show the address where it hosted it unless you want to add a different one in /etc/docker/daemon.json (https://docs.docker.com/engine/userguide/networking/default_network/custom-docker0/)

How to call this below script?

I have cloned a project from repo in projects directory, where I have got a few files as below. I am confused how it would be called or do i need to call it explicitly ? and this file is not containing any extension LIKE PHP OR SH, and project is in php using Vagrant AND VIRTUAL BOX, I am also not much familiar with VAGRANT.
#!/usr/bin/env bash
export DEBIAN_FRONTEND=noninteractive
installpkg(){
dpkg-query --status $1 >/dev/null || apt-get install -y $1
}
apt-get update
installpkg php5
installpkg php5-mysql
installpkg redis-server
installpkg mysql-server
installpkg apache2-utils
installpkg apache2-suexec-custom
Any help will be appreciated.
I believe this is a shell provisioning script that vagrant can execute when booting up the VM. you can add the following to your Vagrantfile so it is executed automatically
Vagrant.configure("2") do |config|
config.vm.provision "shell", path: "<path to your file>"
end

Linking to a Docker memcached container

I have been experimenting with Docker for a few days now and have grown to like it. However, there are a few things that still elude me. Here is what I have thus far
Create a low footprint Ubuntu 14.04 image
//I got this from a post on this forum
#!/bin/bash
docker rm ubuntu-essential-multilayer 2>/dev/null
set -ve
docker build -t textlab/ubuntu-essential-multilayer - <<'EOF'
FROM ubuntu:14.04
# Make an exception for apt: it gets deselected, even though it probably shouldn't.
RUN dpkg --clear-selections && echo apt install |dpkg --set-selections && \
SUDO_FORCE_REMOVE=yes DEBIAN_FRONTEND=noninteractive apt-get --purge -y dselect-upgrade && \
dpkg-query -Wf '${db:Status-Abbrev}\t${binary:Package}\n' |grep '^.i' |awk -F'\t' '{print $2 " install"}' |dpkg --set-selections && \
rm -r /var/cache/apt /var/lib/apt/lists
EOF
TMP_FILE="`mktemp -t ubuntu-essential-XXXXXXX.tar.gz`"
docker run --rm -i textlab/ubuntu-essential-multilayer tar zpc --exclude=/etc/hostname \
--exclude=/etc/resolv.conf --exclude=/etc/hosts --one-file-system / >"$TMP_FILE"
docker rmi textlab/ubuntu-essential-multilayer
docker import - textlab/ubuntu-essential-nocmd <"$TMP_FILE"
docker build -t textlab/ubuntu-essential - <<'EOF'
FROM textlab/ubuntu-essential-nocmd
CMD ["/bin/bash"]
EOF
docker rmi textlab/ubuntu-essential-nocmd
rm -f "$TMP_FILE"
Create a Dockerfile for an Apache image
FROM textlab/ubuntu-essential
RUN apt-get update && apt-get -y install apache2 && apt-get clean
RUN a2enmod ssl
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
EXPOSE 80
EXPOSE 443
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
docker build -t droidos/apache .
Create a Dockerfile for PHP5
FROM droidos/apache
RUN apt-get update && apt-get -y --reinstall install php5 php5-redis php5-memcached php5-curl libssh2-php php5-mysqlnd php5-mcrypt && apt-get clean
RUN php5enmod mcrypt
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
EXPOSE 80
EXPOSE 443
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
docker build -t droidos/php5 .
Create a Dockerfile for memcached and build the image
FROM textlab/ubuntu-essential
# Install packages
RUN DEBIAN_FRONTEND=noninteractive apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install memcached
# memcached public variable
EXPOSE 11211
CMD ["/usr/bin/memcached", "-u", "memcache", "-v"]
docker build -t droidos/memcached .
Fireup a docker container with memcached
docker run -d -P --name memcached droidos/memcached
Fireup a docker container with apache and link it to the memcached container created earlier
docker run -d --name apache --link memcached:memcached -v /var/droidos/site:/var/www/html -v /var/droidos/logs:/var/log/apache2 -p 8080:80 droidos/php5
Browse to example.com:8080
Everything seems ok
Create a memcached test script in /var/droidos/site
<?php
error_reporting(E_ALL);
header('Content-type:text/plain');
$mc = new Memcached();
$mc->addServer("localhost", 11211);
$flag = $mc->add('name','droidos');
echo ($flag)?'y':'n';
echo $mc->getResultCode();
?>
This script returns n47 implying that the memcached server is disabled.
Either my linking is incorrect or memcached has not been started or the memcached container port is not visible in the apache container. SSHing into the memcached container
docker exec -it <container-id> /bin/bash
and running
service memcached status
indicates that the service is not in fact running. So I start it
service memcached start
verify it has started and run the script above again. No joy - I still get an n47 reply rather than the y0 I would like to see. Clearly, I am missing a step somewhere here. I'd be most obliged to anyone who might be able to tell me what that might be.
I think it fails because you're trying to access memcached from the apache container connecting to the localhost of the apache container, while the memcached container is made accessible to the apache one on a different IP address.
This is the line I think is wrong:
$mc->addServer("localhost", 11211);
When you link containers, Docker adds a host entry for the source container to the /etc/hosts file (see the docs about linking).
Therefore you should be able to connect from the apache container to the memcached one using this PHP command:
$mc->addServer("memcached", 11211);
If it doesn't work, check that you can connect to the memcached service from the memcached container itself.

Categories