Forbidden in official image Docker PHP - php

I'm testing the official image docker PHP:
docker run -d -p 8000:80 --name test php:7-apache
Then, I test http://localhost:8000, I find this:
Forbidden
You don't have permission to access / on this server.
What am I doing wrong?

docker run -d -p 8000:80 --name test php:7-apache -v "$PWD":/var/www/html
Go to your project folder and run the above command or replace $PWD with your project directory. -v will sync your project to the docker container.
Or create a Docker file and use copy to run your project within the container.
Any one of the above will solve your problem.
The probable reason for the error is the fact that there is no project to render.

There's no hello world included with this image, /var/www/html/ is empty so there's no content to view. You need to bundle your own code in as the page on this image instructs:
We recommend that you add a custom php.ini configuration. COPY it into
/usr/local/etc/php by adding one more line to the Dockerfile above and
running the same commands to build and run:
FROM php:7.0-apache
COPY config/php.ini /usr/local/etc/php/
COPY src/ /var/www/html/

If you don't have files, it's behavior is normal.
But if you have, probabily is problem of permissions.
Error 403 is likely permission problem.
Inside container, run command:
$ ls -lsh
If letter 'r' (read) don't, you can try:
$ chmod +r <file>

Edit/add the .htaccess rewrite rule:
.htaccess
...
RewriteRule /
...

Related

"no such file or directory" when passing full path to Docker image, but works with relative path to same folder

