Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove build dirs from docker image to keep the layers small #5243

Merged
merged 5 commits into from Jan 14, 2017

Conversation

michaelarnauts
Copy link
Contributor

Description:
Remove the build dir of script/build_libcec and script/build_python_openzwave from the Docker image. This saves 168 MB.

Before rm -rf build/

$ docker history home-assistant
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
02b30b578835        6 seconds ago       /bin/sh -c #(nop)  CMD ["python" "-m" "homeas   0 B                 
4ef301ec282a        7 seconds ago       /bin/sh -c #(nop) COPY dir:bd740fb4658fae4fd2   21.75 MB            
09c00b87ca2c        9 seconds ago       /bin/sh -c pip3 install --no-cache-dir -r req   271.8 MB            
b63682b5de86        5 minutes ago       /bin/sh -c #(nop) COPY file:dc1b865eaa2aa9bf6   16.24 kB            
16324eab1258        5 minutes ago       /bin/sh -c script/build_libcec                  12.85 MB            <---
6537d1cdd54d        5 minutes ago       /bin/sh -c #(nop) COPY file:a4027cf23c2eb7375   1.633 kB            
14fb00dc5a84        5 minutes ago       /bin/sh -c script/build_python_openzwave &&     238 MB              <---              
6de3c5b76b43        9 minutes ago       /bin/sh -c #(nop) COPY file:c477b2f2f3970e8f9   702 B               
895812216a1d        9 minutes ago       /bin/sh -c echo "deb http://download.telldus.   90.34 MB            
2d72e86d9b73        6 weeks ago         /bin/sh -c pip3 install --no-cache-dir colorl   31.86 MB            
a3ef84a2e51c        6 weeks ago         /bin/sh -c #(nop)  WORKDIR /usr/src/app         0 B                 
459e745c5712        6 weeks ago         /bin/sh -c mkdir -p /usr/src/app                0 B                 
03cbf1acb362        6 weeks ago         /bin/sh -c #(nop)  VOLUME [/config]             0 B                 
aaaa1646019d        6 weeks ago         /bin/sh -c #(nop)  MAINTAINER Paulus Schoutse   0 B                 
7045ed20ac61        7 weeks ago         /bin/sh -c #(nop)  CMD ["python3"]              0 B                 
...

$ docker images home-assistant
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
home-assistant      latest              02b30b578835        15 seconds ago      1.349 GB

After rm -rf build/

$ docker history home-assistant-rmbuild
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
74c26e9916bc        20 seconds ago      /bin/sh -c #(nop)  CMD ["python" "-m" "homeas   0 B                 
76a6b13070d6        21 seconds ago      /bin/sh -c #(nop) COPY dir:b61ed18e451c6ba996   21.74 MB            
3cd5fe5c8ab4        23 seconds ago      /bin/sh -c pip3 install --no-cache-dir -r req   271.8 MB            
8a3456dc8896        5 minutes ago       /bin/sh -c #(nop) COPY file:dc1b865eaa2aa9bf6   16.24 kB            
7501089a09e8        5 minutes ago       /bin/sh -c script/build_libcec &&     rm -rf    2.687 MB             <---
f4986e886727        5 minutes ago       /bin/sh -c #(nop) COPY file:a4027cf23c2eb7375   1.633 kB            
ba533fb92b99        5 minutes ago       /bin/sh -c script/build_python_openzwave &&     79.52 MB           <---
6de3c5b76b43        21 minutes ago      /bin/sh -c #(nop) COPY file:c477b2f2f3970e8f9   702 B               
895812216a1d        21 minutes ago      /bin/sh -c echo "deb http://download.telldus.   90.34 MB            
2d72e86d9b73        6 weeks ago         /bin/sh -c pip3 install --no-cache-dir colorl   31.86 MB            
a3ef84a2e51c        6 weeks ago         /bin/sh -c #(nop)  WORKDIR /usr/src/app         0 B                 
459e745c5712        6 weeks ago         /bin/sh -c mkdir -p /usr/src/app                0 B                 
03cbf1acb362        6 weeks ago         /bin/sh -c #(nop)  VOLUME [/config]             0 B                 
aaaa1646019d        6 weeks ago         /bin/sh -c #(nop)  MAINTAINER Paulus Schoutse   0 B                 
7045ed20ac61        7 weeks ago         /bin/sh -c #(nop)  CMD ["python3"]              0 B                 
...

$ docker images home-assistant-rmbuild
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
home-assistant-rmbuild   latest              74c26e9916bc        28 seconds ago      1.181 GB

Related issue (if applicable): fixes #

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.github.io#<home-assistant.github.io PR number goes here>

Example entry for configuration.yaml (if applicable):

Checklist:

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

If the code does not interact with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

@mention-bot
Copy link

