7

Here I created docker container which use the mariadb image and created three volumes as below.

docker-compose.yml

version: '2.0'

services:
  mariadb:
  image: mariadb:latest
  restart: always
  container_name: mariadb
  environment:
    - MYSQL_ROOT_PASSWORD=root
    - MYSQL_DATABASE=testdb
  ports:
   - 3307:3306
  volumes:
   - ./database:/var/lib/mysql
   - ./_conf/mariadb.cnf:/etc/mysql/my.cnf:ro
   - ./logs:/var/log/mysql

First two volume works successfully but I can't able to find mariadb-logs files into logs folder. I seems logs folder showing blank on host as well as on container(/var/log/mysql). I think host folder override into docker file system.

If I remove this volume(./logs:/var/log/mysql) from docker-compose then logs files are showing on container.

My plan is to mount /var/log/mysql/ folder to local machine.

Thanks !

3
  • Try chmod 777 /var/log/mysql on your host, I bet your daemon running as mysql user has no rights to write in this directory on your host. After that you'll have to find the mysql user id inside the container to make a chown to avoid lettign everyone read an write your db logs.
    – Tensibai
    Sep 8, 2017 at 10:17
  • Perhaps selinux or apparmor is activated
    – 030
    Sep 8, 2017 at 10:21
  • I'm using Debian os and apparmor already deactivate. Sep 18, 2017 at 5:04

3 Answers 3

5

If you use CentOS 7 (as I have ). You may have SELinux turned on by default.check https://www.rootusers.com/how-to-enable-or-disable-selinux-in-centos-rhel-7/ or mount like this:

  volumes:
   - ./database:/var/lib/mysql:Z
   - ./_conf/mariadb.cnf:/etc/mysql/my.cnf:Z
   - ./logs:/var/log/mysql:Z
3

If you're running a RedHat-based distribution e.g. Fedora or CentOS, you may have SELinux turned on by default. You can automatically permit your containers to access files on the hosts by mounting with the :Z option, like so:

  volumes:
   - ./database:/var/lib/mysql:Z
   - ./_conf/mariadb.cnf:/etc/mysql/my.cnf:Z
   - ./logs:/var/log/mysql:Z
1

If /var/log/mysql:/var/log/mysql is defined as a volume then the content that reside in the container's /var/log/mysql/ will be stored in the /var/log/mysql/ folder on the host.

It could be possible that it is not possible to mount the /var/lib/mysql as SElinux or app armor is preventing this.

https://hub.docker.com/_/mysql/

Note that users on host systems with SELinux enabled may see issues with this. The current workaround is to assign the relevant SELinux policy type to the new data directory so that the container will be allowed to access it:

$ chcon -Rt svirt_sandbox_file_t /my/own/datadir
2
  • yes I agree, ./database volume working fine and even I create file on ./logs folder on host then it will show on container but volume not working in revers order. Sep 8, 2017 at 6:53
  • If for example the mysql container from dockerhub is run and the database is stored in /var/lib/mysql, the container is rebooted then the database that reside on the host will be found in the container as well
    – 030
    Sep 8, 2017 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.