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?
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 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.
Im trying to deploy my laravel app using docker with laravel sail. Then I created docker-compose.yml file and Dockerfile like below.
docker-compose.yml
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.1
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.1/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
- '${HMR_PORT:-8080}:8080'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_ROOT_HOST: '${DB_HOST}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- 'sail-mysql:/var/lib/mysql'
- './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local
Dockerfile
FROM php:7.4-fpm
# Arguments defined in docker-compose.yml
ARG user
ARG uid
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
# Set working directory
WORKDIR /var/www
USER $user
But, when I try to run docker-compose up, I get this below error. I have been trying this for now 2 days. I installed sail properly.
ERROR: for 7fd8f64d5ed1_proj_laravel.test_1 Cannot start service laravel.test: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "start-container": executable file not found in $PATH: unknown
ERROR: for laravel.test Cannot start service laravel.test: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "start-container": executable file not found in $PATH: unknown
ERROR: Encountered errors while bringing up the project.
Here is my .env
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=here_goes_db_name
DB_USERNAME=sail
DB_PASSWORD=tyutyu
How can I fix this ??
The default docker container should copy a bash script called start-container from your project root to the container during build. If you for example removed it from your project root, it can not copy it and run it later. So the steps to fix it are:
Restore the start-container script to your project root, you can find it here
Rebuild the image with docker-compose build
And everything should work again. I suspect step 2 is even optional as the entire project gets mounted as a volume.
When I boot up a container with below configuration using Blimp (Docker Compose in a cloud), it throws an error Exited: OCI runtime create failed: container_linux.go:345: starting container process caused "chdir to cwd (\"/var/www/html\") set in config.json failed: permission denied": unknown. Booting with docker-compose works just fine.
The problem is with volume mounting as far as I know.
docker-compose.yml
version: '3'
services:
app:
container_name: app
build:
context: ./
dockerfile: Dockerfile
args:
user: mk
uid: 1000
tty: true
volumes:
- ./src:/var/www/html
nginx:
container_name: nginx
image: nginx:stable-alpine
ports:
- "8080:80"
volumes:
- ./src:/var/www/html
- ./docker/nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- app
- mysql
networks:
- default
mysql:
container_name: mysql
image: mysql:5.7
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
volumes:
- ./docker/mysql:/var/lib/mysql
networks:
- default
networks:
default:
driver: bridge
Dockerfile
FROM php:7.4-fpm
LABEL MAINTAINER="Mayur Shingrakhiya <mk.shingrakhiya#gmail.com>"
RUN mkdir -p /var/www/html
ARG user=mk
ARG uid=1000
RUN apt-get update && apt-get install -y git curl libpng-dev libonig-dev libxml2-dev zip unzip
RUN docker-php-ext-install bcmath exif gd mbstring opcache pcntl pdo_mysql
# Get the Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS="1"
ENV PHP_OPCACHE_MAX_ACCELERATED_FILES="10000"
ENV PHP_OPCACHE_MEMORY_CONSUMPTION="192"
ENV PHP_OPCACHE_MAX_WASTED_PERCENTAGE="10"
COPY ./docker/php/conf.d/opcache.ini /usr/local/etc/php/conf.d/opcache.ini
# Create a User
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && chown -R $user:$user /home/$user
RUN chmod -R 775 /var/www/html
WORKDIR /var/www/html
USER $user
You won't find anything related to Blimp as it is not yet publicly released.
Help will be appreciated.
can you try RUN chmod -R 777 /var/www/html and then see if u get this error ?
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:
...