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

<pre><code>  volumes:
   - ./database:/var/lib/mysql:Z
   - ./_conf/mariadb.cnf:/etc/mysql/my.cnf:Z
   - ./logs:/var/log/mysql:Z
</code></pre>

CC BY-SA 4.0
2018-12-12T12:57:31.657Z
2018-12-12T12:57:31.657Z
user2878022
<p>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 <code>:Z</code> option, like so:</p>

<pre><code>  volumes:
   - ./database:/var/lib/mysql:Z
   - ./_conf/mariadb.cnf:/etc/mysql/my.cnf:Z
   - ./logs:/var/log/mysql:Z
</code></pre>

CC BY-SA 3.0
2017-09-17T08:43:35.020Z
2017-09-17T08:43:35.020Z
user2640621
<p>If <code>/var/log/mysql:/var/log/mysql</code> is defined as a volume then the content that reside in the container's <code>/var/log/mysql/</code> will be stored in the <code>/var/log/mysql/</code> folder on the host.</p>

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

<p><a href="https://hub.docker.com/_/mysql/" rel="nofollow noreferrer">https://hub.docker.com/_/mysql/</a></p>

<blockquote>
  <p>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:</p>

<pre><code>$ chcon -Rt svirt_sandbox_file_t /my/own/datadir
</code></pre>
</blockquote>

CC BY-SA 3.0
2017-09-08T06:53:31.007Z
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
CC BY-SA 3.0
2017-09-08T07:07:20.603Z
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
CC BY-SA 3.0
2017-09-08T06:25:44.323Z
2017-09-08T10:20:37.583Z
2017-09-08T10:20:37.583Z
030
<p>Here I created docker container which use the <code>mariadb</code> image and created three volumes as below.</p>
<h3>docker-compose.yml</h3>
<pre><code>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
</code></pre>
<p>First two volume works successfully but I can't able to find mariadb-logs files into <code>logs</code> folder. I seems <code>logs</code> folder showing blank on host as well as on container(/var/log/mysql). I think host folder override into docker file system.</p>
<p>If I remove this volume(<code>./logs:/var/log/mysql</code>) from docker-compose then logs files are showing on container.</p>
<p>My plan is to mount <code>/var/log/mysql/</code> folder to local machine.</p>
<p>Thanks !</p>

CC BY-SA 3.0
2017-09-08T10:17:45.073Z
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
CC BY-SA 3.0
2017-09-08T10:21:21.857Z
Perhaps selinux or apparmor is activated
030
CC BY-SA 3.0
2017-09-18T05:04:28.830Z
I'm using `Debian` os and apparmor already deactivate.
Nullpointer
<docker><docker-compose>
Unable to mount docker folder into host using docker-compose
Nullpointer
# Unable to mount docker folder into host using docker-compose

(Asked by: username_0 on 2017-09-08)

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 !

**Tags:** docker, docker-compose

> **Comments:**
> **username_1**: 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.  
> **username_2**: Perhaps selinux or apparmor is activated  
> **username_0**: I'm using `Debian` os and apparmor already deactivate.  

---

## 3 answers

### Answer by username_3 on 2018-12-12. Score: 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


---

### Answer by username_4 on 2017-09-17. Score: 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


---

### Answer by username_2 on 2017-09-08. Score: 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

> **Comments:**
> **username_0**: 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.  
> **username_2**: 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  

---