From a PHP page in Apache I'm trying to run a docker command that will generate a PDF. I've added both my user and the www-data user to the docker group so they can execute a docker command without sudo.
This works within a PHP file that exists at the /home/my_user/projects/my_project/public folder:
system("docker run --rm -v `pwd`:/app -w /app weasyprint:51 ./healthcheck.htm /weasyprint_test.pdf");
But when I specify the full path (which I've verified is correct), it doesn't work:
system("docker run --rm -v `pwd`:/app -w /app weasyprint:51 /home/my_user/projects/my_project/public/healthcheck.htm /home/my_user/projects/my_project/public/weasyprint_test.pdf");
I see this in the PHP error log when I run the second version:
su-exec: /home/my_user/projects/my_project/public/healthcheck.htm: No such file or directory
For the life of me, I can't figure out why the first version would work and the second wouldn't because they are referring to the same path, just via different syntax. Please let me know if you have any ideas.
If it helps, I'm using the this Docker image for Weasyprint: https://hub.docker.com/r/minidocks/weasyprint
On the Docker VM run ls /home/ and I would wager the directory you expect isn't there. The user referenced in your path probably doesn't exist on the VM, only on your host machine.
The relative path works because it's relative to the root directory provided.

Is there anyway to start docker container without a service?

I have a php container which needs php-fpm to be started everytime I start the container . Now because of a wrong configuration in php-fpm config file , fpm does not gets started and so , container cannot start. Is there anyway that I can start the container without php-fpm so that I can fix the config file?
The container error is as follows :
[04-Sep-2020 13:47:30] ERROR: [/usr/local/etc/php-fpm.conf:7] value is NULL for a ZEND_INI_PARSER_ENTRY
[04-Sep-2020 13:47:30] ERROR: failed to load configuration file '/usr/local/etc/php-fpm.conf'
[04-Sep-2020 13:47:30] ERROR: FPM initialization failed
There are two ways to fix the image. Since I can't find image digitalocean/php, I'll use php:7.4-fpm in my example.
First way:
Copy file from the container and use it to build your own image:
Create Dockerfile:
FROM php:7.4-fpm
COPY ./php-fpm.conf /usr/local/etc/php-fpm.conf
Then:
docker run --detach --name php php:7.4-fpm tail -f /dev/null
docker cp php:/usr/local/etc/php-fpm.conf php-fpm.conf
docker stop php
docker rm -v php
# Edit php-fpm.conf
docker build --tag myphp-fm .
docker run --detach --name php myphp-fpm
and you get running container based on the fixed image.
Second way:
Run a shell using the broken image, fix the file and create a new image using the shell container
docker run -it --name php php:7.4-fpm bash
# Edit /usr/local/etc/php-fpm.conf
# If you install any additional tools remember to remove them afterwards
# and clean any cache's
# Once you're done exit the shell, thus stopping the container
docker commit -a "you" -m "/usr/local/etc/php-fpm.conf fix" php myphp-fpm
docker stop php
docker rm -v php
docker run --detach --name php myphp-fpm
and again you get running container based on the fixed image.
Of course, you can run your new image in whatever way you run the original image in the beginning.
I recommend the first way as it's way easier to edit the file outside the container.

How to access httpd.conf in Docker from a php image that included apache by itself?

Problem
I'm able to find the httpd.conf when I run a container from the httpd image, but I can't find that file when I run the container from a PHP image that includes apache, like the image php:7.4.2-apache-buster
The php image was ran this way, also $(pwd) is pointing to the proyect directory
docker container run -d -p 80:80 \
--name containerName \
-v $(pwd):/var/www/html \
php:7.4.2-apache-buster
Tried
I used docker container exec -it normalApache bash in the normal httpd image, in which I was able to find the httpd.conf file in /usr/local/apache2/conf
But when I try to do the exact same thing in the apache included with the php image, I can't find that file because the directory apache2 in /usr/local doesn't even exit
Whatever you are looking for, the Dockerfile for that container can be found at https://github.com/docker-library/php/blob/703a3d0a4e4c149bfd62fc3e7b71645f9496b178/7.4/buster/apache/Dockerfile, and in line 52 it uses /etc/apache2 as the configuration directory
51 ##<autogenerated>##
52 ENV APACHE_CONFDIR /etc/apache2
53 ENV APACHE_ENVVARS $APACHE_CONFDIR/envvars

Dockerizing a command line application

I have created a command line application using symfony 3.4 which doesn't need to display any web page.
I generally run the commands like following:
php bin/console MY_COMMAND_NAME
I want to dockerize the application and share it with others, so inside the root directory of my project I created a docker-compose.yml file, which looks like following:
version: "3.3"
services:
web:
image: php:7.3-cli
Then I ran docker-compose up, after that I checked the PHP version by the following command and it showed my the correct version:
docker run php:7.3-cli php -v
However, when I ran docker ps, it didn't show any container running.
My question is how to run the commands inside my project root directory. FYI, I am using Docker Toolbox, on windows 10 Home Edition and my project location is:
C:\Users\{my_user_name}\Desktop\folder_1\folder_2
The docker container need to have a long running process defined in CMD to stay running. php-cli is not that. If you run composer up, you'll see something like this:
$ docker-compose up
Creating network "tempphpdocker_default" with the default driver
Pulling web (php:7.3-cli)...
7.3-cli: Pulling from library/php
b8f262c62ec6: Pull complete
a98660e7def6: Pull complete
4d75689ceb37: Pull complete
639eb0368afa: Pull complete
2cdbfdb779b1: Pull complete
e0b637fa9606: Pull complete
da7333b0ef25: Pull complete
01d65ff46009: Pull complete
673e50bed3b9: Pull complete
bf6c6e34305d: Pull complete
Digest: sha256:1453f5ef0d4d1d424ed8114dd90a775bdec06cc6fb3bbae9521dcb4ca0c8ca90
Status: Downloaded newer image for php:7.3-cli
Creating tempphpdocker_web_1 ...
Creating tempphpdocker_web_1 ... done
Attaching to tempphpdocker_web_1
web_1 | Interactive shell
web_1 |
tempphpdocker_web_1 exited with code 0
The exit code is 0. This means your command in the docker image php:7.3-cli has successfully run and finished.
To properly dockerize your applicaiton, you should override this by writing you own docker file with proper COPY calls that bundle your CLI program into it. Your Dockerfile should probably look something like this:
FROM php:7.3-cli
RUN mkdir -p /opt/workdir/bin
RUN mkdir -p /opt/workdir/vendor
COPY bin/ /opt/workdir/bin
COPY vendor/ /opt/workdir/vendor
WORKDIR /opt/workdir
CMD php ./bin/console COMMAND
You can simply build and run this Dockerfile, or you if you prefer docker-compose, you can define docker-compose.yml in the same folder as the Dockerfile:
version: "3.3"
services:
web:
image: php-custom
build: ./
Please noted that a dockerized application can only access files and folder in the docker image. You should bind volumes of your local file system to the container before it can actually work on your filesystem.
Quick and dirty fix to keep you container running just override the container command in docker-compose.
version: "3.3"
services:
web:
image: php:7.3-cli
command: tail -f /dev/null
when you run docker-compose up it will keep the docker container but it will do not thing, just will give away to run command inside container.
docker exec -it php-cli_web_1 ash
My question is how to run the commands inside my project root
directory.
As mentioned by #David, you need to mount your host project to the container in docker-compose.
For instance
your project is placed on the host /home/myporject, mount the project within docker-compose and it will be available inside the container. then you can update the command of your docker-compose to run the script.
keep in mind
The life of container is the life of docker-compose command
When the execution completed your container will be die after execution. so your container will run until the php:7.3-cli /app/your_script.php this script completed.
version: "3.3"
services:
web:
image: php:7.3-cli
command: php:7.3-cli /app/your_script.php
volumes:
- /home/myporject:/app

How can I overwrite a file through Dockerfile in docker container?

I have to overwrite a file through Dockerfile. In particular on an Ubuntu container with Apache and PHP and I have to overwrite the file php5-cgi.conf.
I tried to use the following command:
COPY php5-cgi.conf /etc/apache2/conf-enabled/php5-cgi.conf
but I had the error: File already exists
I have also tried to use the following command
RUN cp -f php5-cgi.conf /etc/apache2/conf-enabled/
but the file is not copied when the container is running, Is there any advice on this?
Drop the file name from the destination:
COPY php5-cgi.conf /etc/apache2/conf-enabled/
The destination is an absolute path (not filename), or a path relative to the work directory, into which the source will be copied inside the destination container.
I just tested following docker file with no problem.
from debian:jessie
COPY debian_version /etc/debian_version
As PolarisUser stated in comments, you need to put debian_version in the same folder as dockerfile or use absolute path.
Another way would be mounting the file when running the container.
docker run -d -v php5-cgi.conf:/etc/apache2/conf-enabled/php5-cgi.conf --name your_container_name <imagename:tag> <startup command>
docker cp "yourfilename" "containername":/destination
Below is a working example:
docker cp config.json bigdataapp:/app/src/bigdatapp/wwwroot/assets/config/config.json

Categories