User Tools

Site Tools


Sidebar

docker:build_create_run_image_phpinfo

Build and modify PHP-7-Apache Docker Image

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:

#Donwloading and creating docker image from GitHub called php:7-apache
FROM php:7-apache
 
#Your email and information
MAINTAINER YOUR_EMAIL_OR_ORGANIZATION
 
#Copy local created file 000-default.conf to image-php-server right location
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
 
#Copy start-apache script to executible linux directory
COPY start-apache /usr/local/bin
 
#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
 
# Copy application source and permissions for source folder
COPY index.php /var/www/public/
RUN chown -R www-data:www-data /var/www
 
CMD ["start-apache"]

Our folder contains a Dockerfile, a 000-default.conf and start-apache. The docker build command will build the Dockerfile inside the current directory:

[root@localhost v1]# docker build -t php-7-apache:v1 .
Sending build context to Docker daemon  6.144kB
Step 1/8 : FROM php:7-apache
7-apache: Pulling from library/php
68ced04f60ab: Pull complete
1d2a5d8fa585: Pull complete
5d59ec4ae241: Pull complete
d42331ef4d44: Pull complete
408b7b7ee112: Pull complete
570cd47896d5: Pull complete
2419413b2a16: Pull complete
2c7832852643: Pull complete
8b0b209a25bc: Pull complete
46011418685f: Pull complete
68be3748ea55: Pull complete
4e3af655ec1e: Pull complete
9f579d3b7159: Pull complete
Digest: sha256:c496c0f962eaaea0f52d9c068bf331fe477703d4657f99b955a2a35a4c3486c4
Status: Downloaded newer image for php:7-apache
 ---> d753d5b380a1
Step 2/8 : MAINTAINER YOUR_EMAIL_OR_ORGANIZATION
 ---> Running in 39e672a90b5a
Removing intermediate container 39e672a90b5a
 ---> 4d25a2d00117
Step 3/8 : COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
 ---> 19a2cdc56a9f
Step 4/8 : COPY start-apache /usr/local/bin
 ---> 603ec78bbed1
Step 5/8 : RUN a2enmod rewrite
 ---> Running in a7fff2718075
Enabling module rewrite.
To activate the new configuration, you need to run:
  service apache2 restart
Removing intermediate container a7fff2718075
 ---> 9bdfd2fefddd
Step 6/8 : COPY index.php /var/www/public/
 ---> 1c116f043a28
Step 7/8 : RUN chown -R www-data:www-data /var/www
 ---> Running in 84452265396e
Removing intermediate container 84452265396e
 ---> d059277e4af0
Step 8/8 : CMD ["start-apache"]
 ---> Running in a82d06ffb356
Removing intermediate container a82d06ffb356
 ---> 6f4b2f7f4ec3
Successfully built 6f4b2f7f4ec3
Successfully tagged php-7-apache:v1

If we list our Docker images now, we’ll see our new built image:

[root@localhost v1]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
php-7-apache        v1                  6f4b2f7f4ec3        2 hours ago         414MB

Finally run a container on port 80:

[root@localhost v1]# docker run -d -p 80:80 php-7-apache:v1
051f5da1a58e483ff672ef32b7a6124a809893b31a3caa27c907a100e8d935e9

Check is container running:

[root@localhost v1]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
051f5da1a58e        php-7-apache:v1     "docker-php-entrypoi…"   2 hours ago         Up 2 hours          0.0.0.0:80->80/tcp   jovial_vaughan

Result :

docker/build_create_run_image_phpinfo.txt · Last modified: 2020/03/05 16:20 by admin