1

I am using docker-compose.yml to run my Laravel app. Everything works fine if I run

chmod -R 755 storage

in the larval root.

But what I want is to define permission on Dockerfile. app/docker/web/Dockerfile

FROM php:7.2.10-apache-stretch

RUN apt-get update -yqq && \
    apt-get install -y apt-utils zip unzip && \
    apt-get install -y nano && \
    apt-get install -y libzip-dev libpq-dev && \
    apt-get install -y libxml2-dev && \
    apt-get install -y libcurl4-openssl-dev pkg-config libssl-dev && \
    a2enmod rewrite && \
    docker-php-ext-configure zip --with-libzip && \
    docker-php-ext-install zip && \
    rm -rf /var/lib/apt/lists/*

RUN pecl install mongodb && \
    docker-php-ext-enable mongodb

RUN curl -sS https://getcomposer.org/installer | php -- --install- 
   dir=/usr/local/bin --filename=composer

COPY default.conf /etc/apache2/sites-enabled/000-default.conf

WORKDIR /var/www/app

ADD . /var/www/app

RUN chown -R www-data: /var/www/app
RUN chmod -R 755 /var/www/app/storage

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

EXPOSE 80

And docker-compose.yml

version: '3.1'
services:
    web:
        container_name: ${APP_NAME}
        build:
            context: ./docker/web
        ports:
            - 8088:80
        volumes:
            - ./:/var/www/app
        depends_on:
            - mongo
        links:
            - mongo:mongo
   mongo:
        image: mongo
        restart: always
         ports:
            - "27016:27016"
        volumes:
            - ./storage/mongodb:/data/db
        environment:
            MONGO_INITDB_ROOT_USERNAME: root
            MONGO_INITDB_ROOT_PASSWORD: password

While running docker-compose up

chmod: cannot access '/var/www/app/storage': No such file or directory
ERROR: Service 'web' failed to build: The command '/bin/sh -c chmod -R 
755 /var/www/app/storage' returned a non-zero code: 1

Can anyone suggest to me what I am doing wrong? Thanks in advance.

2 Answers 2

1

I'm guessing this image default user is ubuntu, and you are switching the folders permissions to www-data, which would make the default user able to read but not write.

It's correct for a production setup which would have apache being run as www-data, but notice you are starting apache as the default user for that image, so try removing both of those statements:

RUN chown -R www-data: /var/www/app
RUN chmod -R 755 /var/www/app/storage

This way the same user that is mounting the folders will also be running apache

If that doesn't work, go full berserk and just make the super user run all the steps with

USER root
Sign up to request clarification or add additional context in comments.

Comments

0

In your Dockerfile you are already copying the files inside the image: ADD . /var/www/app.

For this reason it doesn't make sense to also map the volume in your docker-compose.yml.

The solution is to REMOVE the volume declaration in your web service:

         volumes:
            - ./:/var/www/app

2 Comments

I removed the line volumes: - ./:/var/www/app. Still error response "cannot access '/var/www/app/storage'".
Do you have a folder storage in your local? Probably not. So either create it in local and make sure it is not present in .dockerignore (if you have one) or create it in the container: RUN mkdir /var/www/app/storage before you try to change the permissions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.