This is an old revision of the document!
To build and modify docker image create new folder in your Linux Docker machine
[root@localhost v1]# ll /root/Docker_Php_Deploy/v1/ total 20 -rwxrwxr-x 1 root root 239 Mar 5 13:06 000-default.conf -rwxr-xr-x 1 root root 300 Mar 5 14:24 Dockerfile -rw-r--r-- 1 root root 432 Mar 5 11:22 howto.txt -rwxr-xr-x 1 root root 242 Mar 5 14:23 index.php -rwxrwxr-x 1 root root 164 Mar 5 13:36 start-apache [root@localhost v1]#
The php:7-apache image set the Apache public directory to /var/www/html
. However, in this case, following Laravel’s conventions, we need to set it to the /var/www/public
. One way to achieve this is by setting up a virtual host configuration. Create a file called 000-default.conf
with the following contents:
# 000-default.conf <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/public <Directory /var/www> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>
Apache, by default, listens on port 80 (HTTP), this isn’t a problem when running the server on your machine. But some cloud providers require that containers use different ports.
We’ll create a script to dynamically override Apache’s port when the container starts. Create a file called start-apache with the following contents:
#!/usr/bin/env bash sed -i "s/Listen 80/Listen ${PORT:-80}/g" /etc/apache2/ports.conf sed -i "s/:80/:${PORT:-80}/g" /etc/apache2/sites-enabled/* apache2-foreground
And ensure the file is executable:
$ chmod 755 start-apache
We’re set to create a production-ready image. Create a file called Dockerfile.
We’ll use the FROM clause to use the offical php apache images as a starting point:
# Dockerfile FROM php:7-apache
The MAINTAINER instruction should contain the developer or company name.
# Dockerfile FROM php:7-apache MAINTAINER SemaphoreCI <dev@example.com> ...
Now, we need COPY the file into the image:
... COPY 000-default.conf /etc/apache2/sites-available/000-default.conf ...
Laravel requires Apache’s mod_rewrite plugin to be enabled, we can do this using the a2enmod utility. With RUN, we run commands inside the container:
... RUN a2enmod rewrite ...
To get the source files inside the container, we can use the COPY command again:
... COPY src /var/www/ RUN chown -R www-data:www-data /var/www ...
The last thing we need to do is to run the Apache server in the background. The CMD command should be used only one time in a Dockerfile, and it needs to have the following form:
CMD ["executable","param1","param2"]
We’ll call the start script we created earlier:
... CMD ["start-apache"]
The final Dockerfile should look like this:
FROM php:7-apache MAINTAINER YOUR_EMAIL_OR_ORGANIZATION COPY 000-default.conf /etc/apache2/sites-available/000-default.conf COPY start-apache /usr/local/bin RUN a2enmod rewrite # Copy application source COPY src /var/www/ RUN chown -R www-data:www-data /var/www CMD ["start-apache"]