Configuring a PHP Apache Web Service Container at Build Time

I am building a PHP Apache Web Service Docker Container Prototype.

  1. The first and simplest way was to create a Container with the official Image published by WordPress
    with a simple Dockerfile like:
FROM wordpress:latest

Which builds but than fails to run:

# docker run -it wordpress_local apache2-foreground
WordPress not found in /var/www/html - copying now...
Complete! WordPress has been successfully copied to /var/www/html
AH00534: apache2: Configuration error: No MPM loaded.

which is a Known Error unable to be fixed. So the Image is broken.
Other Images like php7-apache2 also produce the same Error.

  1. Unable to find a prebuild Image that would actually run I started to build an Image from scratch.
    It contains
  • Alpine Linux 3.12
  • Apache 2.4
  • PHP 7.3

with the Dockerfile:

# cat Dockerfile
FROM alpine:3.12
RUN apk add apache2 php7 php7-apache2 
ADD html/ /var/www/html/
WORKDIR /var/www/html/
CMD ["httpd", "-DNO_DETACH -DFOREGROUND -e debug"]

and a docker-compose.yml:

# cat docker-compose.yml
version: '3'
services:
  web:
    image: php_web_alpine
    build: .
    ports:
     - "8081:8081"

This builds nicely:

# docker build -t php_web_alpine .
Sending build context to Docker daemon  7.68 kB
Step 1/5 : FROM alpine:3.12
 ---> a24bb4013296
Step 2/5 : RUN apk add apache2 php7 php7-apache2
 ---> Using cache
 ---> bf59e0c43f1f
Step 3/5 : ADD html/ /var/www/html/
 ---> 0fe4bfd871b2
Removing intermediate container cec9de242174
Step 4/5 : WORKDIR /var/www/html/
 ---> 03d3fe0a077f
Removing intermediate container b1763eb3e56b
Step 5/5 : CMD httpd -DNO_DETACH -DFOREGROUND -e debug
 ---> Running in 4ca69abc9f52
 ---> e3a33ae6e028
Removing intermediate container 4ca69abc9f52
Successfully built e3a33ae6e028

But than does not run because of a simple Configuration Error:

# docker-compose up 
Recreating phpalpine_web_1 ... done
Attaching to phpalpine_web_1
web_1  | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.20.0.2. Set the 'ServerName' directive globally to suppress this message
phpalpine_web_1 exited with code 0

It needs just to fill the httpd.conf Configuration File with the correct values.

So, how can I fill the Web Service Configuration Files (Apache and PHP and others, etc …) at Build Time to get a nice reproducible Build ?

Answer

Nothing could be more easy, add the COPY directive to your Dockerfile.

it will look like something like this:

COPY MyWebConf.conf /etc/apache2/sites-enabled/

It will copy and overwrite any existing configuration with the same name.

If you want to test the configuration before perform a build of the image mount it in the run time of the container.

Attribution
Source : Link , Question Author : Bodo Hugo Barwich , Answer Author : AtomiX84

Leave a Comment