Working with the DockerContext¶
The context is a tar file, that is submitted to the API in order to define the image building process. It has to
include the Dockerfile and all necessary other files. The latter are all files referenced to in any
For syntax of
ADD <source> <destination>
source in this case refers to the path inside the build context, i.e. the tar file root.
When you add files to a
it generates a list of used files and directories. These can be automatically added to a
DockerContext. For most common build scenarios, you may start the build process
directly by calling
client.build_from_file(dockerfile, 'new_base_image', add_latest_tag=True, rm=True)
This automatically generates the context and uploads it. However, the context can also be modified further beforehand.
Creating a DockerContext¶
from dockermap.api import DockerContext with DockerContext(dockerfile) as context: ...
This will create a new compressed tar archive, add the generated Dockerfile (string buffer) and the referenced
files. Note that the
DockerFile fill be finalized and cannot be modified further
with (Python context manager syntax) should be used, since
generates a temporary file which is automatically removed at the end of the block.
It is also possible to pass in a path to a file, e.g.:
from dockermap.api import DockerContext with DockerContext(path_to_dockerfile) as context: ...
In that case, referenced files are not added automatically and have to be placed using the following methods.
Adding more files¶
DockerContext provides the methods
addfile(), which refer to
tarfile.TarFile.addfile(). Besides that,
addarchive() copies the contents of another tar archive, including the
structure of files and directories.
Using the context¶
Before sending the file to the Docker Remote API, the underlying tar archive has to be closed. This is handled by
finalize(). Note that the underlying tar archive is closed from that point
and can no longer be modified.
The context tarball is transferred to Docker with
from dockermap.api import DockerClientWrapper, DockerContext client = DockerClientWrapper('unix://var/run/docker.sock') with DockerContext(path_to_dockerfile) as context: ... context.finalize() client.build_from_context(context, 'new_image')
dockermap.map.base.DockerClientWrapper.build_from_file() is only a convenience wrapper around it. It
DockerContext object automatically.
Getting more information¶
Although it may not be relevant in practice, the entire context tarball could be stored to an archive using
save(). By default this is a gzip compressed tar archive, but the actual
method (which also needs to be specified to the Docker Remote API) can be read from the
gzipmeans that the tarball is in the default format, i.e. .tar.gz;
bzip2indicates a bzip compressed tar archive;
Nonemeans that the tar archive is not compressed.