Docker: Modify the current value of Apache LogLevel directive within the configuration file

1. Run the Apache web server container

Running the httpd container
$ docker run -d httpd:latest


2. Get the id of the running container

Output from `docker ps`
$ docker ps
801e3b4a29bd httpd:latest "httpd-foreground" 16 seconds ago Up 14 seconds 80/tcp condescending_easley
35b5493e239c rancher/server "/usr/bin/entry /u..." 2 months ago Up 15 hours 3306/tcp,>8080/tcp keen_agnesi

A simple docker ps will list the running containers and from there you can get the container id.
You can see that the id of the container we want begins with 801e…

3. Open a shell in the running container

Open a bash shell
# docker exec -it 801e /bin/bash

Once the command is executed you enter a root shell within the container (shown by the presence of ‘root’ and ‘#’).

4. Check the current value of the LogLevel directive within the configuration file

Check the LogLevel
root@801e3b4a29bd:/usr/local/apache2# cat conf/httpd.conf | grep -i loglevel
# LogLevel: Control the number of messages logged to the error_log.
LogLevel warn

Explanation of the command:
cat conf/httpd.conf | grep -i loglevel.
cat conf/httpd.conf prints the content of the file conf/httpd.conf.
The | (pipe) redirects the output printed by cat to the next command which is grep.
grep is called with the argument loglevel and the flag -i.
The -i flag makes sure the loglevel string is treated in a case-insensitive way by grep.
The command results in all lines with any occurrence of loglevel (case-insensitive) being printed to the screen.
From the output we can see that the LogLevel is set to warn – whereas we would like it set to debug.

5. Use sed to search and replace the line with what we want

root@801e3b4a29bd:/usr/local/apache2# sed 's/LogLevel warn/LogLevel debug/' conf/httpd.conf > conf/httpd.conf.changed && mv conf/httpd.conf.changed conf/httpd.conf

Most likely, as is in this case, the container will not have a text editor installed – even vi and nano will not present. As a result we use sed (which is installed) to make the change.
An alternative would be to use your container’s package manager to install an editor. For example if the container is Debian based you could run apt-get update && apt-get install vim to install vim . Once installed you could use vim to edit the file.
Explanation of the command:
sed ‘s/LogLevel warn/LogLevel debug/’ conf/httpd.conf …
Substitute LogLevel warn with LogLevel debug in the contents of the file conf/httpd.conf. Note sed just streams this substitution to stdout so it still needs to be written to disk to persist.
… > conf/httpd.conf.changed && mv conf/httpd.conf.changed conf/httpd.conf
Write the output from the substitution to the file conf/httpd.conf.changed (it could be called anything) and then rename (move) the file so it overwrites the original. This is done to avoid the issue of creating an empty file as output (as would be the case if we wrote to the same file we read in from).
Essentially this just replaces the occurrence of LogLevel warn with LogLevel debug in the conf/httpd.conf file .

6. Check that your file change has been made correctly

LogLevel has been changed
root@801e3b4a29bd:/usr/local/apache2# cat conf/httpd.conf | grep -i LogLevel
# LogLevel: Control the number of messages logged to the error_log.
LogLevel debug

We can see from the output the LogLevel has been changed from warn to debug as we wanted.

7. Exit the shell (also exits the container)

Exit the shell and container
root@801e3b4a29bd:/usr/local/apache2# exit

8. Restart the container for the changes to take effect

Restart the container
$ docker restart 801e

The actual file change occurs immediately within the container but the global configuration file for the Apache server (httpd.conf) is only read when the server starts.
As a result we need to restart the server/container for the changes to take effect.

Leave a Reply

Your email address will not be published. Required fields are marked *