skip to Main Content

I am trying to set up an environment for Laravel, and in that process I want to change my document root into a public folder. I made this work in a Dockerfile, but in reality I much rather want it in a docker-compose.yml file.

I feel that I have implemented the required environment commands from their documentation

My code looks as

docker-compose.yml:

version: '3'

services:
    laravel:
        image: php:7.1-apache
        ports:
            - 8080:80
        env_file: ./.env
        environment:
            - "APACHE_DOCUMENT_ROOT=/var/www/html/public"
            - "sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf"
            - "sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf"
        volumes:
            - ./src/:/var/www/html/

.env

APACHE_DOCUMENT_ROOT=/var/www/html/public

I first tried without the .env file, but then it gave me an error stating that the APACHE_DOCUMENT_ROOT variable was not set. Which is why I have the external .env file.

Can anybody lead me in a direction for improving this yml, so I have a different document root for my Apache?

Thank you in advance.

3

Answers


  1. Chosen as BEST ANSWER

    I found a solution for this issue. basically, it worked when I created a Dockerfile with build commands. So the following structure fixed the issue:

    docker-compose.yml

    version: '3'
    services:
        web:
            build: .
            ports:
                - 80:80
            volumes:
                - ./src:/var/www/html
    

    Dockerfile

    FROM php:7.1-apache
    
    ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
    RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
    RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
    

    Just remember to run docker-compose build before running the image (if anybody else have the same problem).


  2. You are running the sed command in “Environment section”. Instead, you can use the “command” section to execute your sed command. These commands will run after entrypoint of Docker. So, your updated docker-compose.yaml will look something like as follow:

     version: '3'
    
    services:
        laravel:
            image: php:7.1-apache
            ports:
                - 8080:80
    
            command:
                - export APACHE_DOCUMENT_ROOT=/path/to/new/root
                - sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
                - sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
    
            volumes:
                - ./src/:/var/www/html/
    

    Please update your docker-compose.yaml file and then let me know if it solves your problem

    Login or Signup to reply.
  3. For testing temporary solutions, you can manually change the apache .conf file.
    When your container is running, enter the bash:

    docker exec -it your_container_name bash
    

    Edit the content of the .conf file:

    vim /etc/apache2/sites-available/000-default.conf
    

    (if vim is not found, run apt-get update and apt-get install vim)

    Finally reload apache:

    /etc/init.d/apache2 reload
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search