DOCKER: ERR_EMPTY_RESPONSE - Mac - php

I'm new to Docker and just made my first steps.
I wanted to play a little bit with docker and was about configuring a Dockerfile for yii:
FROM php:7.2.3-apache
RUN curl -sS https://getcomposer.org/installer | php && \
mv composer.phar /usr/local/bin/composer
RUN apt-get update && apt-get install -y git unzip zip
EXPOSE 8080
RUN composer create-project --prefer-dist yiisoft/yii2-app-basic test
Then I run the container with:
docker container run -d --name test -p 8080:8080 test-yii
When I go to localhost:8080 i got an ERR_EMPTY_RESPONSE.
This is the network result of docker container inspect:
"Ports": {
"80/tcp": null,
"8080/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8080"
}
]
},
I appreciate every hint that helps me to solve this issue!
Edit: I forgot to mention that i connect to the container and then run php yii serve to test if Yii runs and this results in the above described problem.

YII is not started when you create a container. That's because it isn't defined in Dockerfile. Only apache starts there, because that's come from the image php:7.2.3-apache.
The correct Dockerfile is:
FROM php:7.2.3-apache
RUN curl -sS https://getcomposer.org/installer | php && \
mv composer.phar /usr/local/bin/composer
RUN apt-get update && apt-get install -y git unzip zip
EXPOSE 8080
RUN composer create-project --prefer-dist yiisoft/yii2-app-basic test
CMD test/yii serve 0.0.0.0
Now, CMD layer with yii overlaps CMD layer from php:7.2.3-apache image.
If you want to start both yii and apache inside a container you should look to these pieces of advice.
Update to the Edit section in the question:
You need to run php yii serve 0.0.0.0. Otherwise yii binds to localhost:8080 and is only accessible inside a container

Related

Git Clone and composer install on docker

I hope you are all safe and well,
I have a question regarding docker and cloning from GitHub.
When I run docker compose up, everything works as it should, part from when I get to the cloning stage. It goes through the process of cloning the master of repo into the folder but does not actually create it, so when I get to the point where I want to install composer via the RUN composer install command, it fails because it actually hasn't cloned the repo and I am guessing this is because it is waiting for the whole build process to finish?
If that is the case, is there a command I can use in the Dockerfile to make it wait or even depend on there being a cloned repo in the root of the app?
If not, can I put composer as a separate container and have it depend on the php service to clone the repo?
Here is a copy of my php docker file.
FROM php:7.3
RUN apt-get update -y && apt-get install -y openssl zip unzip git
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo pdo_mysql
WORKDIR /app/
RUN mkdir CREATEDFOLDER
RUN cd CREATEDFOLDER
RUN git init
RUN git remote add origin https://USERNAME:PASSWORD#github.com/USER/REPO.git
RUN git pull origin dev
WORKDIR /app/CREATEDFOLDER/
RUN composer install
EXPOSE 8000
CMD php artisan serve --host=0.0.0.0
Thanks in advance :)

How to run composer as non root user on official docker php image

I am using the official php:alpine https://github.com/docker-library/php/blob/master/7.2/alpine3.7/fpm/Dockerfile as my base image. My projects are basically composer based project. So I installed composer on it like below.
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin -- --filename=composer
When I install any packages using composer install it runs as root as the main php process runs as root. So how can I run main php process as root and composer as another non root user deploy?
Update-1
My dockerfile is like below. So as you can see I am not installing composer packages inside dockerfile rather I install those packages on container like docker exec -it php composer install.
FROM php:7.2-fpm-alpine
..........................
..........................
..........................
RUN set ex \
# Install Composer( Requires git )
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin -- --filename=composer \
ENTRYPOINT ["/entrypoint.sh"]
CMD ["php-fpm"]
I was trying to achieve it like https://unix.stackexchange.com/questions/476155/how-to-pass-multiple-parameters-to-su-user-c-command but in vain.
Incase anyone wants to achieve the same follow instructions below
I added a USER in dockerfile so that composer will be run as user deploy in future. But php-fpm needs to be run as root. So I set a setuid bit on the php-fpm binary. And now everything got resolved.
RUN chmod u+s /usr/sbin/php-fpm7
USER deploy
ENTRYPOINT ["/entrypoint.sh"]
CMD ["php-fpm7","-F"]

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

Permission Issue in Docker container for Symfony2

I'm tring to create an Docker image to bootstrap Symfony project.
Here is my Dockerfile:
FROM php:7-apache
LABEL Description = "This image is used to start Symfony3 project"
ENV DIRPATH /var/www/html
# apt-get command
RUN apt-get update && apt-get install -y \
vim \
git
RUN apt-get install -y zlib1g-dev && docker-php-ext-install zip
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
# Install the Symfony Installer
RUN curl -LsS http://symfony.com/installer -o /usr/local/bin/symfony
RUN chmod a+x /usr/local/bin/symfony
# Create the php.ini file
RUN cp /usr/src/php/php.ini-development /usr/local/etc/php/php.ini
The build and the container creation works well but I have a permission issue in my container.
When I'm going to my app_dev.php, I have this message:
You are not allowed to access this file. Check app_dev.php for more information.
Apparently, I can access this file only with localhost.
Also, PHP can't delete or create anything in my container.
For exemple I have the following error when I'm running:
$php app/console cache:clear
Failed to remove directory "/var/www/html/app/cache/dev_old/doctrine
How can I solved that in my Dockerfile?
Finally found it after weeks:
Add that in you Dockerfile. It solved the permission issue.
# Workaround for write permission on write to MacOS X volumes
# See https://github.com/boot2docker/boot2docker/pull/534
RUN usermod -u 1000 www-data

Docker and symfony

I'm struggling with Docker.
I'm tring to create an image to work on symfony project and to learn Docker in the same time.
Here is my Dockerfile:
FROM php:7-apache
LABEL Description = "This image is used to start Symfony3 project"
ENV DIRPATH /var/www/html
# apt-get command
RUN apt-get update && apt-get install -y \
vim \
git \
&& apt-get clean
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
# Install the Symfony Installer
RUN curl -LsS http://symfony.com/installer -o /usr/local/bin/symfony
RUN chmod a+x /usr/local/bin/symfony
I build the image with the command:
docker build -t symfony .
Works well! Cool!
I'm create a container with:
docker run --name symfony -d -v "$PWD":/var/www/html -p 80:80 symfony
Works well also. The web server is running on the good port.
I can go in my container with:
docker exec -ti symfony bash
But when I'm trying to do a composer update, I have some errors:
Failed to download symfony/symfony from dist: Could not decompress the archive, enable the PHP zip extension.
A php.ini file does not exist. You will have to create one.
How can I create the php.ini in Dockerfile?
I also think that I have an issue with permission.
When I'm trying to the web/app_dev.php I have this message:
You are not allowed to access this file. Check app_dev.php for more information.
You can ADD a custom php.ini configuration specifing it in the dockerfile,
As Example, you can take a look at this repo for this example:
dokerfile
# install a few more PHP extensions
RUN apt-get update && apt-get install -y php5-imagick php5-gd php5-mongo php5-curl php5-mcrypt php5-intl
# copy a custom config file from the directory where this Dockerfile resides to the image
COPY php.ini /etc/php5/fpm/php.ini
You can find various approach and various sample on the net.
Hope this help
Next to the missing php.ini file you should also install zip so you can download from dist, i.e.
RUN docker-php-ext-install zip
Which will install and enable the PHP zip extension which is requested in your error message.

Categories