1

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?

Edson Horacio Junior
  • 3,033
  • 2
  • 29
  • 50

1 Answers1

1

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

Shuchi Sethi
  • 683
  • 7
  • 13
  • I have put the ENTRYPOINT in dockerfile, but everytime it finishes the container gets restarted. I don't want this, it needs to keep running, how do I proceed? – Edson Horacio Junior Oct 10 '19 at 18:03
  • Well, this is different from the original question. Can you please post what's in your ENTRYPOINT? You can also check the docker logs to see what's happening and try to figure out what's causing the restart. – Shuchi Sethi Oct 10 '19 at 22:49
  • I've searched this before, if I use ENTRYPOINT, CMD or COMMAND, the container acts like that is the whole purpose of it, so when it finished it'll restart (because I'm using restart: always). This is what I put: `ENTRYPOINT chown apache:apache /var/log/app` – Edson Horacio Junior Oct 11 '19 at 15:25