I’m working on configuring a docker setup for an old php project with apache, php 7.2 and mariadb. After I got the container up and running I get permission denied when I try to write a file from php.
What is the best approach to solve this?
docker-compose.yml
version: "3"
networks:
dirtbike:
services:
webserver:
build:
context: .
dockerfile: Dockerfile
container_name: dirtbike-webserver
restart: 'always'
depends_on:
- database
ports:
- "80:80"
- "443:443"
networks:
- dirtbike
volumes:
- ./public_html:/var/www/html
database:
image: mariadb:10.3
container_name: dirtbike-database
restart: 'always'
networks:
- dirtbike
ports:
- "127.0.0.1:3306:3306"
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
Dockerfile
FROM php:7.2-apache-stretch
RUN docker-php-ext-install pdo_mysql
RUN a2enmod ssl && a2enmod rewrite && a2enmod headers
RUN mkdir -p /etc/apache2/ssl
COPY ./Docker/ssl/*.pem /etc/apache2/ssl/
COPY ./Docker/config/apache/dirtbike.conf /etc/apache2/sites-available/000-default.conf
index.php
<?php
file_put_contents(__DIR__.DIRECTORY_SEPARATOR.'test.txt', 'lorem ipsum');
2
Answers
The problem was that the user from the container (www-root) was different then the user from the host. I added args:
uid: ${UID}
in the docker-compose file like this:in Dockerfile I added
ARG uid
andRUN usermod -u ${uid} www-data && groupmod -g ${uid} www-data;
like this:In your docker compose file you can to add your local machine user. First you need check current user id, in my case it is ubuntu:
Output:
docker-compose.yml:
Hope help you.