Docker Cookbook


Backing Up a Database Running in a Container



Download 6,31 Mb.
Pdf ko'rish
bet34/260
Sana21.04.2022
Hajmi6,31 Mb.
#570749
1   ...   30   31   32   33   34   35   36   37   ...   260
Bog'liq
Docker Cookbook

1.17 Backing Up a Database Running in a Container
Problem
You are using a MySQL image to provide a database service. You need to back up this
database for data persistency.
Solution
Several backup strategies are possible alone or in combination. The two main con‐
cepts with containers are that you can execute a command inside a container running
in the background and that you can also mount a host volume (i.e., a single accessible
30 | Chapter 1: Getting Started with Docker
www.allitebooks.com


storage area in your host filesystem) into the container. In this recipe, you will see
how to do the following:
• Mount a volume from the Docker host into the MySQL container
• Use the 
docker exec
command to call 
mysqldump
Starting from the 
Recipe 1.16
, where you set up a WordPress site by using two linked
containers, you are going to modify the way you start the MySQL container. Once the
containers are started and you have a fully functional WordPress site, you can stop
the containers, which stops your application. At that point, the containers have not
been removed entirely yet and the data in the database is still accessible. However, as
soon as you remove the containers (
docker rm $(docker ps -aq)
), all data will be
lost.
A way to keep the data, even when containers are removed with the 
docker rm -v
command, is to mount a volume from your Docker host inside a container. If you
were to delete the MySQL container with only the 
docker rm
command, the volume
defined by the image would still persist even if you delete the container. If you look at
the 
Dockerfile
used to build the MySQL image, you sill see a reference to
VOLUME /var/lib/mysql
. This means that when you start a container based on this
image, you can bind mount a host directory to this mount point inside the container.
Let’s do it:
$ docker run --name mysqlwp -e MYSQL_ROOT_PASSWORD=wordpressdocker \
-e MYSQL_DATABASE=wordpress \
-e MYSQL_USER=wordpress \
-e MYSQL_PASSWORD=wordpresspwd \
-v /home/docker/mysql:/var/lib/mysql \
-d mysql
Note the 
-v /home/docker/mysql:/var/lib/mysql
line that performs this mount.
After doing the WordPress configuration, the 
/home/docker/mysql
directory on the
host is populated:
$ ls mysql/
auto.cnf ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema wordpress
To get a dump of the entire MySQL database, use the 
docker exec
command to run
mysqldump
inside the container:
$ docker exec mysqlwp mysqldump --all-databases \
--password=wordpressdocker > wordpress.backup
You can then use the traditional techniques for backup and recovery of the database.
For instance, in the cloud, you might want to use an Elastic Block Store (e.g., AWS
EBS) mounted on an instance and then mounted inside a container. You can also
keep your MySQL dumps inside an Elastic Storage (e.g., AWS S3).

Download 6,31 Mb.

Do'stlaringiz bilan baham:
1   ...   30   31   32   33   34   35   36   37   ...   260




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish