I started my attempt to package PHP after reading this. There they had used a docker-compose file to achieve the required result. I wanted to do it without using the docker-compose file. we also need php-fpm for it for that I read an article here to find out the command needed to install php-fpm. So having seen all these tutorials, I thought it is simpler than I expected. well, this is how simple it was.
I realized that the docker file for apache I created in my last post won't be enough since it wont forward php files to the php container. This is how I set up my folder structure
.
├── apache
| ├── dockerfile
| └── demo.apache.conf
├── php
| └── dockerfile
└── public_html
├── index.php
└── test.html
This is how I changed the apache dockerfile. I modified the file so that it would replace the default 000-default.conf with my custom demo.apache.conf using the COPY command in the dockerfile. This is how my dockerfile for apache looks like
FROM ubuntu
RUN ["apt-get","update"]
RUN ["apt-get","install","apache2","-y"]
RUN apache2ctl start
RUN a2enmod proxy_fcgi
COPY demo.apache.conf /etc/apache2/sites-available/000-default.conf
EXPOSE 80
ENTRYPOINT apache2ctl start && tail -f /dev/null
RUN ["apt-get","update"]
RUN ["apt-get","install","apache2","-y"]
RUN apache2ctl start
RUN a2enmod proxy_fcgi
COPY demo.apache.conf /etc/apache2/sites-available/000-default.conf
EXPOSE 80
ENTRYPOINT apache2ctl start && tail -f /dev/null
I have just enabled the proxy_fcgi module and copied the conf file and this is the demo.apache.conf file
ServerName localhost
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1
<Directory /var/www/html/>
DirectoryIndex index.php index.html
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
With this, I have set up the dockerfile for apache. I removed old containers using
docker system prune -a
Next, I moved on to creating the dockerfile for php container.
FROM ubuntu
RUN ["apt-get","update"]
RUN apt-get install php7.2-fpm php7.2-mysql php7.2-mbstring php7.2-curl php7.2-dom -y
EXPOSE 9000
ENTRYPOINT service php7.2-fpm start && tail -f /dev/null
RUN ["apt-get","update"]
RUN apt-get install php7.2-fpm php7.2-mysql php7.2-mbstring php7.2-curl php7.2-dom -y
EXPOSE 9000
ENTRYPOINT service php7.2-fpm start && tail -f /dev/null
It seems simple right. That's because this doesn't work. I felt ok so now to run this and I ran them with the following commands
docker build -t apache2 .
docker build -t phptest .
docker run -d -v /home/user/Desktop/DockerTesting/public_html:/var/www/html -p 100:80 --name apachecontainer apache2
docker run -d -v /home/user/Desktop/DockerTesting/public_html:/var/www/html -p 9000:9000 --name phpcontainer phptest
So as I was searching the internet for a solution. I found that I have to make php-fpm listen to port 9000 and that the setting could be found at www.conf file. So now I had to find the www.conf file in the container. So I used
docker exec -it phpcontainer /bin/bash
to establish a connection to the container and after a bunch of cd and ls commands. I found the www.conf file at '/etc/php/7.2/fpm/pool.d/' so I copied the file out of the container using
docker cp phpcontainer:/etc/php/7.2/fpm/pool.d/www.conf www.conf
sure enough, the listen was not set to a port. So I changed that line to 'listen = 9000' and saved the www.conf file inside the php folder and modified the dockerfile as
FROM ubuntu
RUN ["apt-get","update"]
RUN apt-get install php7.2-fpm php7.2-mysql php7.2-mbstring php7.2-curl php7.2-dom -y
COPY www.conf /etc/php/7.2/fpm/pool.d/www.conf
EXPOSE 9000
ENTRYPOINT service php7.2-fpm start && tail -f /dev/null
RUN ["apt-get","update"]
RUN apt-get install php7.2-fpm php7.2-mysql php7.2-mbstring php7.2-curl php7.2-dom -y
COPY www.conf /etc/php/7.2/fpm/pool.d/www.conf
EXPOSE 9000
ENTRYPOINT service php7.2-fpm start && tail -f /dev/null
Now I removed the phpcontainer and the phptest image and rebuilt them using the above mentioned commands. Now with all the hope in the world, I opened http://localhost:100/ and again I was greeted with the same error. I had no idea what went wrong or how to fix it. I tried again but this time using docker-compose
I set the version as 3.7 based on the documentation. And ran the file using
and then modified my docker-compose file as
Trial Using Docker-Compose:
Now that my attempt to package PHP and Apache in separate containers without docker-compose failed miserably. I decided to try it using docker-compose and this is my folder structure. Everything is the same as before except for the docker-compose.yml file
.
├── apache
| ├── dockerfile
| └── demo.apache.conf
├── php
| ├── dockerfile
| └── www.conf
├── public_html
| ├── index.php
| └── test.html
└── docker-compose.yml
I created the docker file based on the example given here.
version: "3.7"
services:
php:
build: './php/'
networks:
- backend
volumes:
- ./public_html/:/var/www/html/
apache:
build: './apache/'
depends_on:
- php
networks:
- backend
ports:
- "8080:80"
volumes:
- ./public_html/:/var/www/html/
networks:
backend:
services:
php:
build: './php/'
networks:
- backend
volumes:
- ./public_html/:/var/www/html/
apache:
build: './apache/'
depends_on:
- php
networks:
- backend
ports:
- "8080:80"
volumes:
- ./public_html/:/var/www/html/
networks:
backend:
I set the version as 3.7 based on the documentation. And ran the file using
docker-compose up -d
and this time I was able to access the php file at http://localhost:8080. I was confused I used the same dockerfiles as before but now everything works like magic. So I thought either the way I am building them is wrong or else the way I was running them is wrong. So I decided to test it out. First I stopped them using
docker-compose down
First I thought of testing the way I build the images. I built the images the same way I did last time
docker build -t apache2 ./apache
docker build -t phptest ./php
and then modified my docker-compose file as
version: "3.7"
services:
php:
image: "phptest"
networks:
- backend
volumes:
- ./public_html/:/var/www/html/
apache:
image: "apache2"
depends_on:
- php
networks:
- backend
ports:
- "8080:80"
volumes:
- ./public_html/:/var/www/html/
networks:
backend:
services:
php:
image: "phptest"
networks:
- backend
volumes:
- ./public_html/:/var/www/html/
apache:
image: "apache2"
depends_on:
- php
networks:
- backend
ports:
- "8080:80"
volumes:
- ./public_html/:/var/www/html/
networks:
backend:
Now I started them 'docker-compose up -d'. And this time it worked too I was able to access the php page. So now that I know that building the image was not the problem. I have to modify the run command and play with it a bit
Trial Without Docker-Compose:
Since I was able to run both the containers through docker-compose without any issues. I was trying to run them without docker-compose. The first thing I could notice from the docker-compose.yml file is the network that gets created. So I tried this
docker build -t apache2 .
docker build -t phptest .
docker network create testnet
docker run -d -v /home/user/Desktop/DockerTesting/public_html:/var/www/html -p 9000:9000 --network testnet --name phpcontainer phptest
docker run -d -v /home/user/Desktop/DockerTesting/public_html:/var/www/html -p 100:80 --network testnet --name apachecontainer apache2
I created a new network based on the documentation and added the containers to this network. And it didn't work either. Then I noticed that in the yml file the port of the php container was not mapped so I also tried this for the php container
docker run -d -v /home/user/Desktop/DockerTesting/public_html:/var/www/html --network testnet -- name phpcontainer phptest
Nope, didn't work out either. I was at my wit's end that I even posted a StackOverflow question. It was then that I thought of inspecting the containers created with docker-compose and docker-run. So I ran both the set of containers. The two working ones with docker-compose and the other two with docker run. Then inspected the containers using the command
docker container inspect apachecontainer
docker container inspect phpcontainer
and also the other two containers. Then I started to go through them comparing the working versions of apache and php with the non-working versions of them. I found that the working versions had a few docker-compose labels which seemed to be unimportant. But then I found something interesting under a node that says network both the working versions had an alias as apache and php respectively. It was then that it struck me the demo.apache.conf file I was using had the following line
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1
The fcgi://php:9000/ referred to the alias "php". So I set a network alias for both the containers and ran them with the commands
docker run -d -v /home/user/Desktop/DockerTesting/public_html:/var/www/html --network testnet --network-alias php --name phpcontainer phptest
docker run -d -v /home/user/Desktop/DockerTesting/public_html:/var/www/html -p 100:80 --network testnet --network-alias apache --name apachecontainer apache2
And this time I was able to access the PHP pages by going to http://localhost:100. And finally, I was able to achieve what I had wanted to do. Next, I plan to install MySQL in a separate container and link it with these two. I will write about it in my next post
No comments:
Post a Comment