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

docker what is chown
docker copy --chown
docker chown slow
docker chown operation not permitted
docker cp chown
dockerfile exec
chown: invalid user docker
dockerfile arguments

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


As @Fortis and @whites11 pointed out I am overriding the existing CMD in the original PHP 5.6 Dockerfile. The various combinations of CMD and ENTRYPOINT are described in the Dockerfile reference - Understand how CMD and ENTRYPOINT interact - I believe I am in the bottom right of the matrix, combining the "CMD exec_cmd" from the original Dockerfile (in this case "CMD apache-foreground") with ENTRYPOINT ["exec_entry", "p1_entry"].

So, in my Dockerfile I just need to run the apache-foreground script that was set-up in the original Dockerfile, like this:

ENTRYPOINT ["/entrypoint.sh", "apache2-foreground"]

And leave the entrypoint.sh ending with:

exec "$@"

This allows me to do what I need to (create a mounted volume in my docker-compose.yml to the local filesystem, then use the entrypoint.sh to create and chown + chmod a directory inside that mount with a local user & group from inside the container) and then execute /apache2-foreground as the original Dockerfile intends.

I initially tested running apache2-foreground from within entrypoint.sh, and it also works, but I think that is a less "transparent" approach, and I guess for even better decoupling, in order to allow the authors of the original Dockerfile to change the CMD in their Dockerfile and my set-up not break as a result, I should pass whatever their CMD was into my Dockerfile ENTRYPOINT definition, sort of like...

ENTRYPOINT ["/entrypoint.sh", CMD]

But I don't know if that's possible is it?

Docker container creating directories owned by root, I need them , I've tried doing a chown -r and setgid on the host machine but the files FROM ubuntu:xenial MAINTAINER TheCreatorzOne RUN apt-get update --shell /bin/​bash --disabled-password --gecos "" qbittorrent && \ mkdir -p  Without rmdir and chown, I can perfectly build the docker if run over port:9000. As soon as I try setting up the directory for .sock, in the dockerfile, the docker build process exits. All Details below.


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.

Why does chown not work in RUN command in Docker?, In Docker "anything after the VOLUME instruction in a Dockerfile will Your mkdir and chown commands are RUN when building the image,  sudo docker run -p 8080:8080 --name=jenkins-main -d myjenkins The container is now up and running, checking with sudo docker ps But when I ls inside of file Docker, only the Dockerfile is there, no new directory called Jenkins.


The php docker image itself, has it's own command entry and entrypoint as you can see in its Dockerfile, and you're overriding them. So, instead of running apache2-foreground as it should, it runs your custom command and entrypoint, which creates the directories, change permissions and exits. Please note that there has to be a long running process for the container to stay up.

Instead you can add the following commands to the Dockerfile (haven't ran this, errors may occur):

RUN mkdir -p /var/www/html/ && chown www-data /var/www/html/maps && chgrp www-data /var/www/html/maps

or if you don't care about running them in a single layer, you can break them in separate RUN statements.

You should also remove the following lines:

COPY entrypoint.sh /entrypoint.sh
RUN chmod 0755 /entrypoint.sh

then run docker-compose build webserver again

Running docker/docker-compose, Running docker/docker-compose - "Cannot mkdir, permission I also tried running chown and chmod on the folders runtime with no luck  When I run docker-compose build --no-cache that step is passed: Step 7/7 : RUN cd /var/www/app && chmod +x yii && cd web && mkdir -p uploads && cd /var/www && chown nginx:nginx -R app/ ---> Running in 26a918bece47 Removing intermediate container 26a918bece47 ---> 00db026a461c Successfully built 00db026a461c Successfully tagged passport-app_yii2


The php:5.6-apache image has already an entrypoint defined.

If you override it with a custom script, you need to call the original entrypoint in order for the image to work.

Check here, you'll see that the original entrypoint is

ENTRYPOINT ["docker-php-entrypoint"]

You can try executing it from your script, such as:

#!/bin/sh

mkdir /var/www/html/maps
chown www-data /var/www/html/maps
chgrp www-data /var/www/html/maps
docker-php-entrypoint "$@"

this is untested but should work.

Permission denied for directories created automatically by , As part of a Dockerfile I'm running: ADD id_rsa.pub RUN mkdir /home/ubuntu/.​ssh/ RUN chown ubuntu:ubuntu -R /home/ubuntu/.ssh ADD  In general, either the pelias CLI is run as root, and the `chown` will succeed, or the CLI is run as the same user the Docker container processes will run as. In this case the `mkdir` on the preceeding line will set up the directory with proper ownership.


You can add those commands (COPY, CHMOD) inside docker file it self.

# Copy FileDirectory public html pages
  COPY FileDirectory/var/www/html /var/www/html
  RUN chmod +x /var/www/html

Have a look at my docker file https://github.com/RohanMohite/Docker-Nginx-PHP/blob/master/server1/Dockerfile

If you need any further information to setup Dockerize PHP application then have a look at https://devopsdevelopment.wordpress.com/2017/08/02/how-to-configure-docker-with-nginx-and-php-application/

Please let me known.

Chown doesn't work in Dockerfile? : docker, I'm trying to run chown in my dockerfile, but it doesn't seem to take. -rf /var/lib/​apt/lists/* RUN echo "jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers RUN mkdir​  RUN yum -y install php RUN yum clean all. This other one instead will produce a single layer with the php package installed and with the yum cache correctly cleaned. RUN yum -y install php && yum clean all Docker ChOwn. What about the chown?


Cannot execute RUN mkdir in a Dockerfile, Well, the directory /var/www does not exist either and the issue is that mkdir command is recursive by default, i.e. mkdir assumes that the  An important point from that article above: "[When VOLUME is specified after a RUN command that modifies the volume], docker is clever enough to copy any files that exist in the image under the volume mount into the volume and set the ownership correctly.


How to add a file to a docker container which has no root , mkdir docker-addfilepermission cd docker-addfilepermission touch There is likely a way to view and change the Dockerfile for tomcat, but I can't figure it out after a few minutes. My inelegant solution is to add this line before the chown: Run the container via a bootstrap script that changes the ownership. mkdir myproject && cd myproject echo "hello" > hello echo-e "FROM busybox COPY /hello / RUN cat /hello" > Dockerfile docker build -t helloapp:v1 . Move Dockerfile and hello into separate directories and build a second version of the image (without relying on cache from the last build).


Avoiding Permission Issues With Docker-Created Files · vsupalov.com, One frequent solution, is to “chown” your shared folder again and again. It's tedious and there is a better way: read on to learn learn how to build, configure and run  Every command in a Dockerfile runs in a separate (intermediate) container. The results are then stored as a new image layer on top of the existing ones. Adding a file in one layer and then