使用Docker创建数据容器

int32位 posted @ Mar 22, 2016 03:31:27 PM in docker , 1915 阅读
转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!

翻译自: Data-only container madness

1.什么是数据容器?

数据容器就是本身只创建一个volume供其他容器共享,创建完后即退出,不执行任何任务。比如通过以下方式创建一个postgres容器。

docker run --name dbdata postgres echo "Data-only container for postgres"

该容器运行echo "Data-only container for postgres"即退出,然而只要没有删除该容器,该容器/var/lib/postgresql/data的volume(在Dockerfile使用VOLUME指令定义)就会一直存在。

然后我们可以新建若干容器来共享数据,比如:

docker run -d --volumes-from dbdata --name db1 postgres

2.如何创建数据容器?

太简单了,创建任何容器,然后使用-v创建volume即可。但大家一定会想到用最小的镜像吧,比如hello-world,即

docker run -v /data hello-world

但这样是错误的! 为什么呢?

我们首先创建一个简单的镜像:

FROM debian:jessie
RUN useradd mickey
RUN mkdir /foo && touch /foo/bar && chown -R mickey:mickey /foo
USER mickey
CMD ls -lh /foo

构建:

docker build -t mickey_foo -< Dockerfile

运行下:

docker run --rm -v /foo mickey_foo

输出:

total 0
-rw-r--r-- 2 mickey mickey 0 Nov 18 05:58 bar

运行正常,没有任何问题。

下面我们尝试使用busybox来作为数据容器:

docker run -v /foo --name mickey_data busybox true
docker run --rm --volumes-from mickey_data mickey_foo

输出:

total 0
# Empty WTF??
docker run --rm --volumes-from mickey_data mickey_foo ls -lh /
total 68K
drwxr-xr-x   2 root root 4.0K Nov 18 06:02 bin
drwxr-xr-x   2 root root 4.0K Oct  9 18:27 boot
drwxr-xr-x   5 root root  360 Nov 18 06:05 dev
drwxr-xr-x   1 root root 4.0K Nov 18 06:05 etc
drwxr-xr-x   2 root root 4.0K Nov 18 06:02 foo
drwxr-xr-x   2 root root 4.0K Oct  9 18:27 home
drwxr-xr-x   9 root root 4.0K Nov 18 06:02 lib
drwxr-xr-x   2 root root 4.0K Nov 18 06:02 lib64
drwxr-xr-x   2 root root 4.0K Nov  5 21:40 media
drwxr-xr-x   2 root root 4.0K Oct  9 18:27 mnt
drwxr-xr-x   2 root root 4.0K Nov  5 21:40 opt
dr-xr-xr-x 120 root root    0 Nov 18 06:05 proc
drwx------   2 root root 4.0K Nov 18 06:02 root
drwxr-xr-x   3 root root 4.0K Nov 18 06:02 run
drwxr-xr-x   2 root root 4.0K Nov 18 06:02 sbin
drwxr-xr-x   2 root root 4.0K Nov  5 21:40 srv
dr-xr-xr-x  13 root root    0 Nov 18 06:05 sys
drwxrwxrwt   2 root root 4.0K Nov  5 21:46 tmp
drwxr-xr-x  10 root root 4.0K Nov 18 06:02 usr
drwxr-xr-x  11 root root 4.0K Nov 18 06:02 var
Owened by root? WTF???
docker run --rm --volumes-from mickey_data mickey_foo touch /foo/ba
touch: cannot touch '/foo/bar': Permission denied

发生了什么呢?我们的/foo 仍然存在, 但是它是空的并且所有者是root

让我们再试试使用我们刚刚构建的mickey_foo作为数据容器:

