'Edit default .htaccess in wordpress docker

I have a wordpress container which gets generated by a docker-compose file. Wordpress container contains .htaccess file by default. I want to edit that such that that the edit is performed only once when container is created.



Solution 1:[1]

For now I can think of two possible solutions for this problem:

  1. We can use a static .htaccess file and mount that as a volume at the place where original .htaccess is copied, i.e, at /var/html/www inside container. From updates to this answer, I have verified that this .htaccess file will not get overwritten by default docker-entrypoint.sh.

  2. We can use the docker-entrypoint.sh from wordpress docker at github and modify the part which creates the .htaccess file. This will keep the default behavior of entrypoint and .htaccess file will also be modified.


Update: Answer given by @Nazar is also correct. Having a closer look at default docker-entrypoint.sh in wordpress image, I found that there is a condition at line 35, which checks if .htaccess file already exists and creates a new file only if file does not exist. So, if I create my own file using RUN, it will not be overwritten.

Solution 2:[2]

The easiest way is to build your own image based on wordpress:with-netcat, with all required preparations:

FROM wordpress:with-netcat

RUN /. EDIT YOUR .htaccess file here ./

ENTRYPOINT ...
CMD ...

And to use this image instead of the original one.

Solution 3:[3]

This can be tricky if you're mounting a directory on the host to /var/www/html (for example when using -v /host/path:/var/www/html or similar) -- the volume mount will clobber (overwrite) anything in your image.

If your host does not have any mounts, simply adding COPY .htaccess /var/www/html/.htaccess (or modifying the existing one with RUN) should work.

To get around the volume mount clobbering the image's htaccess file, write an entrypoint script to overwrite the file instead. Because the entrypoint runs after the mount exists, it will be sure to take effect.

Create a custom entrypoint shell script that copies your htaccess file into /var/www/html then fires off the default entrypoint:

#!/usr/bin/env bash

# entrypoint.sh

cp /my-htaccess-file /var/www/html/.htaccess

# run the entrypoint from the ancestor image
docker-entrypoint.sh "$@"

Then in your Dockerfile to copy your own htaccess file to a known location, add the entrypoint script, and (re)set the image command:

FROM wordpress:5.8.2-php7.4
COPY entrypoint.sh /my-entrypoint.sh
# RUN chmod +x /my-entrypoint.sh # uncomment if your source file is not executable
COPY .htaccess /my-htaccess-file
ENTRYPOINT ["/my-entrypoint.sh"]
# whenever the entrypoint is set, the command must also be set again
# this is the default CMD from the ancestor image.
CMD ["apache2-foreground"]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Gorjan Mishevski
Solution 3