Why we need to run composer install inside the docker file, if it will be overwritten with volumes.
Mahmoud-Skafi opened this issue · 11 comments
Hello,
I am curious as to why we need to run composer install
inside the Docker file if it will be overwritten with volumes.
when we build the docker this will run first
# copy source files and config file
COPY --chown=${USERNAME}:${USERNAME} . $APP_HOME/
COPY --chown=${USERNAME}:${USERNAME} .env.$ENV $APP_HOME/.env
# install all PHP dependencies
RUN if [ "$BUILD_ARGUMENT_ENV" = "dev" ] || [ "$BUILD_ARGUMENT_ENV" = "test" ]; then COMPOSER_MEMORY_LIMIT=-1 composer install --optimize-autoloader --no-interaction --no-progress; \
else COMPOSER_MEMORY_LIMIT=-1 composer install --optimize-autoloader --no-interaction --no-progress --no-dev; \
fi
and this will copy the local files and overwrite what we did in the previous step in the docker file
docker-compose-yaml
NGINX: volumes:
- .:/var/www/html:ro,cached
LARAVEL: volumes:
- .:/var/www/html:cached
Hello
Please pay attention to the other docker-compose files that you have inside this repo and there you don't have mapping at all. That's why for all environments except dev you need to copy files inside.
Please inform us if you need anything else
Hi,
I see that in docker-compose for staging and prod there are no volumes in the laravel section, but there are volumes in nginx, still, this will copy the local files into nginx container, not the laravel container, for example, if we don't have the vendor file locally, this will means there is no vendor file in the nginx container because there is no connection between the nginx container and the larevel container, so what I did is create new volume in both nginx and laravel:
NGINX :
volumes:
- php:/var/www/html
LARAVEL:
volumes:
- php:/var/www/html
and at the bottom:
volumes:
php:
driver: local
I'm still confuse how we can share file between nginx and laravel
Hi
We have public folder inside NGINX and it is enough for proxy, and basically, NGINX just as "proxy". Please execute necessary commands in order to build prod/staging environments and check that everything are working as expected.
I know, ok, could you please explain this to me
set $root /var/www/html/public;
root $root;
in the nginx config, my question is how nginx proxy to the folder that exists in the larevel container, maybe the line above explains that?
and what the purpose for this line in the docker-composer nginx:
volumes:
- .:/var/www/html:ro,cached
Please pay attention to the line https://github.com/systemsdk/docker-nginx-php-laravel/blob/master/docker/dev/nginx.conf#L59
my question is how nginx proxy to the file that exists in the laravel container.
I'm quite not sure how nginx container proxy to the files that exist in the laravel container(I think it does not do that), nginx proxy to the files that bean copies locally.(so if we dont have a vendor locally it will not work), therefore why do we need to copy files and run composer install inside the dockerfile for laravel?
so now copying the local files and running composer install affects both containers, without the need to run composer install locally and then run composer install after the containers have been built
docker-compose.yaml
version: '3'
services:
### NGINX Server #########################################
nginx:
build:
context: "./docker/nginx"
depends_on:
- php-fpm
volumes:
- ${NGINX_SSL_PATH}:/etc/nginx/ssl
- ${NGINX_HOST_LOG_PATH}:/var/log/nginx
- php:/var/www/html
# - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
ports:
# port mappings, host to docker
- "${NGINX_HOST_HTTPS_PORT}:443"
- "${NGINX_HOST_HTTP_PORT}:80"
networks:
- backend
container_name: ${COMPOSE_PROJECT_NAME}-nginx
### PHP-FPM ##############################################
php-fpm:
build:
context: .
volumes:
- php:/var/www/html
depends_on:
- redis
# - mariadb
networks:
- backend
expose:
- "9000"
container_name: ${COMPOSE_PROJECT_NAME}-php-fpm
networks:
backend:
driver: bridge
volumes:
redis:
driver: local
php:
driver: local
Basically if you want to check your theory you can make next steps:
1)delete vendor folder locally
2)create some App\Http\Controllers\TestController with method show that "return view('welcome');"
3)edit routes/web.php and put there "Route::get('/test', [TestController::class, 'show']);"
2)make build-prod
3)make start-prod
4)make key-generate
5)enter make ssh-nginx and make sure that you don't have vendor folder there
6)enter make ssh and make sure that you have vendor folder inside
7)open your browser and access http://localhost/test and it should be available
So, basically, if you want to have vendor folder inside NGINX you can use your example above but you don't need vendor folder there as there is no php interpreter inside NGINX at all. Maybe you need to pay attention to the NGINX config and understand how it is working.
my problem is how can nginx container proxy to a file/folder that does not exist in the image, I don't know how nginx proxy to the folder that exists inside another image.
ps:
why do we do this there is no point right(in ngixn docker-compose file):
volumes:
- .:/var/www/html:ro,cached
You can read NGINX manual and examples if you want to understand config properly.
ps:
Currently what you need is to have just everything that are inside public folder as you have default nginx config set $root /var/www/html/public; so you can map as - ./public:/var/www/html/public:ro,cached (and NGINX will check if index.php file exists and transfer request to the laravel image).
But if you want to use environment not only for api requests, maybe you will need to change NGINX config and allow to access some other folders(static html files or images, etc...), that's why currently we have everything except vendor.
@dimadeush thanks so much, everything is now clear to me.