9

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"]
Siyual
  • 16,415
  • 8
  • 44
  • 58
Daniel Bento
  • 111
  • 2
  • 7

1 Answers1

9

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"
}

]

Polinux
  • 964
  • 1
  • 6
  • 8
  • Thanks I'll try that. – Daniel Bento Dec 29 '15 at 14:26
  • It worked. I was creating my own dockerfile to Mariadb (it's not wrong, but harder), and I was using docker rm every time I was closing Docker. But now, with that image that doesn't happen. The problem was not exactly solved, but this is a good solution also. – Daniel Bento Dec 29 '15 at 14:44
  • 3
    This article suggests that you should use the same image for the db and data-volume containers: http://container42.com/2014/11/18/data-only-container-madness/ – toast38coza Feb 17 '16 at 19:41
  • What's the advantage of this approach versus having the data in the same container as the db? – Edson Medina Nov 30 '16 at 10:38
  • thanks. this might be a handy command to know, to see what the volume name is, to inspect the volume: docker volume ls – michel.iamit Jan 05 '17 at 11:01
  • This still worked for me in 2022 with mysql-server:8.0 and the cool thing is you can leave the my.cnf alone! (unlike the suggestions provided with the mysql docker image.) Now on to step 2, which is to get phpmyadmin running in another container in the stack. – Steve L May 26 '22 at 19:00