sfarzoso
sfarzoso

Reputation: 1600

How to mount host directory after copying the files to the container?

I need to copy the files of src folder to the container chowning them using www-data user and group, so in my Dockerfile I did:

COPY --chown=www-data:www-data src ./

when I access to the container I can see all the copied file but if I edit a file on the host, I'm not able to see the changes, so I have to rebuild the project using docker-compose up --build -d.

This is my docker-compose:

version: '3.9'

services:

  php-fpm:
    container_name: php_app
    restart: always
    build:
      context: .
      dockerfile: ./docker/php-fpm/Dockerfile
    #volumes:
    #  - ./src:/var/www/html

if I comment out volumes I can work on the host directory and see the changes, but in this way I lose the www-data chown.

How can I manage such situation? Essentially I want:

Upvotes: 1

Views: 212

Answers (1)

anemyte
anemyte

Reputation: 20176

There's no special feature to apply chown to mounted files. Leaving that and manual use of chown aside, you can make php-fpm workers to run with your uid. Here's how for php:8.0.2-fpm-alpine image (in other images path to config file can be different):

# Copy pool config out of a running container 
docker cp php_app:/usr/local/etc/php-fpm.d/www.conf .

# Change user in config
sed "s/user = www-data/user = $(id -u)/" www.conf -i
# and/or change group
sed "s/group = www-data/group = $(id -g)/" www.conf -i

Now mount the edited config into the container using volumes in docker-compose.yml:

services:
  php-fpm:
    volumes:
      - ./src:/var/www/html # code
      - ./www.conf:/usr/local/etc/php-fpm.d/www.conf # pool config

And restart the container.

Upvotes: 1

Related Questions