Without going into details about the different storage backend solutions for Docker (check Docker - About Storage Drivers for reference), docker reuses all the shared intermediate points of an image.
Having said that, even though you see in the docker images output [1.17 GB, 1.17 GB, 1.17 GB, 138MB, 918MB] it does not mean that is using the sum in your storage. We can say the following:
sum(`docker images`) <= space-in-disk
Each of the steps in the Dockerfile creates a layer.
Let's take the following project structure:
├── common-requirements.txt
├── Dockerfile.1
├── Dockerfile.2
├── project1
│ ├── requirements.txt
│ └── setup.py
└── project2
├── requirements.txt
└── setup.py
With Dockerfile.1:
FROM python:3.6-slim
# - here we have a layer from python:3.6-slim -
# 1. Copy requirements and install dependencies
# we do this first because we assume that requirements.txt changes
# less than the code
COPY ./common-requirements.txt /requirements.txt
RUN pip install -r requirements
# - here we have a layer from python:3.6-slim + your own requirements-
# 2. Install your python package in project1
COPY ./project1 /code
RUN pip install -e /code
# - here we have a layer from python:3.6-slim + your own requirements
# + the code install
CMD ["my-app-exec-1"]
With Dockerfile.2:
FROM python:3.6-slim
# - here we have a layer from python:3.6-slim -
# 1. Copy requirements and install dependencies
# we do this first because we assume that requirements.txt changes
# less than the code
COPY ./common-requirements.txt /requirements.txt
RUN pip install -r requirements
# == here we have a layer from python:3.6-slim + your own requirements ==
# == both containers are going to share the layers until here ==
# 2. Install your python package in project1
COPY ./project2 /code
RUN pip install -e /code
# == here we have a layer from python:3.6-slim + your own requirements
# + the code install ==
CMD ["my-app-exec-2"]
The two docker images are going to share the layers with python and the common-requirements.txt. It is extremely useful when building application with a lot heavy libraries.
To compile, I will do:
docker build -t app-1 -f Dockerfile.1 .
docker build -t app-2 -f Dockerfile.2 .
So, think that the order how you write the steps in the Dockerfile does matter.