I figured out a way to get Full Access to just any Configuration File.
I developed my solution from this tutorial about Database Container Configuration and Build
MySQL Container Build and Configuration
As well from the Official Documentation at:
Docker Volumes Configuration and Usage
This is a Multi Step Process but which only needs to be done when the Original Configuration File changed.
The Concept consists of the Export and Reimport of the configured Configuration Files at Container Build Time.
- I create a Local Directory where to export the Original Configuration Files to.
$ mkdir config
$ mkdir uploads/config -p
$ vi Dockerfile
RUN mkdir /home/uploads
VOLUME /home/uploads
- To access the Image I launch it mounting the Local Directory to the Volume Mount Point
(Important is the :Z
to be able to export anything there)
# docker run -it --entrypoint sh -v /absolute/path/uploads:/home/uploads:Z php_web_alpine
- Now I can access the Configuration Directories and copy any File I need to edit to the Export Directory
/var/www/html # cd /etc/ap*
/etc/apache2 # pwd
/etc/apache2
/etc/apache2 # ls -lah
total 188K
drwxr-xr-x 1 root root 4.0K Jul 7 11:30 .
drwxr-xr-x 1 root root 62 Jul 7 11:33 ..
drwxr-xr-x 2 root root 4.0K Jul 7 11:30 conf.d
-rw-r--r-- 1 root root 17.3K Jul 7 11:30 httpd.conf
-rw-r--r-- 1 root root 17.4K Apr 1 18:18 httpd.conf.save
-rw-r--r-- 1 root root 17.4K Jul 7 11:30 httpd.conf.setup.1
-rw-r--r-- 1 root root 17.3K Jul 7 11:30 httpd.conf.setup.2
-rw-r--r-- 1 root root 17.3K Jul 7 11:30 httpd.conf.setup.3
-rw-r--r-- 1 root root 12.8K Apr 1 18:18 magic
-rw-r--r-- 1 root root 59.4K Apr 1 18:18 mime.types
/etc/apache2 # cp httpd.conf /home/uploads/config/apache/
/etc/apache2 # cd conf.d
/etc/apache2/conf.d # ls -lah
total 40K
drwxr-xr-x 2 root root 4.0K Jul 7 11:30 .
drwxr-xr-x 1 root root 4.0K Jul 7 11:30 ..
-rw-r--r-- 1 root root 2.2K Apr 1 18:18 default.conf
-rw-r--r-- 1 root root 1.2K Apr 1 18:18 info.conf
-rw-r--r-- 1 root root 5.0K Apr 1 18:18 languages.conf
-rw-r--r-- 1 root root 4.3K Apr 1 18:18 mpm.conf
-rw-r--r-- 1 root root 378 Jun 12 12:27 php7-module.conf
-rw-r--r-- 1 root root 732 Apr 1 18:18 userdir.conf
/etc/apache2/conf.d # cp mpm.conf /home/uploads/config/apache/
/var/www/html # cd /etc/php7
/etc/php7 # pwd
/etc/php7
/etc/php7 # mkdir /home/uploads/config/php/
/etc/php7 # cp php.ini /home/uploads/config/php/
/etc/apache2 # exit
- After the Export I find the Configuration Files from the Image in the Local Directory and can move them to the Configuration Directory for the Container Build
# cd config
# pwd
/apsolute/path/uploads/config
# ls -lah apache
insgesamt 28K
drwxr-xr-x. 2 user_name user_group 38 jul 7 12:45 .
drwxr-xr-x. 3 user_name user_group 19 jul 7 12:25 ..
-rw-r--r--. 1 root root 18K jul 7 12:45 httpd.conf
-rw-r--r--. 1 root root 4,4K jul 7 12:45 mpm.conf
# ls -lah php
insgesamt 72K
drwxr-xr-x. 2 root root 20 jul 7 13:32 .
drwxr-xr-x. 4 user_name user_group 29 jul 7 13:33 ..
-rw-r--r--. 1 root root 70K jul 7 13:32 php.ini
# mv apache ../../config/
# mv php ../../config/
# cd ../../config/
- Now I can edit the Configuration Files completely, easily and naturally.
- I include the configured Configuration Files in the
Dockerfile
to build a Completely Configured Web Server Container
- Keeping in mind that Configuration Files can change over time I will avoid to clobber the original files without having a save copy. In the nice RPM style with the
.save
Extension
$ vi Dockerfile
COPY config/apache/httpd.conf /etc/apache2/httpd.conf.setup
COPY config/apache/mpm.conf /etc/apache2/conf.d/mpm.conf.setup
COPY config/php/php.ini /etc/php7/php.ini.setup
RUN cd /etc/apache2\
&& mv httpd.conf httpd.conf.save\
&& cp -f httpd.conf.setup httpd.conf
RUN cd /etc/apache2/conf.d\
&& mv mpm.conf mpm.conf.save\
&& cp -f mpm.conf.setup mpm.conf
RUN cd /etc/php7\
&& mv php.ini php.ini.save\
&& cp -f php.ini.setup php.ini
- Now I can build the Container savely
# docker build -t php_web_alpine .
Sending build context to Docker daemon 122.9 kB
Step 1/12 : FROM alpine:3.12
---> a24bb4013296
Step 2/12 : RUN apk add apache2 php7 php7-apache2
---> Using cache
---> bf59e0c43f1f
Step 3/12 : COPY MyWeb.conf /etc/apache2/sites-enabled/
---> Using cache
---> f38a0d067fdd
Step 4/12 : COPY config/apache/httpd.conf /etc/apache2/httpd.conf.setup
---> d29e5b94e1ab
Removing intermediate container 9efaed4b5da5
Step 5/12 : COPY config/apache/mpm.conf /etc/apache2/conf.d/mpm.conf.setup
---> 286100656541
Removing intermediate container 2cc1ebac920a
Step 6/12 : COPY config/php/php.ini /etc/php7/php.ini.setup
---> cb12fe5aa008
Removing intermediate container 7dd9bde72996
Step 7/12 : RUN cd /etc/apache2 && mv httpd.conf httpd.conf.save && cp -f httpd.conf.setup httpd.conf
---> Running in d29ed8f6c3a1
---> dc1dcbc6adf6
Removing intermediate container d29ed8f6c3a1
Step 8/12 : RUN cd /etc/apache2/conf.d && mv mpm.conf mpm.conf.save && cp -f mpm.conf.setup mpm.conf
---> Running in 1667394701de
---> ecf4b961add4
Removing intermediate container 1667394701de
Step 9/12 : RUN cd /etc/php7 && mv php.ini php.ini.save && cp -f php.ini.setup php.ini
---> Running in 55502cdfc2da
---> c32bfcb13c4a
Removing intermediate container 55502cdfc2da
Step 10/12 : ADD html/ /var/www/html/
---> c458eec870e2
Removing intermediate container 2359ebc7b630
Step 11/12 : WORKDIR /var/www/html/
---> 56790c506e0d
Removing intermediate container ef739299fb95
Step 12/12 : ENTRYPOINT httpd -DNO_DETACH -DFOREGROUND -e info
---> Running in cb6d5ff8b90c
---> e2abe6e4091a
Removing intermediate container cb6d5ff8b90c
Successfully built e2abe6e4091a
Now I can launch it and it runs smoothly:
# docker-compose up -d
Recreating phpalpine_web_1 ... done
# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------
phpalpine_web_1 httpd -DNO_DETACH -DFOREGR ... Up 0.0.0.0:8081->8081/tcp
I find that the Web Server is Up and Running:
# netstat -lpn|grep -i :80
tcp6 0 0 :::8081 :::* LISTEN 31528/docker-proxy-
# wget -S -O - "http://localhost:8081"
--2020-07-13 15:43:55-- http://localhost:8081/
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:8081... connected.
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Date: Mon, 13 Jul 2020 14:43:55 GMT
Server: Apache/2.4.43 (Unix)
X-Powered-By: PHP/7.3.19
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
Length: 0 [text/html]
Saving to: ‘STDOUT’
[ <=> ] 0 --.-K/s in 0s
2020-07-13 15:43:55 (0,00 B/s) - written to stdout [0/0]
Discussion:
- Why do I want export the Original Configuration Files?
Configuration Files and Package Layout are very different from Distribution to Distribution. I might not have the correct ones and so the Service might turn out to be configured not as intended.
- Why do I want to preserve the Original Configuration Files at Build Time?
At Build Time the Packages might get updated and some Update might change something within the Configurations as it happens often with MySQL / MariaDB. To troubleshoot any Issue I need to be able to compare with the Original Configuration Files.