php and apache together in docker - php

first of all i pulled apache image:
docker pull httpd
then i ran my apache on 8080 port
docker run -dit --name httpd -p 8080:80 -v "$PWD":/usr/local/apache2/htdocs/ httpd
i opened 127.0.0.1:8080 and it is ok
i have a file named info.php and it exists in PWD and when i open it in my browser i see php code and it had not been compiled
how can it use php and apache together in docker and how i am able to run php in my browser
im new comer to docker

you can use this image php:<version>-apache
php:-apache
This image contains Debian's Apache httpd in conjunction with PHP (as
mod_php) and uses mpm_prefork by default.
docker run -d -p 80:80 --name my-apache-php-app -v "$PWD":/var/www/html php:7.2-apache
Or you can copy PHP files at build times.
Tree
├── Dockerfile
└── src
└── info.php
Dockerfile
FROM php:7.2-apache
COPY src/ /var/www/html/
Run container
docker run -d -p 80:80 --name my-apache-php-app my_image

Related

Docke run volume root file ownership issue

I'm having issues getting a mounted docker volume to use a user other than root.
Any files I create with this command:
docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp php:8.1-cli php "$#"
will always have root ownership on my host directory. Can someone clue me into how I get it to mount with my host's dir user? (Ex: /home/meisuser/mountedvolume
I've tried:
docker run -it --rm --name my-running-script --user $(id -u):$(id -g) -v "$PWD":/usr/src/myapp -w /usr/src/myapp php:8.1-cli php "$#"
but it doesn't seem to work.

Composer while 'docker-compose' with mount bind

I try to build a docker-compose project for a php application beside a database, which is automatically deployed on a developer machine. It's not a problem, but where it's stuck is the composer-install.
I want to have a directory from the host computer which is bind into the container, so developer are able to change code and are able to see it immediately in the docker instance. That means
Deploy a container with PHP and a webserver
Deploy the database
Bind the local source directory into the PHP container
Execute a composer-install in that directory
The directory tree is like
/
-php
-- src
-- Dockerfile
-postgres
-- Dockerfile
docker-compose.yml
I attached snippets from my docker-compose.yml and the PHP Dockerfile, anyone has an idea why it fails or see the problem in the order or something else or even can me explain what I have to notice? That would be great!
Dockerfile:
FROM xy
RUN curl -sS https://getcomposer.org/installer | php \
&& mv composer.phar /usr/local/bin/ \
&& ln -s /usr/local/bin/composer.phar /usr/local/bin/composer
COPY ./src /var/www/html
WORKDIR /var/www/html/
RUN composer install --prefer-source --no-interaction
Docker-compose file:
version: '3.4'
services:
php:
build: ./php
container_name: "php"
volumes:
- ./php/src:/var/www/html
postgres:
image: postgres:10.4-alpine
container_name: "postgres"

How to use php & nodejs from separate containers

Let's say I have the following situation:
docker-compose.yml
version: '3'
services:
web:
build: .
links:
- apache
- php
- node
depends_on:
- apache
- php
- node
volumes:
- .:/var/www/html
apache:
image: httpd
php:
image: php
node:
image: node
and I also have a Dockerfile
FROM phusion/baseimage
RUN apt-get update
RUN apt-get install -yq git curl zip wget curl
RUN curl -s https://getcomposer.org/installer | php # error, no PHP exec found
RUN mv composer.phar /usr/local/bin/composer
RUN npm install -g bower gulp
COPY ./ /var/www/html
WORKDIR /var/www/html
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
CMD ["/sbin/my_init"]
Now my question is: how could I use PHP & Node, which are installed in separate containers, in this main app Dockerfile? Is that even possible, or do I need to manually install PHP & Node inside my Dockerfile?
Docker doesn't work the way you are thinking. This isn't like code inheritance or configuring a single machine with multiple runtime languages. You are configuring multiple virtual machines, each with their own runtime environment.
if you want to run a PHP app, you put that app in the container that has PHP. same with Node.

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/)

can't update php.ini file in Docker container

I'm trying to set Magento2 on Docker with Nginx & PHP7.
I've added a custom php.ini file as recommended by the PHP7 Docker image. I can see from phpinfo.php that it's loading my php.ini file but none of my updates are there.
It should be:
memory_limit = 2G
max_execution_time = 800
I've checked the PHP container and I can see the php.ini file is there with the correct settings, or so I think?
$ docker exec -it mymagento2docker_php_1 /bin/bash
# cat /usr/local/etc/php/php.ini
; This file is created automatically by the docker build
memory_limit = 2G
max_execution_time = 800
What am I doing wrong? Below are some more details.
Docker Project
.
├── docker-compose.yml
├── index.php
├── magento2
│   ├── [DIRECTORIES & FILES OMMITED]
│  
├── nginx
│   ├── Dockerfile
│   ├── default.conf
│   └── nginx.conf
├── output.txt
├── php
   ├── Dockerfile
    └── config
   └── php.ini
