2

I have created database.sql file and added to a folder inside the container so that it can be used by the application.I want that my data should remain persistent even when my container is removed.I tried using volume.But how to add .sql file and make that file consistent.

sudo docker -v /datadir sqldb 

here, sqldb is database image name and datadir is mount folder.

Dockerfile of sqldb:

FROM ubuntu:latest

RUN apt-get update
RUN apt-get upgrade -y

RUN apt-get -y install mysql-client mysql-server curl
ADD ./my.cnf /etc/mysql/my.cnf

RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf

ADD database.sql /var/db/database.sql

ENV user root
ENV password password
ENV url file:/var/db/database.sql
ENV right WRITE

ADD ./start-database.sh /usr/local/bin/start-database.sh
RUN chmod +x /usr/local/bin/start-database.sh
EXPOSE 3306

CMD ["/usr/local/bin/start-database.sh"]

start-database.sh file

#!/bin/bash

# This script starts the database server.
echo "Creating user $user for databases loaded from $url"
a
# Import database if provided via 'docker run --env url="http:/ex.org/db.sql"'
echo "Adding data into MySQL"
/usr/sbin/mysqld &
sleep 5
curl $url -o import.sql

# Fixing some phpmysqladmin export problems
sed -ri.bak 's/-- Database: (.*?)/CREATE DATABASE \1;\nUSE \1;/g' import.sql

# Fixing some mysqldump export problems (when run without --databases switch)
# This is not tested so far
# if grep -q "CREATE DATABASE" import.sql; then :; else sed -ri.bak 's/-- MySQL dump/CREATE DATABASE `database_1`;\nUSE `database_1`;\n-- MySQL dump/g' import.sql; fi

mysql --default-character-set=utf8 < import.sql
rm import.sql
mysqladmin shutdown
echo "finished"

# Now the provided user credentials are added
/usr/sbin/mysqld &
sleep 5
echo "Creating user"
echo "CREATE USER '$user' IDENTIFIED BY '$password'" | mysql --default-character-set=utf8
echo "REVOKE ALL PRIVILEGES ON *.* FROM '$user'@'%'; FLUSH PRIVILEGES" | mysql --default-character-set=utf8
echo "GRANT SELECT ON *.* TO '$user'@'%'; FLUSH PRIVILEGES" | mysql --default-character-set=utf8
echo "finished"

if [ "$right" = "WRITE" ]; then 
echo "adding write access"
echo "GRANT ALL PRIVILEGES ON *.* TO '$user'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES" | mysql --default-character-set=utf8
fi

# And we restart the server to go operational
mysqladmin shutdown
cp /var/db/database.sql /var/lib/docker/volumes/mysqlvol/database.sql
echo "Starting MySQL Server"
/usr/sbin/mysqld
Musa Haidari
  • 2,109
  • 5
  • 30
  • 53
vicky
  • 81
  • 2
  • 4

1 Answers1

2

To keep data persisted even when container is removed please use volumes. For more info look at:

I hope that helps. If not please ask.

Community
  • 1
  • 1
Fuxi
  • 5,298
  • 3
  • 25
  • 35
  • I have my database in /var/lib/mysql folder.how i can link my application container to it,so that it can use my existing database. – vicky Dec 17 '15 at 05:04
  • how i can add var/lib/mysql data into my volume i.e var/lib/docker/volumes/mysqlvol... – vicky Dec 17 '15 at 05:42
  • You need to add something like `VOLUME /data/mysql` in your docker file, then start container with `-v /var/lib/mysql:/data/mysql:rw`. That should mount your mysql data into container under `/data/mysql` – Fuxi Dec 17 '15 at 09:04
  • here, what is /data/mysql ? is it the local folder on your machine to which mount is done.where i should create this folder. – vicky Dec 17 '15 at 09:38
  • `/data/mysql` is a folder in container from where mysql can read data (you can use any folder you'd like. Adding `VOLUME /data/mysql` to dockerfile should create it. If not just add `RUN mkdir /data/mysql` – Fuxi Dec 17 '15 at 12:43
  • I tried this,but still my application is not able to get my existing database – vicky Dec 18 '15 at 07:02
  • Can you share any error you have. It's hard to advice without. – Fuxi Dec 18 '15 at 08:58
  • i had run my database container in intermediate mode and checked for the my database,but it was not their inside /var/lib/mysql folder – vicky Dec 18 '15 at 09:37
  • If you want to have db data in `/var/lib/mysql` you need to adjust `/data/mysql` to `/var/lib/mysql` – Fuxi Dec 18 '15 at 11:31
  • when i run the command to mount the mysql folder it shows error can't cannot to local mysql server.command: **sudo docker run -v /home/docker_admin/dbmount:/var/lib/mysql --name mysqlmock sqlmock ** here,sqlmock is my database image – vicky Dec 21 '15 at 05:07
  • 151221 5:10:40 [Note] /usr/sbin/mysqld (mysqld 5.5.46-0ubuntu0.14.04.2) starting as process 5 ... Creating user ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' adding write access ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) mysqladmin: connect to server at 'localhost' failed – vicky Dec 21 '15 at 05:14
  • Ok, you need to go thought the mysql docs and figure it out by your self. I would advise to move data to `/data/mysql` so you will not have problems like that. At this point it's mysql configuration issue – Fuxi Dec 21 '15 at 08:27
  • sudo docker run -v /data/mysql:/var/lib/mysql sqldb when i tried this,sql server runs but my application is not able to find my database. – vicky Dec 21 '15 at 11:14
  • You can change where mysql will be looking for data and/or it's socket. Have a look at http://www.mkyong.com/mysql/where-does-mysql-stored-the-data-in-my-harddisk/ – Fuxi Dec 21 '15 at 14:12