I am trying to update my .htaccess file on a Docker container. After updating the file I need to restart Apache. Whenever I try to restart Apache: with the command service apache2 restart I get the following error:
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
Action 'start' failed.
The Apache error log may have more information.
...fail!
When I got to the error log it doesn't have any new errors.
This is what my Dockerfile looks like:
FROM ubuntu:12.04
# Install dependencies
RUN apt-get update -y
RUN apt-get install -y git curl apache2 php5 libapache2-mod-php5 php5-mcrypt php5-mysql php5-curl vim
# Install app
RUN rm -rf /var/www/ *
ADD src /var/www
# Configure apache
RUN a2enmod rewrite
RUN chown -R www-data:www-data /var/www
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
EXPOSE 80
CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]
Actually you don't need to restart Apache in order to apply changes defined in .htaccess - those are applied during runtime. If you're modifying apache config file (like virtual host definition or something in httpd.conf) you can also reload config without restarting apache using
sudo /etc/init.d/apache2 reload
It's because you are (correctly) not starting apache as a service when you docker run the container. The line:
CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]
Starts apache in the foreground.
I'm guessing you are then using docker exec to execute a shell in the container to edit the file and restart apache? If so this would explain why the second time you start apache it complains about the existing process.
I think if you are using containers in this way then you are really missing out on the benefit of containers which comes when you treat them as immutable and keep the data outside the container (either on your host or in volumes) so that you can easily replace the container.
In your case if you need to modify the .htaccess file I think it would be more normal to mount that file into the container by using a command like:
docker run -d --name apache -v $(pwd)/.htaccess:/path/to/.htaccess -p 80:80 image:tag
Then if you have to change the file and need to restart apache you can use:
docker restart apache
Although it may be worth investigating the suggestion from Charlotte Dunois that you might not even need to restart apache.
Related
We have a php application which was running fine using
Docker Image :
php:7.2-apache-stretch
We now have to make use of the below image for better performance and make the application work as it was before .
php:7.2.17-fpm-stretch
As this image does not have apache in it . I updated the Dockerfile starting from the installation of apache2 and related packages based on various forums .
There is many other steps . I have just added the instructions which I have updated in the Dockerfile.
FROM php:7.2.17-fpm-stretch
RUN apt-get update && apt-get install -y apache2 wget
RUN cd /tmp && wget http://mirrors.kernel.org/ubuntu/pool/multiverse/liba/libapache-mod-fastcgi/libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb && dpkg -i libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb;apt-get install -f
RUN a2enmod actions proxy_fcgi fastcgi
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
COPY info.php /var/www/html/info.php
COPY run.sh /app/run.sh
# EXPOSE 9000
RUN chmod 755 /app/run.sh
CMD "/app/run.sh"
The info.php contains <?php phpinfo( ); ?> . In the run.sh script , we start the php-fpm service and apache2 as below
php-fpm -D
/usr/sbin/apachectl -D FOREGROUND
previously I was trying to access the app from the port which was mapped to 9000 ( fpm ) . When I accessed the correct port where apache was running , I was able to view info.php .
The Content in the vhost.conf file.
<FilesMatch \.php$>
SetHandler "proxy:fcgi://localhost:9000"
</FilesMatch>
I got it working making a few modifications.
I used this slightly modified dockerfile:
FROM php:7.2.17-fpm-stretch
RUN apt-get update; apt-get install -y apache2 wget
RUN cd /tmp && wget http://mirrors.kernel.org/ubuntu/pool/multiverse/liba/libapache-mod-fastcgi/libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb && dpkg -i libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb;apt-get install -f
RUN a2enmod actions proxy_fcgi fastcgi
COPY run.sh /app/run.sh
RUN chmod 755 /app/run.sh
CMD "/app/run.sh"
I also added the following snippet (the same modification you did) to /etc/apache2/sites-available/000-default.conf:
<FilesMatch \.php$>
SetHandler "proxy:fcgi://localhost:9000"
</FilesMatch>
Here is the output I get:
[09-Apr-2019 21:23:06] NOTICE: fpm is running, pid 9
[09-Apr-2019 21:23:06] NOTICE: ready to handle connections
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
Also, did you try building the dockerfile without the cache? Sometimes, caching can cause issues with package installation (see here for more details). To do a clean build of an image, just use:
docker build --no-cache
I got your new configuration working using this dockerfile:
FROM php:7.2.17-fpm-stretch
RUN apt-get update && apt-get install -y apache2 wget
RUN cd /tmp && wget http://mirrors.kernel.org/ubuntu/pool/multiverse/liba/libapache-mod-fastcgi/libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb && dpkg -i libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb;apt-get install -f
RUN a2enmod actions proxy_fcgi fastcgi
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
COPY info.php /var/www/html/info.php
COPY run.sh /app/run.sh
RUN chmod 755 /app/run.sh
EXPOSE 80
CMD "/app/run.sh"
The command I ran was:
docker run -P -d --rm <php-image>
The exposed ports are:
0.0.0.0:32773->80/tcp, 0.0.0.0:32772->9000/tcp
I was able to access info.php using http://localhost:32773/info.php
I was able to access the php page . Everything was working fine . But I was looking in the wrong direction . When I run the container .
docker run -P -d --rm php:test-fpm
The output was
82071c9ff023 php:test-fpm "docker-php-entrypoi…" 2 seconds ago Up 1 second 0.0.0.0:32778->80/tcp, 0.0.0.0:32777->9000/tcp practical_mclean
I was accessing localhost:32777/info.php . But I should I accessed the 32778 where apache is exposed and localhost: 32778/info.php worked !!! .
Is there a way to avoid the port mapping of 9000. ???
I have my LAMP stack installed already before Docker's. And I am using this image to build and run my Docker's LAMP stack:
$ docker pull linuxconfig/lamp
After all are downloaded and installed:
$ docker run -it linuxconfig/lamp /bin/bash
root#2e80dfd55a6e:/# service apache2 start
[....] Starting web server: apache2AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
So at my http://172.17.0.2/, I can see this default page:
But where can I locate its location so that I can put my PHP projects in there?
This the DockerFile from that image:
FROM linuxconfig/apache
MAINTAINER Lubos Rendek <web#linuxconfig.org>
ENV DEBIAN_FRONTEND noninteractive
# Main package installation
RUN apt-get update
RUN apt-get -y install supervisor libapache2-mod-php5 php5-mysql mysql-server
# Extra package installation
RUN apt-get -y install php5-gd php-apc php5-mcrypt
# Configure MySQL
RUN sed -i 's/bind-address/#bind-address/' /etc/mysql/my.cnf
# Include supervisor configuration
ADD supervisor-lamp.conf /etc/supervisor/conf.d/
ADD supervisord.conf /etc/supervisor/
# Include PHP Info page
ADD index.php /var/www/html/index.php
# Create new MySQL admin user
RUN service mysql start; mysql -u root -e "CREATE USER 'admin'#'%' IDENTIFIED BY 'pass';";mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'#'%' WITH GRANT OPTION;";
# Allow ports
EXPOSE 80 3306
# Start supervisor
CMD ["supervisord"]
EDIT:
$ sudo docker run --name=lamp -dP -v $PWD/html:/var/www/html linuxconfig/lamp
c2d1687aef21f8a12a7fbb31bf8cf71c1e5adabf381bc6d70e8804c6663f0bc0
And:
$ sudo docker port lamp
80/tcp -> 0.0.0.0:32769
3306/tcp -> 0.0.0.0:32768
I go to my browser at: http://172.17.0.2:32769/
I get this error:
See if this article can help: "LAMP ( Linux, Apache, MariaDB, PHP ) stack Docker image deployment"
Save index.php file and within a new html directory.
Alternatively, html directory may contain your desired PHP application:
$ mkdir html
$ vi html/index.php
$ ls html/
index.php
At this stage we are ready to deploy “linuxconfig/lamp” docker image:
sudo docker run --name=lamp -dP -v $PWD/html:/var/www/html linuxconfig/lamp
That means you are mounting your host directory html into the linuxconfig/lamp container folder /var/www/html. (see "Mount a host directory as a data volume")
I have created a docker image, tested it locally. All working well.
After that I used the same Dockerfile - built it and run it on EC2.
The Apache webserver is listening to the connections, I have opened firewall in EC2 machine security settings, but if I try to navigate to the webisite, it seems to be opening for approx 2.5 minutes.
And then the website OPENS. Each navigation step takes 2.5 minutes.
Later:
I have tried to replicate this environment on Windows machine with Virtual Box and it has exactly same issue. Website would take long time and eventually open. It also broke connect to another machine that that was completely not docker related (Ubuntu dev box)
Can anybody advice something?
Here is docker file:
FROM ubuntu
RUN apt-get update -y
RUN apt-get install -y apache2 php5 vim libapache2-mod-php5 php5-mcrypt
RUN echo "ServerName localhost" >> /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.pid
EXPOSE 80
RUN ln -sf /dev/stderr /var/log/apache2/error.log
CMD /usr/sbin/apache2ctl -D FOREGROUND
As illustrated in "Debug your PHP in Docker with Intellij/PHPStorm and Xdebug", it is possible to debug php from within a container.
The OP activated it directly in php.ini:
[xdebug]
zend_extension="/usr/lib/php5/20121212/xdebug.so"
xdebug.remote_enable=on xdebug.remote_handler=dbgg
xdebug.remote_host=172.17.0.1
xdebug.remote_port=9000
xdebug.remote_connect_back=on
xdebug.remote_autostart=on
xdebug.remote_log="/var/log/xdebug/xdebug.log"
Removing that module was enough to bring the response time down.
I'm beginner with Docker, and I'm trying to build my own image: Ubuntu + Nginx + PHP.
So, I have a directory called test. Inside directory two other directories, app and sites-enabled. Also, there's a Dockerfile, with content:
FROM ubuntu:trusty
RUN apt-get update && \
apt-get install -y nginx php5-fpm php5-mysql php-apc php5-imagick php5-imap php5-mcrypt php5-gd libssh2-php && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
ADD sites-enabled/ /etc/nginx/sites-enabled/
ADD app/ /app/
EXPOSE 80
CMD ["php5-fpm", "-c", "/etc/php5/fpm"]
CMD ["/usr/sbin/nginx"]
I build this image successfully. Then I create container with docker run -d image_name. I get the ID, and then I run docker inspect -f "{{.NetworkSettings.IPAddress}}" ID in order to get the IP address of the container.
I need this IP address, because I also run HAProxy in another container, so I can configure it to point to the right localtion.
So, both HAProxy and container with PHP app are running OK. HAProxy is pointing at the right application. PHP application files are uploaded at the right location inside the container.
But, Nginx doesn't execute PHP. Instead, when I try to access the application, I just get a downloaded file with my index.php PHP code.
What could be the problem? Please help.
My first guess was that I'm doing something wrong in Dockerfile when I run php5-fpm. I've tried few different ways, but non of them seem to work.
One CMD only.
If you need "services", look into supervisord or runit.
I'm trying to run php built-in server (php -S localhost:8080) via docker, I cannot access site from the host though - I always end up with Connection reset.
Here's a simple Dockerfile I build on:
FROM centos:centos6
RUN rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
RUN rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
RUN rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
RUN yum --enablerepo=remi,remi-php55 install -y php php-opcache php-cli php-pear php-common && yum clean all
RUN php -r "readfile('https://getcomposer.org/installer');" | php
RUN echo "date.timezone = Europe/Prague" >> /etc/php.ini
RUN mv composer.phar /usr/bin/composer
RUN php -r "eval('?>'.file_get_contents('http://backend.bolt80.com/piecrust/install'));"
RUN mv piecrust.phar /usr/bin/chef
CMD ["/bin/bash"]
Is it even possible to run this server with docker? While trying to make it work, I found out that when nginx was installed and set to listen on this very port, it is accessible from the host. PHP built-in server seems to be hidden from the host, thus not able to serve any requests though.
Anyone was successful making this work?
If from within the docker container you start your webserver with php -S localhost:8080 then the webserver will only accept connections originating from the docker container itself.
To be able to communicate with your webserver from the docker host you need to make two changes:
in your Dockerfile, add EXPOSE 8080, or when running the container add -p 8080 to the docker run command line. This will tell the docker host that your container has a program which expects communication on port 8080
start the webserver with php -S 0.0.0.0:8080 so it also accepts connections from outside of the docker container itself
Once you have a container running with those changes, open up a new terminal on the docker host and use the docker ps command to see what port on the docker host is forwarded to port 8080 in the container. For instance:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fbccf4058b07 test:latest "php -S 0.0.0.0:8080 4 minutes ago Up 4 minutes 0.0.0.0:49153->8080/tcp sad_hawking
In this example port 49153 of the docker host is to be used. Then query your webserver to validate you can communicate with it:
$ curl http://localhost:49153
For docker compose
services:
api:
build: ./api
command: php -S 0.0.0.0:80
ports:
- 80:80
with dockerfile, placed in ./api folder
FROM php:apline
WORKDIR /
COPY . .