Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker - executing mkdir, chown & chgrp after the container is up

I'm trying to create a docker container, using docker-compose, which mounts a volume on the local filesystem (for the container's /var/www/html) then adds a directory called maps and chowns and chmods is to www-data, so that the web server can write files into it.

I've tried a couple of approaches, using an entrypoint.sh script like this:

Dockerfile

FROM php:5.6-apache
COPY apache-config.conf /etc/apache2/sites-enabled/000-default.conf
RUN a2enmod rewrite headers
RUN service apache2 restart
COPY entrypoint.sh /entrypoint.sh
RUN chmod 0755 /entrypoint.sh

docker-compose.yml (stuff in {} just comes from a .env file)

version: '2'

services:
  webserver:
    build: ./docker/webserver
    image: web
    ports:
      - "8080:80"
    volumes:
      - ./web:${APACHE_DOC_ROOT}
    links:
      - db
    environment:
      - HTTP_ROOT=http://${DOCKER_HOST_IP}:${DOCKER_HOST_PORT}/
      - PHP_TMP_DIR=${PHP_TMP_DIR}
      - APACHE_LOG_DIR=${APACHE_LOG_DIR}
      - APACHE_DOC_ROOT=${APACHE_DOC_ROOT}/
      - SERVER_ADMIN_EMAIL=${SERVER_ADMIN_EMAIL}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}

entrypoint.sh

#!/bin/sh

mkdir /var/www/html/maps
chown www-data /var/www/html/maps
chgrp www-data /var/www/html/maps
exec "$@"

I've also tried without any of the entrypoint.sh stuff, just adding this into the composer.yml (after the environment key):

    command: bash -c "mkdir /var/www/html/maps && chown www-data /var/www/html/maps && chgrp www-data /var/www/html/maps"

But both these approaches seem to give no error in docker-compose logs other than

webserver_1 exited with code 0

like image 510
Mr Benn Avatar asked Oct 25 '25 07:10

Mr Benn


1 Answers

Not sure if you still have this problem, but maybe adding this will help you. I mean, it resolved my problem - PHP scripts running on the container didn't have write permissions.

RUN usermod -u 1000 www-data

Hope this will help.

like image 54
user5650206 Avatar answered Oct 27 '25 22:10

user5650206