docker-compose.yml
nginx:
build: ./nginx/
ports:
- 80:80
links:
- php
volumes_from:
- app
php:
build: ./php/
expose:
- 9000
links:
- mysql
volumes_from:
- app
app:
image: php:7.0-fpm
volumes:
- ./magento2:/var/www/html
command: "true"
mysql:
image: mysql:latest
volumes_from:
- data
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: mage2
MYSQL_DATABASE: mage2
MYSQL_USER: mage2
MYSQL_PASSWORD: mage2
data:
image: mysql:latest
volumes:
- /var/lib/mysql
command: "true"
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 8080:80
links:
- mysql
environment:
PMA_HOST: mysql
php/Dockerfile
FROM php:7.0-fpm
# Install dependencies
RUN apt-get update \
&& apt-get install -y \
cron \
libfreetype6-dev \
libicu-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
libxslt1-dev
# Configure the gd library
RUN docker-php-ext-configure \
gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
# Install required PHP extensions
RUN docker-php-ext-install \
gd \
intl \
mbstring \
mcrypt \
pdo_mysql \
xsl \
zip \
soap
# Install the 2.4 version of xdebug that's compatible with php7
RUN pecl install -o -f xdebug-2.4.0
COPY config/php.ini /usr/local/etc/php/
## php/config/php.ini ##
; This file is created automatically by the docker build
memory_limit = 2G
max_execution_time = 800
UPDATE
I've tried restarting nginx with the below, but it did not work.
$ docker exec -it mymagento2docker_php_1 /bin/bash
# /etc/init.d/nginx restart
bash: /etc/init.d/nginx: No such file or directory
# service nginx restart
nginx: unrecognized service
# nginx -s reload
bash: nginx: command not found
# exit
$ docker restart mymagento2docker_nginx_1
mymagento2docker_nginx_1
$ docker exec -it mymagento2docker_nginx_1 /bin/bash
# /etc/init.d/nginx restart
Restarting nginx: nginxross in ~/my-magento2-docker
$ docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------
mymagento2docker_app_1 true Exit 0
mymagento2docker_data_1 docker-entrypoint.sh true Exit 0
mymagento2docker_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
mymagento2docker_nginx_1 nginx -g daemon off; Exit 0
mymagento2docker_php_1 php-fpm Up 9000/tcp
mymagento2docker_phpmyadmin_1 /run.sh phpmyadmin Up 0.0.0.0:8080->80/tcp
ross in ~/my-magento2-docker
$ docker-compose up -d
Starting mymagento2docker_app_1
Starting mymagento2docker_data_1
mymagento2docker_mysql_1 is up-to-date
mymagento2docker_phpmyadmin_1 is up-to-date
mymagento2docker_php_1 is up-to-date
Starting mymagento2docker_nginx_1
ross in ~/my-magento2-docker
$ docker exec -it mymagento2docker_nginx_1 /bin/bash
# service nginx restart
Restarting nginx: nginxross in ~/my-magento2-docker
$ docker-compose up -d
Starting mymagento2docker_app_1
Starting mymagento2docker_data_1
mymagento2docker_mysql_1 is up-to-date
mymagento2docker_phpmyadmin_1 is up-to-date
mymagento2docker_php_1 is up-to-date
Starting mymagento2docker_nginx_1
ross in ~/my-magento2-docker
$ docker exec -it mymagento2docker_nginx_1 /bin/bash
# nginx -s reload
2016/10/05 14:07:43 [notice] 12#12: signal process started
#
Add a volumes: section to your php service in the docker-compose.yml file, map a local directory with a custom.ini file to /usr/local/etc/php/conf.d, and restart your container. Whatever valid settings in that file will override those from the main php.ini file. (Incidentally you can do the same with MySQL but not with Nginx).
This works in my own project:
php:
volumes:
- ./localpath/custom.ini:/usr/local/etc/php/conf.d/custom.ini
i think you have to reload the nginx config.
i dont know which OS your php container uses, but try inside of the container some of these:
# /etc/init.d/nginx restart
# service nginx restart
# nginx -s reload
my logical reason is, that you install php ( and start it at the same time ) and after all you copy the new config.
This is what happened :
- If you map the php.ini from host to container, make sure that file (php.ini) must exist on host before you launch the container. Otherwise, the container will not start
- Please check if docker was able to read your php.ini file (check permission, sometime docker is running as different user and can't access host file which belongs to root)
- Every time you update php.ini, you must restart php-fpm (get inside the container and restart the php-fpm to test, do not rebuild container) , and clear cache (if you enable opcache)

Categories