3
\$\begingroup\$

I've created a repo for some Docker containers that work together with docker-compose to make a very easy and quick installation for Laravel including nginx, mariadb, and redis. Laravel is known for having one somewhat annoying step to get it started, which is that the storage and bootstrap/cache directories have to be writable by the webserver/php process:

chmod -R 777 storage bootstrap/cache

However, docker containers that use shared volumes between the container and host tend to have permission issues because of discrepancies between uids and guids differing between the container and the host.

This is a Mac-only project until I get these basic issues solved.

I would greatly appreciate solutions for how to have the php process user in the php container have write permissions on those directories in the shared volume.

This install script (called by typing ./install into the terminal) is what should be theoretically setting the permissions:

install

#!/bin/bash
echo "installing and building docker containers..."
docker-compose up -d

echo "installing laravel/installer via composer..."
rm -rf code
if [[ $(composer global show) != *laravel/installer* ]]
  then
    composer global require "laravel/installer"
fi

echo "installing laravel..."
laravel new code

echo "changing working directory to code..."
cd code

echo "setting permissions..."
chmod -R 777 storage bootstrap/cache
docker exec ${PWD##*/}_php_1 chgrp -R www-data /code
docker exec ${PWD##*/}_php_1 chmod -R 777 storage bootstrap/cache

echo "installing predis..."
composer require "predis/predis"

echo "installing correct database settings to laravel..."
sed -i '' "s/DB_HOST=127.0.0.1/DB_HOST=mariadb/" .env
sed -i '' "s/DB_DATABASE=homestead/DB_DATABASE=laravel/" .env
sed -i '' "s/DB_USERNAME=homestead/DB_USERNAME=laravel/" .env
sed -i '' "s/REDIS_HOST=127.0.0.1/REDIS_HOST=redis/" .env
sed -i '' "s/CACHE_DRIVER=file/CACHE_DRIVER=redis/" .env
sed -i '' "s/SESSION_DRIVER=file/SESSION_DRIVER=redis/" .env

echo "returning working directory to previous state..."
cd ..

echo "installation complete"

docker-compose.yml

version: '2'
services:
  web:
    image: nginx:alpine
    ports:
    - "80:80"
    volumes:
    - ./code:/code
    - ./site.conf:/etc/nginx/conf.d/site.conf
    networks:
    - front-tier
    - back-tier
  php:
    image: laravel_pdo_php
    build: ./php
    working_dir: /code
    volumes:
    - ./code:/code
    - ./php.ini:/usr/local/etc/php/php.ini
    networks:
    - back-tier
    expose:
    - "9000"
  mariadb:
    image: mariadb:latest
    ports:
    - "3306:3306"
    volumes:
    - mariadb:/var/lib/mysql
    networks:
    - back-tier
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: laravel
      MYSQL_USER: laravel
      MYSQL_PASSWORD: secret
    expose:
    - "3306"
  redis:
    image: redis:alpine
    networks:
    - back-tier
    expose:
    - "6379"
volumes:
  mariadb:
    driver: local
networks:
  front-tier:
    driver: bridge
  back-tier:
    driver: bridge

php/Dockerfile

FROM php:7-fpm
RUN docker-php-ext-install pdo pdo_mysql
CMD ["php-fpm"]
\$\endgroup\$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.