22

How can I access the php error logs for my container?

For some reason I'm really struggling to find out how to do this after a long time of searching various articles.

I'm using a simple php7 apache container which looks like: FROM php:7-apache

RUN apt-get update -y && apt-get install -y \
        libpng12-dev \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        curl \
        libcurl4-openssl-dev \
        libxpm-dev \
        libvpx-dev \
    && docker-php-ext-configure gd \
    --with-freetype-dir=/usr/lib/x86_64-linux-gnu/ \
    --with-jpeg-dir=/usr/lib/x86_64-linux-gnu/ \
    --with-xpm-dir=/usr/lib/x86_64-linux-gnu/ \
    --with-vpx-dir=/usr/lib/x86_64-linux-gnu/ \
    && docker-php-ext-install \
        pdo \
        pdo_mysql \
        gd \
        curl \
    && a2enmod rewrite \
    && service apache2 restart

Ideally I just need to view the contents of the error log or get a new custom log set locally on my machine so I easily see potential issues with my site build.

Any pointers appreciated. I found the docker documentation very confusing on the topic of logs...

Gerico
  • 5,079
  • 14
  • 44
  • 85

3 Answers3

41

It exists the following docker command:

docker logs -f --details containerName

that will show you the mysql and php errors log files

for more check the documentation: docker logs

Edwin
  • 2,146
  • 20
  • 26
  • the (--details) is in paranthese, because it is optional and should be `--details` if you want to add it – Edwin Aug 23 '17 at 12:17
  • 1
    I know that, but a user new to Docker may not. – Jay Blanchard Aug 23 '17 at 12:18
  • 1
    yes, sorry about that I just copied from a cheatsheet of mine – Edwin Aug 23 '17 at 12:20
  • 14
    When I add --details to check php-fpm logs `docker logs --details docker_php-fpm_1` I do not get any more details. All stays the same. Just basic: `172.25.0.8 - 30/Jan/2019:11:06:52 +0000 "POST /index.php" 500 172.25.0.8 - 30/Jan/2019:11:06:59 +0000 "GET /index.php" 200 172.25.0.8 - 31/Jan/2019:00:49:46 +0000 "GET /index.php" 404 172.25.0.8 - 31/Jan/2019:00:50:28 +0000 "GET /info.php" 200` – rhand Jan 31 '19 at 01:16
  • https://stackoverflow.com/questions/55207245/apache-php-error-log-location-in-docker – Максим Степанов Apr 23 '21 at 19:58
  • It'd be nice if the details option was available for docker compose as well. That said, I don't see, currently, any more information when adding that option, than I do without it. – Matthew Setter May 05 '23 at 07:13
6

By default the container doesn't seem to log PHP errors to STDOUT or STDERR. I found that when using the php.ini-development config file (see 'Configuration' in this article) it logs a lot more useful information.

To view the logs for a container, the most basic way is to do docker ps, find the container hash, and then do docker logs container_hash.

Jimbali
  • 2,065
  • 1
  • 20
  • 24
  • 1
    Thank you for this! Finally getting my PHP logs since using `mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"` when building the Docker Container – Oliver Dec 18 '22 at 17:23
3

All PHP output will be in the container, so you can use all the docker goodies to access the logs...

My favorite is attach as it allows you to follow the logs in real time. ( docker attach containerName )

There's also logs to see the past logs. docker logs containerName will print out all the output from the container. You might prefer adding the --tail=N flag where N is the number of lines to get.

Salketer
  • 14,263
  • 2
  • 30
  • 58