@michaelarnauts, thanks for your PR! By analyzing the history of the files in this pull request, we identified @balloob, @armills and @allanglen to be potential reviewers.

@emlove
Copy link
Contributor

emlove commented Jan 9, 2017

I agree, although I would prefer one rm -rf build/ at the end of all source builds.

We could also remove development packages installed here: https://github.com/michaelarnauts/home-assistant/blob/6bf321e932e3441d0ccc891f0dd4757864c25296/Dockerfile#L15 cmake, swig, *-dev, etc.

@michaelarnauts
Copy link
Contributor Author

michaelarnauts commented Jan 9, 2017 via email

@emlove
Copy link
Contributor

emlove commented Jan 9, 2017

Good point. Nevermind!

@emlove
Copy link
Contributor

emlove commented Jan 9, 2017

What are your thoughts on copying the build scripts into docker first, and moving all of the compilation/apt-get functions into a script that is executed in a single RUN directive? Something like below. We could get rid of a lot of the line wrapping that way.

#!/bin/sh
# script/setup_docker_prereqs(?) something like that
set -e

echo "deb http://download.telldus.com/debian/ stable main" >> /etc/apt/sources.list.d/telldus.list
wget -qO - http://download.telldus.se/debian/telldus-public.key | apt-key add -
apt-get update
apt-get install -y --no-install-recommends nmap net-tools cython3 libudev-dev sudo libglib2.0-dev bluetooth libbluetooth-dev \
    libtelldus-core2 cmake libxrandr-dev swig

script/build_python_openzwave
mkdir -p /usr/local/share/python-openzwave
mv /usr/src/app/build/python-openzwave/openzwave/config /usr/local/share/python-openzwave/config

script/build_libcec

rm -rf build/
apt-get remove cmake swig, ...
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

@michaelarnauts
Copy link
Contributor Author

That could be a good idea. I can set something up tomorrow.

@michaelarnauts
Copy link
Contributor Author

This cleans it up to 1113 GB, a saving of 236MB.
I'm not sure if libbluetooth-dev and libglib2.0-dev are also required in PACKAGES. Does anybody know why they are added?

@michaelarnauts
Copy link
Contributor Author

michaelarnauts commented Jan 10, 2017

Removing libbluetooth-dev and libglib2.0-dev saves us another megabyte. The build of openzwave and libcec seems to work out fine, but maybe another python library has a dependency on this?

@balloob
Copy link
Member

balloob commented Jan 10, 2017

Love the idea of a script. That way we can also share it between the Dockerfiles without having to try to keep them in sync.

bluetooth is needed for one of the bluetooth trackers. We should probably start documenting why each package was added.

@michaelarnauts
Copy link
Contributor Author

I understand for libbluetooth, but also libbluetooth-dev? I guess dev packages should only be available when we compile stuff. I should check the code of the Bluetooth tracker...

@michaelarnauts
Copy link
Contributor Author

  • libglib2.0-dev got added for bluepy in 177d0c2, but it seems libglib2.0-dev is only needed for building. I think this dependency can be dropped.
  • libbluetooth-dev and libbluetooth got added for PyBluez in 91e36f3, but it seems libbluetooth-dev is only needed for building. I think the dependency of libbluetooth-dev can be dropped.

@philhawthorne, do you know for sure if libbluetooth-dev is needed (you've made the initial PR for this)? The build seems to go okay, but I don't have bluetooth on the device where I can test.

…first step of Dockerfile since it will be installed later on anyway. Drop libglib2.0-dev and libbluetooth-dev
COPY script/build_libcec script/build_libcec
RUN script/build_libcec
# Copy build scripts
COPY script/setup_docker_prereqs script/build_python_openzwave script/build_libcec script/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to just copy the whole script directory for simplicity? It's all going to get copied over when the source directory is copied anyway. We could even promote the COPY . . line to before running the scripts so everything is available.

Again, docker isn't my forte so I'm not sure if increasing the size of intermediate images is a concern.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only want to copy the file you need at that step, because when a change is made to a different file (that can be unrelated to what you need at that point), the whole docker cache gets invalidated, and it has to rebuild everything again.

Therefore, you definitely don't want to add a COPY . . in the beginning of the Dockerfile, since every tiny change in any file triggers will force docker to rebuild everything from up to the COPY . .-line.

Interesting literature here: https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK that makes sense.

@michaelarnauts
Copy link
Contributor Author

I think this is okay now. I just would like a confirmation that bluetooth in Docker is still okay.

@michaelarnauts
Copy link
Contributor Author

We are at 1.09 GB now, a total saving of 259 MB :)

