My php container does not have the permissions to write cache on the mounted volume.
docker-compose.yml:
version: '2'
volumes:
database_data:
driver: local
services:
php:
build: ./docker/php/
expose:
- 9000
volumes:
- ./public:/var/www/html
working_dir: /var/www/html
nginx:
image: nginx:latest
depends_on:
- php
ports:
- 80:80
volumes:
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
volumes_from:
- php
docker/php/Dockerfile:
FROM php:7.0-fpm
RUN docker-php-ext-install pdo_mysql \
&& docker-php-ext-install json
# Permission fix
RUN usermod -u 1000 www-data
You need to adapt the uid & gif of www-data to match the ownership of the files.
Inside your php's Dockerfile:
RUN sed -ri 's/^www-data:x:82:82:/www-data:x:1000:50:/' /etc/passwd
RUN chown -R www-data:www-data /var/www/html
Do you use it under Mac? Try to append this line in your php Dockerfile:
RUN usermod -G staff www-data
Another approach is to use the chown flag when copying, as stated here.
COPY --chown=<user>:<group> ...
Related
I have the following set up and have tried all the ways that I can find to pass env variables to PHP but none of the them are working. Not sure what I have set up wrong. Ideally I want to be able to get the variable using $_SERVER['MY_ENV'], but I have also tried getenv('MY_ENV') with no success.
docker-compose.yml
version: '3.9'
services:
app:
restart: unless-stopped
build:
context: ./
dockerfile: Dockerfile
environment:
- MY_ENV=dev
command: php -S app:8000 -t public
ports:
- "8888:8000"
depends_on:
- db
volumes:
- ./:/var/www
links:
- db
networks:
- myapp
db:
image: mysql:5.7
restart: always
command: mysqld --sql_mode="" --default-authentication plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laravel
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
ports:
- "3306:3306"
volumes:
- ./mysql-data:/var/lib/mysql
networks:
- myapp
networks:
myapp:
DockerFile
FROM php:7.2-fpm
RUN apt-get update -y && apt-get install -y openssl zip unzip git cron
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
RUN docker-php-ext-install pdo pdo_mysql
COPY composer.lock composer.json /var/www/
WORKDIR /var/www
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
COPY --chown=www:www . /var/www
USER www
EXPOSE 9000
CMD ["php-fpm"]
Im trying to deploy my laravel app using docker. Then I created docker-compose.yml file and Dockerfile like below.
docker-compose.yml
version: "3.7"
services:
app:
build:
args:
user: sammy
uid: 1000
context: ./
dockerfile: Dockerfile
image: travellist
container_name: travellist-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- travellist
db:
image: mysql:8.0
container_name: travellist-db
restart: unless-stopped
environment:
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker-compose/mysql:/docker-entrypoint-initdb.d
networks:
- travellist
nginx:
image: nginx:alpine
container_name: travellist-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d/
networks:
- travellist
networks:
travellist:
driver: bridge
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
And when I try docker-compose up, all the containers run with a any error. And also when I run docker-compose ps, it shows like this,
Name Command State Ports
------------------------------------------------------------------------------------------------
travellist-app docker-php-entrypoint php-fpm Up 9000/tcp
travellist-db docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp
travellist-nginx /docker-entrypoint.sh ngin ... Up 0.0.0.0:8000->80/tcp,:::8000->80/tcp
But, when laravel application, tries to connect with mysql, it doesnt happens. And even I cant connect using TablePlus, it shows this.
Lost connection to MySQL server at 'waiting for initial communication packet', system error: 10060
And sometimes, I get like below,
SQLSTATE[HY000] [1045] Access denied for user 'root'#'192.168.48.3' (using password: YES)
How can I fix this ??
Since you're networking the services, you only need to expose the port 3306/tcp (this is the default port mysql serves). This makes it inaccessible from 0.0.0.0 but accessible from your nginx and app container.
I have also added the correct volume to persist data (which I am assuming is what you're trying to do). I moved mysql to latest - feel free to rollback or stick to 8 if you really? need too.
db:
image: 'mysql:latest'
restart: 'unless-stopped'
expose:
- '3306'
environment:
- 'MYSQL_RANDOM_ROOT_PASSWORD=true'
- 'MYSQL_DATABASE=${DB_DATABASE}'
- 'MYSQL_USER=${DB_USER}'
- 'MYSQL_PASSWORD=${DB_PASSWORD}'
- 'MYSQL_ALLOW_EMPTY_PASSWORD=true'
volumes:
- 'laravel-database:/var/lib/mysql/'
networks:
- 'travellist'
volumes:
laravel-database:
Further reading can be found on the Docker hub under mysql.
Inside your Laravel .env, you should connect to the container via its DNS.
DB_HOST=db
If you want to connect to this container externally, you'll need to bind the port to the server. Do so by replacing expose with the following:
ports:
- '3306:3306'
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:
...
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?