24/01/2018 - DOCKER
In this example, we are going to store MySQL data inside a folder which is on host machine rather than the MySQL container. With this way, you can remove and re-create container as many times as you wish but your data will never be lost.
FROM ubuntu:16.04
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server \
&& sed -i "s/127.0.0.1/0.0.0.0/g" /etc/mysql/mysql.conf.d/mysqld.cnf \
&& chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
VOLUME ["/var/lib/mysql"]
EXPOSE 3306
CMD ["mysqld_safe"]
#!/bin/bash
set -e
mkdir /var/run/mysqld
chown -R mysql:mysql /var/run/mysqld /var/lib/mysql
if [ ! -f /var/lib/mysql/ibdata1 ]; then
mysqld --initialize-insecure;
fi
exec "$@"
data/*
ubuntu@linux:~$ ls -la
-rw-rw-r-- 1 ubuntu ubuntu 378 Jan 24 20:02 Dockerfile
-rw-rw-r-- 1 ubuntu ubuntu 8 Jan 24 20:02 .dockerignore
-rw-rw-r-- 1 ubuntu ubuntu 185 Jan 24 20:02 entrypoint.sh
ubuntu@linux:~$ docker build -t mysql_image .
ubuntu@linux:~$ docker run -i -t -d -v `pwd`/data:/var/lib/mysql --name mysql_container mysql_image
As you can see below, the data
folder has been created and has all the MySQL data stored in it.
ubuntu@linux:~$ ls -l
drwxr-xr-x 5 _apt input 4096 Jan 24 17:28 data
ubuntu@linux:~$ ls -l data/
-rw-r----- 1 _apt input 56 Jan 24 17:28 auto.cnf
-rw-r----- 1 _apt input 420 Jan 24 17:28 ib_buffer_pool
-rw-r----- 1 _apt input 12582912 Jan 24 17:28 ibdata1
-rw-r----- 1 _apt input 50331648 Jan 24 17:28 ib_logfile0
-rw-r----- 1 _apt input 50331648 Jan 24 17:28 ib_logfile1
-rw-r----- 1 _apt input 12582912 Jan 24 17:28 ibtmp1
drwxr-x--- 2 _apt input 4096 Jan 24 17:28 mysql
drwxr-x--- 2 _apt input 4096 Jan 24 17:28 performance_schema
drwxr-x--- 2 _apt input 12288 Jan 24 17:28 sys
If you login to container, create databases, tables, users etc. all the relevant information will be visible in data/
folder of host OS. You can remove container and re-create it. You won't lose any data.