~: docker rm -v mickey_data # remove the old one
mickey_data
~: docker run --name mickey_data -v /foo mickey_foo true
~: docker run --rm --volumes-from mickey_data mickey_foo
total 0
-rw-r--r-- 1 mickey mickey 0 Nov 18 05:58 bar
# Yes!
~: docker run --rm --volumes-from mickey_data mickey_foo ls -lh /
total 68K
drwxr-xr-x   2 root   root   4.0K Nov 18 06:02 bin
drwxr-xr-x   2 root   root   4.0K Oct  9 18:27 boot
drwxr-xr-x   5 root   root    360 Nov 18 06:11 dev
drwxr-xr-x   1 root   root   4.0K Nov 18 06:11 etc
drwxr-xr-x   2 mickey mickey 4.0K Nov 18 06:10 foo
drwxr-xr-x   2 root   root   4.0K Oct  9 18:27 home
drwxr-xr-x   9 root   root   4.0K Nov 18 06:02 lib
drwxr-xr-x   2 root   root   4.0K Nov 18 06:02 lib64
drwxr-xr-x   2 root   root   4.0K Nov  5 21:40 media
drwxr-xr-x   2 root   root   4.0K Oct  9 18:27 mnt
drwxr-xr-x   2 root   root   4.0K Nov  5 21:40 opt
dr-xr-xr-x 121 root   root      0 Nov 18 06:11 proc
drwx------   2 root   root   4.0K Nov 18 06:02 root
drwxr-xr-x   3 root   root   4.0K Nov 18 06:02 run
drwxr-xr-x   2 root   root   4.0K Nov 18 06:02 sbin
drwxr-xr-x   2 root   root   4.0K Nov  5 21:40 srv
dr-xr-xr-x  13 root   root      0 Nov 18 06:05 sys
drwxrwxrwt   2 root   root   4.0K Nov  5 21:46 tmp
drwxr-xr-x  10 root   root   4.0K Nov 18 06:02 usr
drwxr-xr-x  11 root   root   4.0K Nov 18 06:02 var
# YES!!
~: docker run --rm --volumes-from mickey_data mickey_foo touch /foo/baz
~: docker run --rm --volumes-from mickey_data mickey_foo ls -lh /foo
total 0
-rw-r--r-- 1 mickey mickey 0 Nov 18 06:11 bar
-rw-r--r-- 1 mickey mickey 0 Nov 18 06:12 baz
# YES!!!

由于我们刚刚使用了相同的镜像作为数据容器镜像,共享的容器能够找到共享数据。为什么使用busybox不可以呢?由于busybox没有/foo这个目录,当我们使用-v创建/foo这个数据卷时,docker会以默认用户自动创建对应的目录(这里是root),而--volumes-from仅仅是重用存在的卷,而不会对卷自动做任何事情。因此当我们尝试去写/foo时由于没有权限(root所有,mickey用户).

因此我们应该使用和共享的容器相同的镜像做数据容器镜像?是的!

那我们使用这么大的镜像不会浪费空间么?

3. 为什么不使用小镜像作为数据容器?

其中一个原因,在上一节已经解释。遗留的一个问题是使用这么大的镜像(因为一般的镜像都会比较大)会不会浪费空间呢?

首先我们需要知道Docker的文件系统是如何工作的。Docker镜像是由多个文件系统(只读层)叠加而成。当我们启动一个容器的时候,Docker会加载只读镜像层并在其上(译者注:镜像栈顶部)添加一个读写层。如果运行中的容器修改了现有的一个已经存在的文件,那该文件将会从读写层下面的只读层复制到读写层,该文件的只读版本仍然存在,只是已经被读写层中该文件的副本所隐藏。当删除Docker容器,并通过该镜像重新启动时,之前的更改将会丢失。在Docker中,只读层及在顶部的读写层的组合被称为Union File System(联合文件系统)。

因此当我们创建了一个debian容器实例时(大约150MB),根据以上的原理,我们再创建1000个debian镜像能够重用原来的只读层,需要的空间还是150MB.

容器本身并不会占任何空间,除非你修改了内容。

因此Docker无论创建一个镜像的多少实例,都不会占据更多的空间。

因此实际上,我们为了创建数据容器而使用busybox反而会占用更多的空间,这个空间就是busybox的镜像大小。

实际上我们经常这样使用:

~: docker run --name mydb-data --entrypoint /bin/echo mysql Data-only container for mydb
~: docker run -d --name mydb --volumes-from mydb-data mysql

上面的实例指行/bin/echo mysql Data-only container for mydb,能够更容易知道这是一个数据容器,利于使用grep查找.

转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!
  • 无匹配
  • 无匹配
Digital Ali 说:
2021年9月13日 19:41

This is the type of information I’ve long been trying to find. Thank you for writing this information. soap2day.com

Digital Ali 说:
2021年9月15日 17:05

Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts. Observer

Digital Ali 说:
2021年9月15日 19:22

I gotta favorite this website it seems very helpful . <a href="https://movies123free.top/">123movies</a>

SEO 说:
2021年10月01日 19:43

This blog is so nice to me. I will keep on coming here again and again. Visit my link as well.. you can try this out

