Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-Compose Persistent Data Trouble

I'm having trouble in configuring persistent data with Mariadb. I'm using docker-compose, with each service in a single container (Nginx, PHP-FPM and Mariadb). Everything is working, except Mariadb doesn't store data. Every time I restart the container, I lose all the data. Then I found out that I can use another container just to keep data, and it doesn't even have to be running.

So I'm using, in Mariadb container volume_from content container. But when I do that, when I try to map the volume /var/lib/mysql, the Container MariaDb doesn't start.

Error

2015-12-29 12:16:40 7f2f02e4a780
InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.

The error refers to a problem about volume permissions, but I've tried to set permissions through Dockerfile in both containers, and the problem persists. I'm a bit lost. I'm using OSX, so I believe this is an OSX problem. Can anyone help me on this?

This is my code:

My Docker Compose

content:
  build: containers/content
  container_name: content
  hostname: content
  volumes:
    - /var/lib/mysql
mariadb:
  build: containers/mariadb
  container_name: mariadb
  hostname: mariadb
  ports:
    - "3306:3306"
  volumes_from:
    - content
  environment:
    - MYSQL_ROOT_PASSWORD=mariadb
    - TERM=xterm
    - PORT=3306

MariaDB Dockerfile

FROM debian:jessie

RUN apt-get update && apt-get install -y  mariadb-server

EXPOSE 3306

Content Dockerfile

FROM debian:jessie

VOLUME /var/lib/mysql

CMD ["true"]
like image 511
Daniel Bento Avatar asked Dec 29 '15 12:12

Daniel Bento


1 Answers

The way i do it is that I use busybox for all data stored and shared with mariadb. Then use --volumes-from in mariadb to link that directories. Please have a look into my simpified compose.yml file.

db-data:
  container_name: db-data
  image: busybox:latest
  volumes:
    - /data/mysql:/var/lib/mysql

db:
  container_name: db
  image: million12/mariadb
  restart: always
  volumes_from:
    - db-data
  environment:
    - MARIADB_USER=admin
    - MARIADB_PASS=my_pass

Now all database files are accessible on host os too and there shouldn't be any permissions issues.

Update for docker-compose 2.0

version: '2'
volumes:
  database:

services:
  db:
    container_name: db
    image: million12/mariadb
    restart: always
    volumes_from:
       - database
     environment:
       - MARIADB_USER=admin
       - MARIADB_PASS=my_pass

You can see where docker is storing that volume on your hard drive by running command:
docker volume inspect docker_database

[
{
    "Name": "docker_database",
    "Driver": "local",
    "Mountpoint": "/var/lib/docker/volumes/docker_database/_data",
    "Labels": null,
    "Scope": "local"
}

]

like image 177
Polinux Avatar answered Oct 13 '22 23:10

Polinux