Question

Supposed that I will host two project on one machine and both of them will use apache. Then I need to create two container to separate them. And I need run apt-get install apache2 in both container. Will it take twice space as much as apache2's size?

Was it helpful?

Solution

No, Apache will be shared between both containers. If you create an image, every step will be saved in an own layer. So for example you start with Ubuntu (layer 1) and install Apache (layer 2). Then you add project A to the image (layer 3). In another Docker image you also start with Ubuntu (layer 1) and install Apache (layer 2). But then you add project B to the image (layer 3). Both images will be the same until layer 3. And this means the installation of Apache will be shared.

Here is an example:

Dockerfile for project A:

FROM ubuntu
RUN apt-get update
RUN apt-get install -y apache2
RUN touch /opt/a.txt

Dockerfile for project B:

FROM ubuntu
RUN apt-get update
RUN apt-get install -y apache2
RUN touch /opt/b.txt

Both files will create very e similar images except of the last command. If you look at the history of both images you will the following:

vagrant@ubuntu-13:/vagrant/Apache/b$ docker history test/a
IMAGE               CREATED              CREATED BY                                      SIZE
4dc359259700        About a minute ago   /bin/sh -c touch /opt/a.txt                     8 B
9977b78fbad7        About a minute ago   /bin/sh -c apt-get install -y apache2           54.17 MB
e83b3bf07b42        2 minutes ago        /bin/sh -c apt-get update                       20.67 MB
9cd978db300e        3 months ago         /bin/sh -c #(nop) ADD precise.tar.xz in /       204.4 MB
6170bb7b0ad1        3 months ago         /bin/sh -c #(nop) MAINTAINER Tianon Gravi <ad   0 B
511136ea3c5a        10 months ago                                                        0 B

vagrant@ubuntu-13:/vagrant/Apache/b$ docker history test/b
IMAGE               CREATED              CREATED BY                                      SIZE
c0daf4be2ed4        42 seconds ago       /bin/sh -c touch /opt/b.txt                     8 B
9977b78fbad7        About a minute ago   /bin/sh -c apt-get install -y apache2           54.17 MB
e83b3bf07b42        3 minutes ago        /bin/sh -c apt-get update                       20.67 MB
9cd978db300e        3 months ago         /bin/sh -c #(nop) ADD precise.tar.xz in /       204.4 MB
6170bb7b0ad1        3 months ago         /bin/sh -c #(nop) MAINTAINER Tianon Gravi <ad   0 B
511136ea3c5a        10 months ago                                                        0 B

You see the history of the images build by Dockerfile A and Dockerfile B. You also see the different layers (each line is a layer in the image). As you can see, the first 6 layers (!) are exactly the same in both images. Only the last layer is different (and has a different id). You will see this effect very nice when you build the images. When you build the first image A it will take some minutes, because Apache must be downloaded and so on. But when you build image B afterwards it will only take some seconds, because Apache is not downloaded again, instead the existing layer (here with id 9977b78fbad7) will be used!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top