$ docker images home-assistant
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
home-assistant      latest              19e526e3aa36        10 minutes ago      1.09 GB
$ docker history home-assistant
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
19e526e3aa36        9 minutes ago       /bin/sh -c #(nop)  CMD ["python" "-m" "homeas   0 B                 
277032de9982        9 minutes ago       /bin/sh -c #(nop) COPY dir:46ab3cda5adeedc979   21.75 MB            
f1de97456ae2        9 minutes ago       /bin/sh -c pip3 install --no-cache-dir -r req   271.9 MB            
114ea9b14878        13 minutes ago      /bin/sh -c #(nop) COPY file:dc1b865eaa2aa9bf6   16.24 kB            
8819015f88c1        13 minutes ago      /bin/sh -c script/setup_docker_prereqs          114.1 MB            
a6fb64de5d6b        18 minutes ago      /bin/sh -c #(nop) COPY multi:35bdcb2f58887554   3.818 kB            
a3ef84a2e51c        6 weeks ago         /bin/sh -c #(nop)  WORKDIR /usr/src/app         0 B                 
459e745c5712        6 weeks ago         /bin/sh -c mkdir -p /usr/src/app                0 B                 
03cbf1acb362        6 weeks ago         /bin/sh -c #(nop)  VOLUME [/config]             0 B                 
aaaa1646019d        6 weeks ago         /bin/sh -c #(nop)  MAINTAINER Paulus Schoutse   0 B                 
7045ed20ac61        7 weeks ago         /bin/sh -c #(nop)  CMD ["python3"]              0 B                 
...

@philhawthorne
Copy link
Contributor

@michaelarnauts the Bluetooth libraries were added because the Bluetooth tracker wouldn't work in Docker without those libraries installed.

I am not sure if it is still an issue, so give me a day or so and I'll build this new docker file without the Bluetooth libraries, and see if the Bluetooth device tracker still works.

The libraries installed were based on what was needed for the Bluetooth Component and Bluetooth LE component. By the looks of the documentation the LE tracker requires the dev versions.

Requires PyBluez. If you are on Raspbian, run the following command to install the needed dependencies. sudo apt install bluetooth libbluetooth-dev pkg-config libboost-python-dev libboost-thread-dev libglib2.0-dev python-dev

@balloob
Copy link
Member

balloob commented Jan 11, 2017

Some Python packages compile things when they get installed by pip. I would not be surprised if both Bluetooth dependencies do this and thus the headers are required.

@philhawthorne
Copy link
Contributor

Okay so I ran this branch, and edited the docker_prereqs file to remove the bluetooth libraries.

After removing them, HASS can no longer use the bluetooth tracker component.

17-01-14 01:19:50 INFO (MainThread) [homeassistant.loader] Loaded device_tracker.bluetooth_tracker from homeassistant.components.device_tracker.bluetooth_tracker
17-01-14 01:19:50 INFO (Thread-9) [homeassistant.util.package] Attempting install of pybluez==0.22
Command "/usr/local/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-gvdwv1y2/pybluez/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-uijknom1-record/install-record.txt --single-version-externally-managed --compile --home=/tmp/tmpt25f_n6z" failed with error code 1 in /tmp/pip-build-gvdwv1y2/pybluez/
17-01-14 01:20:09 ERROR (Thread-9) [homeassistant.bootstrap] Not initializing device_tracker.bluetooth_tracker because could not install dependency pybluez==0.22
17-01-14 01:20:09 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=persistent_notification, service=create, service_data=notification_id=invalid_config, message=The following components and platforms could not be set up:
* device_tracker.bluetooth_tracker

So looks like the bluetooth libraries will still need to be installed in the Docker image

@balloob
Copy link
Member

balloob commented Jan 14, 2017

Yes, we should not remove any packages that were there before.

@michaelarnauts
Copy link
Contributor Author

@philhawthorne you removed the bluetooth package, I meant the libbluetooth-dev package, what is already dropped in the branch. If this branch works correctly without modifying anything, libbluetooth-dev and libglib2.0-dev (with their dependencies!) aren't needed. I'm quite certain that the bluetooth package itself is needed.

@balloob I think it would benefit the Docker image if we know exactly what libraries are needed, and what not. It doesn't make sense to install development libraries if they are not needed.

@michaelarnauts
Copy link
Contributor Author

I've added them back anyway. If they should turn out to be unnecessary, we can always remove them in the future.

@balloob
Copy link
Member

balloob commented Jan 14, 2017

Like I said, I'm quite certain that they compile helper C libs for the Bluetooth packages and thus need the headers

@michaelarnauts
Copy link
Contributor Author

Indeed. I've added them back.

@balloob balloob merged commit d4eabaf into home-assistant:dev Jan 14, 2017
@colinodell
Copy link
Contributor

@balloob @michaelarnauts This change broke zwave - see #5328 for the issue and #5329 for a fix.

@michaelarnauts
Copy link
Contributor Author

Oops! Missed that... Thanks for the fix!

@home-assistant home-assistant locked and limited conversation to collaborators Apr 30, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants