How url of laravel app under docker without “public”?

Hello,
Installing laravel 5.7 app under docker(based on php:7.2-apache ) I need to use “/public” in my url to run my app, so root url of my app is
http://127.0.0.1:8081/public

I modified .env of my laravel app as
APP_URL=http://127.0.0.1:8081/public/
But it did not help, as I browser I got image url :
http://127.0.0.1:8081/storage/votes/-vote-16/Babe_ver1.jpg?dt=1546059015
which way is invalid, as valid way must be :
http://127.0.0.1:8081/public/storage/votes/-vote-16/Babe_ver1.jpg?dt=1546059015

The similar way with ajax requests, as I got invalid ways :
http://127.0.0.1:8081/admin/get_activity_log_rows/1
But valid must be
http://127.0.0.1:8081/public/admin/get_activity_log_rows/1

In my docker-compose.yml :
version: ‘3.1’

services:

    web:

        build:
            context: ./web   
            dockerfile: Dockerfile.yml
        
        environment:
            - APACHE_RUN_USER=www-data
        volumes:
            - ${APP_PATH_HOST}:${APP_PTH_CONTAINER}
        ports:
            - 8081:80
        working_dir: ${APP_PTH_CONTAINER}


    composer:
        image: composer:1.8
        volumes:
            - ${APP_PATH_HOST}:${APP_PTH_CONTAINER}
        working_dir: ${APP_PTH_CONTAINER}
        command: composer install 

and in .env of docker project :

# PATHS
DB_PATH_HOST=./databases
APP_PATH_HOST=./Votes
APP_PTH_CONTAINER=/var/www/html/

How to set root url of my site with “/public” ?

Thanks!

Hi, instead of using apache, you could use “php artisan” :slight_smile:

Sorry, I did not understand your answer. Could you please to detail it ?

Laravel comes with its own “webserver”, that might be easier to user in this case.

I guess, that you defined your “workdir”, in your dockerfile, to be the root of your webpage, then try and and set this:

ENTRYPOINT [“php”,“artisan”,“serve”,"–host=0.0.0.0","–port=80"]

Do you mean working_dir option ?
this line
ENTRYPOINT [“php”,“artisan”,“serve”,"–host=0.0.0.0","–port=80"]
must be added to “web” container?

Yes :slight_smile:

If you dont have any “WORKDIR” in your Dockerfile, add it, and set it to be the root of your webpage, and then att the entrypoint to the end.

if it dosnt work, please share your dockerfile :slight_smile:

1 Like

Thank you for your help!
I tried but failed.

I added 1 line in web/Dockerfile.yml :

FROM php:7.2-apache

RUN apt-get update && \
    apt-get install -y \
    libfreetype6-dev \
    libwebp-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    nano \
    libgmp-dev \
    libldap2-dev \
    netcat \
    sqlite3 \
    libsqlite3-dev && \
    docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-webp-dir=/usr/include/  --with-jpeg-dir=/usr/include/ && \
    docker-php-ext-install gd pdo pdo_mysql pdo_sqlite zip gmp bcmath pcntl ldap sysvmsg exif \
&& a2enmod rewrite

ENTRYPOINT [ "php", "artisan", "serve", "–host=0.0.0.0", "–port=8081" ]
# I suppose need to use port of "web" in docker-compose.yml ?

Building the project :
$ docker-compose up -d --build
Creating network “votes_docker_default” with the default driver
Building web
Step 1/3 : FROM php:7.2-apache
—> cf1a377ba77f
Step 2/3 : RUN apt-get update && apt-get install -y libfreetype6-dev libwebp-dev libjpeg62-turbo-dev libpng-dev nano libgmp-dev libldap2-dev netcat sqlite3 libsqlite3-dev && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-webp-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && docker-php-ext-install gd pdo pdo_mysql pdo_sqlite zip gmp bcmath pcntl ldap sysvmsg exif && a2enmod rewrite
—> Using cache
—> 40b2a1e9cafc
Step 3/3 : ENTRYPOINT [ “php”, “artisan”, “serve”, “–host=0.0.0.0”, “–port=8081” ]
—> Running in 94e5ee982001
Removing intermediate container 94e5ee982001
—> 3f8e5e3a15ba
Successfully built 3f8e5e3a15ba
Successfully tagged votes_docker_web:latest
Creating votes_docker_web_1 … done
Creating votes_docker_db_1 … done
Creating votes_docker_composer_1 … done
Creating votes_docker_phpmyadmin_1 … done

But checking logs I got error :
$ docker logs --tail=20 votes_docker_web_1

Warning: require(/var/www/html/vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/artisan on line 18 
 
Fatal error: require(): Failed opening required '/var/www/html/vendor/autoload.php' (include_path='.:/usr/local/lib/php') in /var/www/html/artisan on line 18

What is wrong?

Im not sure how you build your laravel app, but it seems like you’re missing out a compose command.
If you have follwed this guide, it should work:
https://laravel.com/docs/5.7/installation#installing-laravel

In my docker-compose.yml

   composer:
        image: composer:1.8
        volumes:
            - ${APP_PATH_HOST}:${APP_PTH_CONTAINER}
        working_dir: ${APP_PTH_CONTAINER}
        command: composer install

there is composer install command. Do you mean it?
But the thing is that as composer.json has a lot of libraries, so uploading/installing all libraries takes several minutes.
usually I waite untill /vendor/autoload.php appears and I consider that composer run successfully.

Can it be that composer tries to run instruction

`ENTRYPOINT [ "php", "artisan", "serve", "–host=0.0.0.0", "–port=8081" ]`

but composer install is in process yet?
Sorry, I am new in docker and that is tricky for me.
If there is a way to enter at entrypoint after composer run successfully ?
Or maybe to run entrypoint ayt command line ?

yes that could be the case, but after it finished, you should be able to restart that web container, and it should run.

but now it seems like to be easier, to use the apache approach since we are facing this problem.

So … basicly you just want apache to point to /public insted of / ?

Yes, I need apach to point to “/public” instead of “/”

Do you mean after I rebuilded the project with command
docker-compose up -d --build

I need a take a pause in several minutes( untill /vendor/autoload.php appears ) and run again
docker-compose up -d
?

I tried, but next got errors:

  $ docker-compose up -d
  votes_docker_db_1 is up-to-date
  Starting votes_docker_composer_1 ... 
  Starting votes_docker_composer_1 ... done
  Starting votes_docker_web_1      ... done
  $ docker logs --tail=20  votes_docker_web_1
    Warning: require(/var/www/html/vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/artisan on line 18

    Fatal error: require(): Failed opening required '/var/www/html/vendor/autoload.php' (include_path='.:/usr/local/lib/php') in /var/www/html/artisan on line 18
      Too many arguments, expected arguments "command".  
                                                       

    $ docker-compose exec web bash
    ERROR: No container found for web_1

?

No, i dont think it will work when you have to build everytime.

Check this guy out, he has a virtual host that seems to do what you need

1 Like

Thank you! I will look into it. But I am surpised, I suppose that I am not the first who has “/” instead of “/public” issue? Are there some common decision ?

The php-apache image is just a base image, then you need to modify it to suit your custom needs.

the /public is mostly used for MVC frameworks, and there are properly some laravel images

Just noticed as you mentioned

restart that web container

Please explain how to make it?

Thank you for your help and for this link.
I looked that at end of web/Dockerfile.yml there was a line :
COPY virtualhost.conf /etc/apache2/sites-enabled/000-default.conf
and in this 000-default.conf “/public” path and other parameters were written.
I am testing on my local laptop now and seems all works ok.
Next I will have to move the project under Digital Ocean with ubuntu 18 server and to make it work with
my dns hosting created at https://my.freenom.com. Could you please give a hint what have I to use for this ?

You should be able to just install docker, get your domain to point to the new server, and that should be it :slight_smile:

unless you have some other demands :slight_smile: