SkyRar
SkyRar

Reputation: 1277

How to redirect mysql log to /dev/stdout inside container?

I am trying to write my own mariadb-alpine docker image. Everything works file but while I am trying to collect the mariadb logs I am getting nothing. I tried to follow a lot of related issue like this and tried those but in vain.

FROM alpine:edge


COPY my.cnf /etc/mysql/my.cnf


RUN set -ex \
    && apk add mariadb mariadb-client shadow \
    && ln -snf /usr/lib/mariadb /usr/lib/mysql \
    && mysql_install_db --user=mysql --skip-name-resolve --auth-root-authentication-method=socket --auth-root-socket-user=root --force --rpm --skip-test-db \
    && usermod -a -G tty mysql \
    && ln -sf /dev/stdout /var/log/mysqld.err \
    && chown -h mysql:mysql /var/log/mysqld.err

CMD ["mysqld_safe"]
EXPOSE 3306

Is it required to mysqld take pid=1 to work stdout ? In my case it is some like below.

# ps aux
PID   USER     TIME  COMMAND
1     root     0:00  {mysqld_safe} /bin/sh /usr/bin/mysqld_safe
134   mysql    0:00  /usr/bin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mariadb/plugin --user=mysql --log-error=/var/log/mysqld.err --pid-file=49ea99ae9348.p
166   root     0:00  sh
171   root     0:00  ps aux

Upvotes: 4

Views: 5179

Answers (1)

BMitch
BMitch

Reputation: 264306

You're likely running into these issues:

Something with alpine breaks the ability to access /dev/stdout when you change user accounts. The workaround I've used involves:

  • Running the container with a tty
  • Adding the user inside the container to the tty group
  • Starting the command with a gosu/exec to replace the pid 1 shell script with your app

I'm not sure if the last part was required, and you may not have access to do this with the mysql command. You're already doing the second item. That just leaves the first item that you can implement with:

docker run -t your_image

or in a compose file:

services:
  mysql:
    image: your_image
    tty: true
    ....

The only other option is to run your application directly as mysql instead of starting it as root with user: mysql in the compose file, but that may not be supported by mysql itself.

If none of those work, the option used by the official image is to pick a debian base image instead of the alpine image. You can see their Dockerfile here:

https://github.com/docker-library/mysql/blob/696fc899126ae00771b5d87bdadae836e704ae7d/8.0/Dockerfile

Upvotes: 4

Related Questions