Docker¶
How to remove extra project files from docker image¶
We use the .dockerignore
file for this. It marks project files to skip when building the docker image.
-
Look into the image to find extra unneeded files
-
Run a shell in the docker image
docker run -it image_name sh
-
Look at the directory structure
ls
-
-
Add the extra files and paths in
.dockerignore
-
Rebuild the image
Cache mount¶
This helps speed up subsequent docker builds by caching intermediate files and reusing them across builds. It's available with docker buildkit. The key here is to disable anything that could delete the cache, because we want to preserve it. The cache mount is not going to end up in the docker image being built, so there's no concern about disk space usage.
Put this flag between RUN
and the command
RUN \
--mount=type=cache,target=/root/.cache
pip install -r requirements.txt
For pip, the files are by default stored in /root/.cache/pip
. Pip caching docs
For apk, the cache directory is /var/cache/apk/
. APK wiki on local cache
Reducing the image size¶
There are methods to do this on many levels. All of these methods contribute to reduce the final image size, either by skipping generation of intermediate files or by removing them afterward. We list the commonly-recommended methods here although we opted to use cache mount instead, which speeds up image rebuilds. The methods discussed here may be more suitable for a CI environment.
!!! Note "mkdocs-material
babel
dependency
`mkdocs-material` theme added `babel` as a dependency starting at version 9.2. As a result, the docker image size increased from <30MB to around 40MB. This is unavoidable.
Docker¶
-
Docker cache mount
We use this method instead of ones which disable caching. See cache mount above. There's no need to delete any files since they're in a cache mount that's not part of the docker image.
Python¶
-
Skip bytecode (.pyc) generation
Python docs on
PYTHONDONTWRITEBYTECODE
Set this environment variable
ENV PYTHONDONTWRITEBYTECODE 1
Set the
-B
flag for pythonRUN python3 -B -m pip install -r requirements.txt
-
Pycache prefix and rm
Python docs on
PYTHONPYCACHEPREFIX
-
Tell python to write
.pyc
files in a mirror directorySet this environment variable to make python store all pycache bytecode files under some directory
ENV PYTHONPYCACHEPREFIX=/root/.cache/pycache/
Use the commandline flag for python
RUN python3 -X pycache_prefix=/root/.cache/pycache/ -m pip install -r requirements.txt
-
Remove the files in the same RUN command by appending this to the end
&& rm -rf /root/.cache/pycache/
-
Pip¶
-
Don't compile python into byte code
Pass the flag into pip to skip generating
pyc
files during installRUN pip install --no-compile -r requirements.txt
-
Disable caching
Set this environment variable
ENV PIP_NO_CACHE_DIR=1
Pass this flag into pip
RUN pip install --no-cache-dir -r requirements.txt
Clean build¶
Combineable flags can be passed into a docker or docker-compose build to force a clean build. See docker build options
-
Try to download the latest base image
docker-compose build --pull
-
Disable caching. Build everything
docker-compose build --no-cache