Edson Horacio Junior
Edson Horacio Junior

Reputation: 3143

Set docker volume permissions at container creation

I have a PHP app in docker that has an empty logs folder, that I added to the docker container as a volume:

app:
    volumes:
        - ./app/logs:/var/log/app

But when the app tries to write it raises an error because it has no permission.

I need a way to set permissions for it everytime the container gets created, so no manual process is needed, I also need the logs to persist even when the container is destroyed.

How can I do it? What would be a good practice for this scenario?

Upvotes: 1

Views: 1780

Answers (1)

Shuchi Sethi
Shuchi Sethi

Reputation: 803

Your web server or the writer to the logs doesn't have permissions to write to /var/log/app. Here you are bind-mounting a directory from the host in a container and in such case files and directories maintain the permissions they have on the host. Setting permissions on your host would solve the problem.

Refer to the following to do the same - write in shared volumes docker

Alternatively, I would suggest creating a docker volume for logs which would also solve your requirement of making them persistent.

For creating the volume use - docker create volume app-logs

services:

  app:
    volumes:
      - app-logs:/var/log/app

volumes:
app-logs:
  external: true

You may then use a docker instruction either CMD or ENTRYPOINT to set right permissions on the mounted volume. It would be like - CMD chown -R apache:apache /var/log/app

Upvotes: 1

Related Questions