I installed laravel using composer inside of my docker container, however, i get this error when i try to access the index page
The stream or file "/var/www/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log:
I am using laravel 9, i have tried all possible solution i could find and it doesn't work.
I run on windows 10.
Here is my Dockerfile
FROM php:8.0.2-fpm
# Install dependencies for the operating system software
RUN apt-get update && apt-get install -y \
git \
curl \
zip \
unzip
# Install extensions for php
RUN docker-php-ext-install pdo_mysql
# Install composer (php package manager)
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Set working directory
WORKDIR /var/www
I have adding the below code to my Dockerfile
# Assign permissions of the working directory to the www-data user
RUN chown -R www-data:www-data \
/var/www/storage \
/var/www/bootstrap/cache
but when try to build the image again with the code in it, i get an error message saying
directory doesn't exist
Here is my docker-compose.yml file
version: '3.8'
services:
php:
build:
context: ./
dockerfile: Dockerfile
container_name: jaynesis-php
restart: always
working_dir: /var/www
volumes:
- ../src:/var/www
nginx:
image: nginx:latest
container_name: jaynesis-nginx
restart: always
ports:
- "8000:80"
volumes:
- ../src:/var/www
- ./nginx:/etc/nginx/conf.d
mysql:
container_name: jaynesis-db
image: mysql:8.0
volumes:
- ./storage/mysql:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
If you have an error: "The stream or file "/var/www/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log:"
Delete this laravel.log file.
Give this a go, it might work...
docker exec -it jaynesis-php bash
chmod 755 storage/ -R
This makes the storage directory a little more writeable.
Related
So, I want to configure the swoole laravel project. I run a Dockerfile and it successfully run. Then I want to run compose file this give me error
There are no commands defined in the "swoole" namespace.
This is my first experience with swoole. And I don't understand what is the problem.
How can solve this problem?
This is a Dockerfile
FROM php:8.1-fpm-alpine
# Install laravel requirement PHP package
RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS libzip-dev sqlite-dev \
libpng-dev libxml2-dev oniguruma-dev libmcrypt-dev curl curl-dev libcurl postgresql-dev
RUN docker-php-ext-install -j$(nproc) gd bcmath zip pdo_mysql pdo_pgsql
RUN pecl install xdebug swoole && docker-php-ext-enable swoole
# Install composer
ENV COMPOSER_HOME /composer
ENV PATH ./vendor/bin:/composer/vendor/bin:$PATH
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN curl -s https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer
# Install PHP_CodeSniffer
WORKDIR /app
COPY ./ ./
USER root
RUN chown -R www-data /app/storage
RUN chmod -R ug+w /app/storage
RUN chmod 777 -R /app/storage
RUN chmod 777 -R /app/public
RUN composer install
RUN php artisan optimize
CMD php artisan swoole:http start
EXPOSE 1215
And this is a docker-compose.yaml file
version: "3.7"
services:
app:
build:
args:
user: www-data
uid: 1000
context: ./
dockerfile: Dockerfile
image: topspot-swoole-image
container_name: topspot-swoole-container
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- topspot-network
nginx:
image: nginx:alpine
container_name: topspot-nginx
restart: unless-stopped
ports:
- 80:80
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d/
networks:
- topspot-network
networks:
topspot-network:
driver: bridge
Solved
I solved it. Firstly install the swoole and publish to a local project. Then run the container and the composer saw the Swoole packages.
I'm making a web application using Laravel 7 but I want to develop it in docker, but when I am using my container, the container files are not synchronized with the ones that are shown in the text editor, how can I solve that?
The files that are not synchronized are, for example, the migrations or the composer.json, that is, when I install a package through composer, those changes are not reflected in the text editor
I've tried to put ./:/var/www/html in docker-compose.yml but I get an error when I run it. 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 online 18
Next, I provide my dockerfile and my docker compose.
Dockerfile
FROM composer:1.10.22 as build_stage
LABEL maintainer="Me"
COPY . /src
ADD .env.example /src/.env
WORKDIR /src
RUN composer install --no-ansi --no-interaction --no-progress --no-scripts --optimize-autoloader --ignore-platform-reqs
RUN php artisan key:generate
FROM php:7-fpm-alpine
ENV \
BUILD_PACKAGES="build-base curl-dev linux-headers" \
SQLITE_PACKAGES="zlib-dev libxml2-dev libxslt-dev postgresql postgresql-dev sqlite sqlite-libs sqlite-dev nano php7-gd php7-mysqli php7-zlib php7-curl"
RUN apk update && apk add $BUILD_PACKAGES && apk add $SQLITE_PACKAGES
COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN set -ex \
&& apk --no-cache add \
postgresql-dev
RUN docker-php-ext-install pdo pdo_pgsql
COPY --from=build_stage /src /var/www/html
RUN chmod -R 777 /var/www/html
EXPOSE 5000
COPY ./run.sh /tmp
RUN chmod +x /tmp/run.sh
ENTRYPOINT ["/tmp/run.sh"]
docker-compose.yml
version: "3.4"
networks:
project-net:
volumes:
project-datastore:
services:
postgres-db:
image: postgres:11-alpine
volumes:
- project-datastore:/var/lib/postgresql/data
networks:
- project-net
ports:
- "25432:5432"
environment:
POSTGRES_DB: db
POSTGRES_USER: user
POSTGRES_PASSWORD: password
project:
build:
context: ./
image: project:latest
volumes:
- ./app:/var/www/html/app
- ./public:/var/www/html/public
- ./routes:/var/www/html/routes
- ./resources:/var/www/html/resources
depends_on:
- postgres-db
networks:
- project-net
ports:
- "5000:5000"
environment:
DATABASE_URL: postgres://user:password#postgres-db:5432/db
run.sh
cp /usr/share/zoneinfo/America/Mexico_City /etc/localtime
php artisan serve --host=0.0.0.0 --port=5000
I'm trying to set up my symfony project in docker, and i'M struggling building the php container.
I've put all my source code in \var\www\app\
Here is an extract of my docker-compose.yml:
php:
build:
context: .
dockerfile: docker/php/Dockerfile
volumes:
- '/var/www/app:/var/www'
restart: on-failure
env_file:
- .env
user: 1000:1000
nginx:
image: nginx:1.19.0-alpine
restart: on-failure
volumes:
- '/var/www/app:/var/www'
- './docker/nginx/app.conf:/etc/nginx/conf.d/app.conf:ro'
ports:
- '80:80'
depends_on:
- php
here's the Dockerfile:
FROM composer:2.0 as composer
FROM php:7.4-fpm
RUN docker-php-ext-install pdo_mysql
RUN pecl install apcu
RUN apt-get update && \
apt-get install -y \
libzip-dev
RUN docker-php-ext-install zip
RUN docker-php-ext-enable apcu
WORKDIR /var/www
COPY --chown=1000:1000 var/www/app /var/www
RUN PATH=$PATH:/usr/src/app/vendor/bin:bin
RUN composer install --no-scripts --prefer-dist \
&& rm -rf "$(composer config cache-dir)" "$(composer config data-dir)"
and here's the error I get while trying to build the php container:
Step 9/11 : COPY --chown=1000:1000 var/www/app /var/www
ERROR: Service 'php' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder883740312/var/www/app: no such file or directory
The problem, I guess, is that it tries to fetch file within the docker build folder instead of /var/www/app. I thought it was because of the context, and I've tried to change it but then it cannot find my Dockerfile anymore.
I don't see how to resolve this, yyet I have the feeling it is a veryy easy one...But I'm quite lost at the moment.
Thanks!
You try to COPY a folder at build time and then at runtime you replace the same by using a VOLUME in your docker-compose. Maybe the volume is not needed?
Regarding the error, docker expects your var folder to be at the same level as docker-compose.yml. If that is not the case and your var folder is in docker/php/, then that is what you have to set as context in your docker-compose.yml.
php:
build:
context: docker/php
dockerfile: Dockerfile
If the Dockerfile is in the context, you don't even need to declare it explicitly in the docker-compose.yml
Trying to set a laravel project with redis, mysql and mongodb. Using docker-compose to set docker containers, but 127.0.0.1 throws The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied. Chown'ing directory but owner of /var/www/html inside 'app' container is root, not www-data. How can I fix it?
Dockerfile
ARG PHP_VERSION
FROM php:7-fpm-alpine
ARG APP_ENV
ARG REMOTE_WORKING_DIR
RUN apk update && apk add --no-cache $PHPIZE_DEPS \
build-base shadow nano curl gcc git bash \
php7 \
...installing dependencies...
...redis & mongodb...
...php settings...
...installing composer...
RUN rm -rf /var/cache/apk/*
RUN apk add shadow && usermod -u 1000 www-data && groupmod -g 1000 www-data
COPY . $REMOTE_WORKING_DIR
RUN sudo chown -R www-data:www-data $REMOTE_WORKING_DIR
USER www-data
EXPOSE 9000
CMD ["php-fpm"]
docker-compose.yml
version: "3"
services:
app:
build:
context: ./docker/php
args:
APP_ENV: ${APP_ENV}
PHP_VERSION: ${PHP_VERSION}
REMOTE_WORKING_DIR: ${REMOTE_WORKING_DIR}
container_name: app
restart: always
env_file: .env
ports:
...
links:
- redis
networks:
...
volumes:
- ${LOCAL_WORKING_DIR}:${REMOTE_WORKING_DIR}
- ./docker/php/config/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
nginx:
...
volumes:
- ${LOCAL_WORKING_DIR}:${REMOTE_WORKING_DIR}
- ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./docker/nginx/conf.d/:/etc/nginx/conf.d/
- ./docker/nginx/ssl/:/etc/nginx/ssl/
ports:
...
depends_on:
- app
networks:
...
redis:
...
networks:
...
I have a PHP project which requires me to change the owner of the /storage folder:
This is my docker-compose.yml
version: '2'
services:
web:
build:
context: ./
dockerfile: ./docker/web.docker
volumes:
- ./:/var/www
ports:
- "8080:80"
links:
- app
app:
build:
context: ./
dockerfile: ./docker/app.docker
volumes:
- ./:/var/www
links:
- database
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
- "REDIS_PORT=6379"
- "REDIS_HOST=cache"
app.docker
FROM php:7.1-fpm
RUN apt-get update && apt-get install -y libmcrypt-dev mysql-client \
&& docker-php-ext-install mcrypt pdo_mysql bcmath
WORKDIR /var/www
RUN chown www-data:www-data -R /var/www/storage/
The thing is that when chown www-data:www-data is run, I guess the folder is still not mounted and therefore I get the following error:
chown: cannot access '/var/www/storage/': No such file or directory
How can I posibly solve this? Is there a hook to run commands after the shared folder has been mounted?