SEO 说:
2021年10月10日 00:29

Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. SEO เว็บพนัน

Best Laptop For Medi 说:
2021年10月24日 04:31

What makes a good laptop for medical school? Reviewer Mate presents the ULTIMATE guide to buying the best laptop for medical school.

SEO 说:
2021年10月31日 14:39

I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here. bandarq pkv

bestmotherboardz 说:
2021年11月07日 17:04

<a href="https://bestmotherboardz.com/">bestmotherboardz</a> helps you improve your system and stay one step ahead when playing with multiple contenders.

bestlaptopviews 说:
2021年11月08日 05:58

bestlaptopviews we provide powerful, light and with a high screen resolution, therefore, we are talking about a minimum of 4-8 GB of RAM, Intel Core i5 or i7 processors and a minimum resolution of 1920 x 1080 pixels.

MakSitesLike 说:
2021年11月09日 04:06

MakSitesLike We Provide You With The New Latest Breaking News: And Videos Straight From The Entertainment Industry.

Question Paper 2022 说:
2021年11月10日 22:46

The approaching First Term Board Exams will reveal this to students. Along with the question papers, the board has also provided all of the subjects, Question Paper 2022 which will assist in understanding the mark distribution used during the evaluation of answer copies. The download links are provided below. When the board first announced the term-by-term assessment strategy, they made it clear.

miichellle.rossee 说:
2021年11月14日 16:46

Reliable, affordable and flexible veeam fedramp compliant protection with Off-Site Cloud Connect Backup Repository to cover your security threats available from Sora Solutions Services. Get started today. Visit https://welcome.sora-solutions.services/en/veeam/ for more information.

Website 说:
2021年12月09日 03:30

HGH injections to lose weight are essential in hormonal growth, weight and strength. For enhancement and overcoming deficiency we need growth hormonal injections and therapy. Our specialists at HGH MD will be glad to discuss your hormone treatment alternatives with you. For a consultation call 888-763-4221 today!

Website 说:
2021年12月13日 16:08

Sora Solutions offer solutions for monitoring and trying to solve IT issues in information technology and data centers. From an IT perspective, Sora Solutions will work with customers to determine the best ways to improve their Data Center Asset Management. Watch your data center at all times in real-time and make informed decisions in event of issues. Real-time monitoring and alarms that can detect violations of policies and power usage together with scheduled reporting and an elaborate reporting library give clients peace of mind knowing that their data is secure. Call today! +41432338888

Website 说:
2021年12月14日 03:31

Looking for a mountain bike, but on a budget? Find the best mountain bikes under 500. Discover reviews on the best bikes for all age groups.

MBOSE +1 Model Paper 说:
2022年8月22日 02:52

The Meghalaya Board of School Examinations (MBOSE) was established with the primary goal of advancing education in the state. It administers the H9th and the Secondary School Leaving Certificate (9th) exams. The main headquarters and regional offices of this board are located in Shillong, the state capital. The Meghalaya Board will release the MBOSE Class 11th test Question Paper 2023 in the month of May on the same official web portal site, according to the official announcement. MBOSE +1 Model Paper 2023 The students who are seeking for the Meghalaya Board Important Question Paper 2023 of the 11th class may download it from the official web portal site, and we also offer the direct link to check your Meghalaya 11th class.

global news 说:
2023年2月01日 03:36

Glad to chat your blog, I seem to be forward to more reliable articles and I think we all wish to thank so many good articles, blog to share with us.

Cladder 说:
2023年2月05日 04:45

Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates.

Ower Shelf 说:
2023年3月22日 04:48

It’s very informative and you are obviously very knowledgeable in this area. You have opened my eyes to varying views on this topic with interesting and solid content.

amarnath helicopter 说:
2024年6月06日 07:49

wow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmark your site and also add rss. keep us updated.

tech 说:
2024年6月08日 05:55

You have done a amazing job with you website eviza e urgjencës për të vizituar Turqinë

tech 说:
2024年9月06日 09:39

Keep up the excellent job; I've read a few entries on your page and find your blog to be intriguing with a wealth of amazing information. I appreciate all of your hard work. 印度 e 簽證資格 多明尼克公民

tech 说:
2024年10月17日 12:52

Hey, I'm having too fantastic of a day because I'm reading this really educational essay at home right now. I appreciate your really hard effort. индийска електронна виза за граждани на Белиз


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter