skip to Main Content

Im few days in one project where I have a task of using Docker. After following few tutorials I find the easy way of deploying my LAMP server using docker-compose that looks like this:

version: '3.7'

services:
    php-httpd:
        image: php:7.3-apache
        ports:
            - 80:80
        volumes:
            - "./DocumentRoot:/var/www/html"

    mariadb:
        image: mariadb:10.5.2
        volumes:
            - mariadb-volume:/var/lib/mysql
        environment:
            TZ: "Europe/Rome"
            MYSQL_ALLOW_EMPTY_PASSWORD: "no"
            MYSQL_ROOT_PASSWORD: "rootpwd"
            MYSQL_USER: 'testuser'
            MYSQL_PASSWORD: 'testpassword'
            MYSQL_DATABASE: 'testdb'

    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        links:
            - 'mariadb:db'
        ports:
            - 8081:80

volumes:
    mariadb-volume:

Now after my app is in middle of development i find a problem that I need to edit the .htaccess to turn on mod_rewrite and add some rules for REST API.

All solutions that I find are mentioning that I should add RUN command in dockerfile similar to this:

FROM httpd:alpine

# Copy .htaccess into DocumentRoot
COPY ./.htaccess /var/www/html/

RUN sed -i '/LoadModule rewrite_module/s/^#//g' /usr/local/apache2/conf/httpd.conf

But I dont have that file, and offical documentation is of no help for me.

All I do is sudo docker-compose up -d to run my images. So question is:

In what stage, how and where should I add this file to make it work.

I know is possible cose I find this statement in one tutorial:

Next time around, we’ll create a more complicated docker-compose.yml
file, one that works in conjunction with a Dockerfile.

But there was no followup on this.

I would appreciate any guidance on this.

Also I know I can access PHP image with: docker exec -it linuxconfig_php-httpd_1 bash.
Maybe I can do something with that?

2

Answers


  1. You have two options:

    1. Docker is a layered file system, You can use one image and make modifications to it to create another image, which you can then push to your private Docker Registry or public dockerhub. To, create a custom image with your .htaccess changes, you will create a file named "Dockerfile". You will then place the Dockerfile in the same directory where your modified .htaccess

    Dockerfile content

    FROM php:7.3-apache
    
    RUN sed -i '/LoadModule rewrite_module/s/^#//g' /usr/local/apache2/conf/httpd.conf
    

    Create Image:

    docker build -t custom-php:7.3-apache .
    

    This will create a new image name (custom-php:7.3-apache). You can then use this new image in your docker-compose.yml file and when deployed, the container will have the updated .htaccess

    1. You may mount the .htaccess to the desired path by using volumes option as shown below

    .

    php-httpd:
        image: php:7.3-apache
        ports:
            - 80:80
        volumes:
            - "./DocumentRoot:/var/www/html"
            - "./.htaccess:/var/www/html/.htaccess"
    

    I prefer #2 as you can edit .htaccess rules without rebuilding the image.

    Login or Signup to reply.
  2. In Dockerfile add "RUN a2enmod rewrite"

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search