You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add support for pointing to tarball contexts in the 'build' parameter. #1209
Since the docker client used by compose (docker-py) supports receiving externally generated tarballs as a parameter for its build() method, this alternative should be available in the project configuration file. i.e.:
service:
build: path/to/tarball.tar.gz
The contents within the compressed file should pass the same level of validation as the currently supported directory paths (for example, 'there must be a Dockerfile at the root inside the tar archive'). Aside from that, the general idea is that the archive is some kind of externally validated artifact, auto-generated by a continuous integration stack, so there should be no need for parsing .dockerignore.
If this sounds like a good idea I can start working on a PR (I already have an working prototype).
Reacted by Matías Iturburu, IPetrik, Ben Mosher, Andrew Craft, Lovelin B, Giórgenes Gelatti and Maxi Herczegh
changed the title [-]Add support for pointing to tarball contexts on the 'build' parameter.[/-][+]Add support for pointing to tarball contexts in the 'build' parameter.[/+]on Mar 27, 2015
@aanand I think it belongs in both compose and docker-py, actually. It would certainly be possible to just change docker-py so that it automatically detects when the context being passed is already compressed (instead of relying on the combination-of-parameters logic that it uses today). My grip with this approach would be that it involves moving validation and conditional behavior too far down to the library level.
As a principle there's always value in keeping validation and detection of user intent as close as possible to surface code and not deep in libraries. Libraries should provide a set of tailored, focused operations that each do one thing with as little conditional logic as possible, specially with regard to the input parameters (they certainly need a lot of conditional logic to deal with failures on the underlying resource level and such). That arrangement makes it easier both to fail early and to optimize for efficiency where relevant.
So, if docker-py is changed the right way it would not solve this particular problem, because the right way would be to have docker-py offer a more fine grained interface for its build() operation, one call for tarballs, one for Dockerfile, one for directory path - and then have the calling code decide upfront which one to use, instead of having just one interface that knows how to deal with whatever the calling code decides to pass on.
So, either both compose and docker-py could be changed in tandem (the 'right way', probably unrealistic, I don't know) or compose could be changed first, to call docker-py's build method with the right combination of arguments when the build: parameter points to a tarball. That would solve the more visible half of the problem, i.e, the user facing half.
As it's currently implemented, compose implicitly assumes the value of build: to be a directory path, since the value is passed "as is" to docker-py with the combination of parameters that docker-py happens to use to infer a directory path value.
In services.py:
defbuild(self, no_cache=False):
log.info('Building %s...'%self.name)
""" - Here it should be determined what kind of resource self.options['build'] points to. - If it's a tarball, the call to self.client.build should look something like: build_output = self.client.build( fileobj=open(self.options['build'], 'rb'), custom_context=True, encoding='gzip', tag=self.full_name, stream=True, rm=True, nocache=no_cache, ) - If it's a directory, use the already existing call:"""build_output=self.client.build(
self.options['build'],
tag=self.full_name,
stream=True,
rm=True,
nocache=no_cache,
)
Then, when docker-py is improved to move away from the combination-of-parameters approach, the implementation in compose could become less crufty than what's necessary now without losing the ability to fail early, etc.
OK, I'd mistakenly believed the docker client would smartly determine whether the argument passed to it was a directory or a tarball, but it looks like they go different routes (stdin for tarball or single Dockerfile vs. argument for directory).
In that case, I think it's fine for that logic to live in Compose: we should check whether the path passed to build is a directory or a regular file, and in the latter case pass fileobj=open(self.options['build'], 'rb'), as you say.
@aanand If build command could accept tarball as a regular arg, then it would benefit other compose tools too. When I look in the source code of docker build and it already has the necessary checking functions etc.
We just have to peek the input file, and check if if archive.IsArchive(magic) then set the variable context to be context = ioutil.NopCloser(but). IMHO It does not seem to be too difficult.
Oh right… I also seem to have forgotten that compose actually doesn't use the CLI client, but directly calls the API instead. (where in the API, tarball is always sent across the same way).
Would it be ok to add backports.lzma as a requirement? Python2's tarfile module doesn't support lzma compression natively. Since the docker daemon accepts xz I think compose should support it too.
In two different projects, we have a custom build scripts to achieve a couple of things...
In one project we use tar to build a tarball (concatenating a few directories, and removing a few others) and pass this to docker build. In another project, we remove a directory from the context in which docker build is run so that we don't have to remove this directory as part of the build process (it's a node_modules directory), then run docker build and then move the directory back for development purposes. Having written that, we could use tar in both instances which would be better.
I was going to open a new ticket to discuss support for pre and post build scripts, so that you could alter the build directory as required before and after a build. But I thought I'd chime in here instead... having the ability to point to a tarball would suffice I suppose. The ability to run a pre-build script would be fantastic to generate the tarball, but you could get around this by a custom script that generates a tarball and then executes docker-compose build.
@smebberson I have a working implementation of this feature in the PR referenced above.
My scenario is more or less the same as yours. We have a staging directory with a bunch of maven generated tar archives with Dockerfiles inside them. When someone finishes a project build, the archive is shipped to this staging area. New uploads are detected by an inotify-wait script of some contraption, which then calls docker-compose to rebuild and restart the corresponding image.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Activity
[-]Add support for pointing to tarball contexts on the 'build' parameter.[/-][+]Add support for pointing to tarball contexts in the 'build' parameter.[/+]dnephin commentedon Mar 27, 2015
It seems reasonable to me that
build:would support tarballs as wellaanand commentedon Mar 27, 2015
Seems like this change belongs in docker-py.
moysesb commentedon Mar 28, 2015
@aanand I think it belongs in both compose and docker-py, actually. It would certainly be possible to just change docker-py so that it automatically detects when the context being passed is already compressed (instead of relying on the combination-of-parameters logic that it uses today). My grip with this approach would be that it involves moving validation and conditional behavior too far down to the library level.
As a principle there's always value in keeping validation and detection of user intent as close as possible to surface code and not deep in libraries. Libraries should provide a set of tailored, focused operations that each do one thing with as little conditional logic as possible, specially with regard to the input parameters (they certainly need a lot of conditional logic to deal with failures on the underlying resource level and such). That arrangement makes it easier both to fail early and to optimize for efficiency where relevant.
So, if docker-py is changed the right way it would not solve this particular problem, because the right way would be to have docker-py offer a more fine grained interface for its
build()operation, one call for tarballs, one for Dockerfile, one for directory path - and then have the calling code decide upfront which one to use, instead of having just one interface that knows how to deal with whatever the calling code decides to pass on.So, either both compose and docker-py could be changed in tandem (the 'right way', probably unrealistic, I don't know) or compose could be changed first, to call docker-py's build method with the right combination of arguments when the
build:parameter points to a tarball. That would solve the more visible half of the problem, i.e, the user facing half.As it's currently implemented, compose implicitly assumes the value of
build:to be a directory path, since the value is passed "as is" to docker-py with the combination of parameters that docker-py happens to use to infer a directory path value.In services.py:
Then, when docker-py is improved to move away from the combination-of-parameters approach, the implementation in
composecould become less crufty than what's necessary now without losing the ability to fail early, etc.aanand commentedon Mar 30, 2015
OK, I'd mistakenly believed the docker client would smartly determine whether the argument passed to it was a directory or a tarball, but it looks like they go different routes (stdin for tarball or single Dockerfile vs. argument for directory).
In that case, I think it's fine for that logic to live in Compose: we should check whether the path passed to
buildis a directory or a regular file, and in the latter case passfileobj=open(self.options['build'], 'rb'), as you say.dreamcat4 commentedon Mar 30, 2015
@aanand If build command could accept tarball as a regular arg, then it would benefit other compose tools too. When I look in the source code of
docker buildand it already has the necessary checking functions etc.We just have to peek the input file, and check if
if archive.IsArchive(magic)then set the variablecontextto becontext = ioutil.NopCloser(but). IMHO It does not seem to be too difficult.Existing tarball handling (the stdin):
https://github.com/docker/docker/blob/master/api/client/build.go#L74-90
Where to add new tarball handling (as regular argument):
https://github.com/docker/docker/blob/master/api/client/build.go#L94-95
Where the
contextvariable gets sent to docker daemon:https://github.com/docker/docker/blob/master/api/client/build.go#L206-214
dreamcat4 commentedon Mar 30, 2015
Oh right… I also seem to have forgotten that compose actually doesn't use the CLI client, but directly calls the API instead. (where in the API, tarball is always sent across the same way).
moysesb commentedon Mar 31, 2015
Would it be ok to add backports.lzma as a requirement? Python2's tarfile module doesn't support lzma compression natively. Since the docker daemon accepts xz I think compose should support it too.
smebberson commentedon Apr 2, 2015
In two different projects, we have a custom build scripts to achieve a couple of things...
In one project we use
tarto build a tarball (concatenating a few directories, and removing a few others) and pass this todocker build. In another project, we remove a directory from the context in whichdocker buildis run so that we don't have to remove this directory as part of the build process (it's anode_modulesdirectory), then rundocker buildand then move the directory back for development purposes. Having written that, we could usetarin both instances which would be better.I was going to open a new ticket to discuss support for pre and post build scripts, so that you could alter the build directory as required before and after a build. But I thought I'd chime in here instead... having the ability to point to a tarball would suffice I suppose. The ability to run a pre-build script would be fantastic to generate the tarball, but you could get around this by a custom script that generates a tarball and then executes
docker-compose build.moysesb commentedon Apr 2, 2015
@smebberson I have a working implementation of this feature in the PR referenced above.
My scenario is more or less the same as yours. We have a staging directory with a bunch of maven generated tar archives with Dockerfiles inside them. When someone finishes a project build, the archive is shipped to this staging area. New uploads are detected by an
inotify-waitscript of some contraption, which then callsdocker-composeto rebuild and restart the corresponding image.stale commentedon Oct 10, 2019
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
milesrichardson commentedon Oct 15, 2019
Please don't close
Would love to be able to do something similar to this:
tar -czf - ./srcDirOne ./srcDirTwo ./srcDirThree \ | docker build -t acme/widget -f widget/Dockerfile -4 remaining items
stale commentedon Sep 8, 2020
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
meeseekz commentedon Sep 14, 2020
can #7283 be merged? this would be very useful.
stale commentedon Sep 14, 2020
This issue has been automatically marked as not stale anymore due to the recent activity.
stale commentedon Mar 19, 2021
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
milesrichardson commentedon Mar 20, 2021
Looks like a small and easy PR, can it be merged?
stale commentedon Mar 20, 2021
This issue has been automatically marked as not stale anymore due to the recent activity.
bevin0708 commentedon Apr 15, 2021
Looking for the same feature.
stale commentedon Oct 30, 2021
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
stale commentedon Apr 17, 2022
This issue has been automatically closed because it had not recent activity during the stale period.