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 !

Nullpointer
  • 255
  • 1
  • 4
  • 11
  • 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 08 '17 at 10:17
  • Perhaps selinux or apparmor is activated – 030 Sep 08 '17 at 10:21
  • I'm using Debian os and apparmor already deactivate. – Nullpointer Sep 18 '17 at 05:04

3 Answers3

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
user2878022
  • 51
  • 1
  • 2
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
user2640621
  • 1,395
  • 8
  • 20
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
030
  • 13,235
  • 16
  • 74
  • 173
  • 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. – Nullpointer Sep 08 '17 at 06: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 08 '17 at 07:07