Commit Graph

101 Commits

Author SHA1 Message Date
Joshua Arrevillaga eea4838d88 ADD/COPY --link support added
What type of PR is this?
/kind feature

What this PR does / why we need it:
It implements --link for COPY and ADD instructions and enables the creation of
cachable layers that can be reused independently across builds.

Follows buildkit `--link` specifications

How to verify it
bats tests/bud.bats

Which issue(s) this PR fixes:
Fixes #4325

Does this PR introduce a user-facing change?
Yes, gives extra functionality to Containerfiles

Signed-off-by: Joshua Arrevillaga <2004jarrevillaga@gmail.com>
2025-07-09 17:14:28 -04:00
Nalin Dahyabhai b9a65a9d86 add: add a new --timestamp flag
Add a --timestamp flag to the "add" and "copy" CLIs, along with a
corresponding field in AddAndCopyOptions.

When a timestamp is set, we'll force the timestamp on data copied in to
be the specified value while reading it, so that the content will have
the specified datestamp in the rootfs and when the image is committed.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-05-21 09:51:51 -04:00
Kir Kolyshkin e8dba98314 ci: add nolintlint, fix found issues
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-07 13:02:39 -07:00
Kir Kolyshkin 35a5b3aaef Fix QF1007 staticcheck warnings
These:

> add.go:457:2: QF1007: could merge conditional assignment into variable declaration (staticcheck)
> 	newDestDirFound := false
> 	^
> cmd/buildah/umount.go:33:2: QF1007: could merge conditional assignment into variable declaration (staticcheck)
> 	umountAll := false
> 	^

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-07 13:01:56 -07:00
flouthoc 0c377816a9
add: report error while creating dir for URL source.
Correctly report back error when attempting to create `Tmpdir` for a
given url source.

Also remove superfluous `isGitTag` from define/types.go since `git
fetch` correctly resolves by provided reference.

Closes: https://github.com/containers/podman/issues/25679

Signed-off-by: flouthoc <flouthoc.git@gmail.com>
2025-03-26 08:10:59 -07:00
Jan Rodák 7ca9f3464b
Add --parents option for COPY in Dockerfiles
It also includes an implementation of the --parents flag for the buildah copy command.

Fixes: https://issues.redhat.com/browse/RUN-2193
Fixes: https://github.com/containers/buildah/issues/5557

Signed-off-by: Jan Rodák <hony.com@seznam.cz>
2025-03-18 21:58:27 +01:00
Nalin Dahyabhai c30f5784da define.TempDirForURL(): always use an intermediate subdirectory
Ensure that the temporary directory that we create is never itself the
top-level directory of the content that we're downloading, in case it's
an archive which includes a "." with weird permissions.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-01-17 09:59:55 -05:00
Kir Kolyshkin e213d65be6 deps: switch to moby/sys/userns
runc/libcontainer/userns package moved to a new home; the old package
will be made obsoleted. Switch to the new package.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2024-10-22 17:34:51 -07:00
danishprakash 71fc845d0b
tests/bud.bats: add git source
Signed-off-by: Danish Prakash <contact@danishpraka.sh>
2024-09-09 12:45:15 +05:30
danishprakash 47297b3d5c
add: add support for git source
Signed-off-by: Danish Prakash <contact@danishpraka.sh>
2024-09-09 12:45:15 +05:30
Nalin Dahyabhai ca3b80eb0f CI: enable the whitespace linter
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-08-15 16:09:25 -04:00
Nalin Dahyabhai 8ae99121c1 CI: enable the gofumpt linter
Turn on the gofumpt linter.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-08-15 13:17:44 -04:00
Nalin Dahyabhai 3ea43566ac Add(): re-escape any globbed items that included escapes
When collecting items we matched using the passed-in globs during Add(),
be sure that globbing performed by Get() will match the items exactly,
by escaping special matching characters.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-08-08 16:46:47 -04:00
Nalin Dahyabhai fdf1c75cd3 linters: unused arguments shouldn't have names
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-08-07 10:10:35 -04:00
Nalin Dahyabhai 5f18ba259a AddAndCopyOptions: add CertPath, InsecureSkipTLSVerify, Retry fields
Add CertPath and InsecureSkipTLSVerify flags to AddAndCopyOptions, and
connect the CLI flag values passed to `buildah add` and `buildah build`
so that Builder.Add() gets those.

Add MaxRetries and RetryDelay fields to AddAndCopyOptions, and connect
them to the values passed on the command line to `buildah add` and
`buildah build`.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-07-25 17:39:08 -04:00
Nalin Dahyabhai a7224d1674 Update godoc for Builder.EnsureContainerPathAs
Also log a debug message when the method is called.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-06-18 09:33:31 -04:00
Doug Rabson 32b8ceeb6f Fix a build break on FreeBSD
The build breaks trying to build libcontainer/userns which no longer
builds on FreeBSD. Fortunately we only need this for
userns.RunningInUserNS so this change moves that call to a linux-only
file and adds a stub for FreeBSD.

[NO NEW TESTS NEEDED]

Signed-off-by: Doug Rabson <dfr@rabson.org>
2024-01-26 14:26:30 +00:00
Jean-Francois Roy 99cad6ee1a
Implement ADD checksum flag #5135
See https://docs.docker.com/build/dockerfile/release-notes/#160.

Fixes #5135

Signed-off-by: Jean-Francois Roy <jf@devklog.net>
2023-11-13 08:32:22 -08:00
Nalin Dahyabhai 9dd66e4cbf add/copy: make sure we handle relative path names correctly
Make sure that `add` and `copy` handle relative paths given as input
correctly, both with and without a context directory being specified.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2023-09-15 16:20:19 -04:00
Chris Evich 46eea31588
Replace io/ioutil calls with os calls
In golang 1.19, `io/ioutil` is fully deprecated preventing Buildah from
compiling.  Replace all calls with equivalent calls from the `os`
package.

Signed-off-by: Chris Evich <cevich@redhat.com>
2022-12-06 14:29:32 -05:00
Daniel J Walsh 8d5d763213
Fix stutters
Podman adds an Error: to every error message. So starting an error
message with "error" ends up being reported to the user as

Error: error ...

This patch removes the stutter.

Also ioutil.ReadFile errors report the Path, so wrapping the err message
with the path causes a stutter.

Signed-off-by: Daniel J Walsh dwalsh@redhat.com

[NO NEW TESTS NEEDED]

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2022-09-19 07:11:44 -04:00
Sascha Grunert ce384684c0
Switch to golang native error wrapping
We now use the golang error wrapping format specifier `%w` instead of
the deprecated github.com/pkg/errors package.

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
2022-07-07 11:41:47 +02:00
Aditya R 7a22c40edc
add: fail on bad http response instead of writing to container
Adding sources from URL using `ADD` instruction adds reponse to build
container even if it receives bad HTTP response, following behaviour is
not in parity with `docker` or `buildkit`.

Following commit ensures that `ADD` where source is external URL fails
on build step if we get bad HTTP response.

Example: Following containerfile should fail while building

```Dockerfile
FROM registry.fedoraproject.org/fedora:36
ADD https://mirror.init7.net/fedora/fedora/linux/releases/36/Server/x86_64/iso/Fedora-Server-netinst-x86_64-36-1.5.foo /
```

**Fixes: BZ#2102140**

Signed-off-by: Aditya R <arajan@redhat.com>
2022-07-01 16:28:31 +05:30
Giuseppe Scrivano 08613ccd6b
buildah: create WORKDIR with USER permissions
Closes: https://github.com/containers/buildah/issues/3620

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2022-03-30 16:23:56 +02:00
Nalin Dahyabhai f9b0fbeac8 add: ensure the context directory is an absolute path
When we're processing a context directory, make sure it's an absolute
path.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2022-03-01 11:07:56 -05:00
Daniel J Walsh a8b15f8d45
Report ignorefile location when no content added
Users have accidently had a .containerignore or .dockerignore
file in their context directly which blocked all content.
Currently we report that no globs matched, but do not
identify where the globs came from.

This change is an attempt to add this data to the error.
Example: https://github.com/containers/buildah/issues/3318

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-10-15 05:14:07 -04:00
Daniel J Walsh 69e5e49040
Chown with environment variables not set should fail
Fixes: https://github.com/containers/buildah/issues/3380

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-07-20 05:16:38 -04:00
dependabot[bot] dfc3875f35
build(deps): bump github.com/opencontainers/runc
Bumps [github.com/opencontainers/runc](https://github.com/opencontainers/runc) from 1.0.0-rc95 to 1.0.0.
- [Release notes](https://github.com/opencontainers/runc/releases)
- [Commits](https://github.com/opencontainers/runc/compare/v1.0.0-rc95...v1.0.0)

---
updated-dependencies:
- dependency-name: github.com/opencontainers/runc
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

[NO TESTS NEEDED]

Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-06-23 08:43:28 -04:00
Paul Holzinger fd7af1a5e7 Fix copy into workdir for a single file
When copying a single file to the workdir make sure to create the workdir
and not to overwrite it as file.

Fixes containers/podman#10671

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2021-06-14 17:49:43 +02:00
Daniel J Walsh 37e9d254cc
Fix copier when using globs
In Docker if you are copying more then one object, and
one of them is successful, then the command is successful. Currently in
buildah each glob has to be successful. This PR matches Buildah to
Docker.

Fixes: https://github.com/containers/podman/issues/9594

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-04-22 10:39:23 -04:00
Nalin Dahyabhai 0b4d973238 COPY --chown: expand the conformance test
Expand conformance test coverage for COPY --chown, and correct our
behavior when the argument is a single number: instead of assuming the
number is the UID and GID = 0, the GID should be the same as the UID.

This means that we can't quite use the same function for parsing the
arguments for COPY --chown and USER, as they evidently have different
defaults for the case where the argument is a single number.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-03-22 16:37:12 -04:00
Nalin Dahyabhai effb375b5a ADD/COPY: create the destination directory first, chroot to it
Always create the destination directory first when ADDing or COPYing
content into a container, then extract contents into it using the
destination directory as the chroot instead of the container's root
directory.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-03-02 16:29:43 -05:00
Jakub Guzik 88301c85d4 buildah add/copy --chmod cli implementation for files and urls
Signed-off-by: Jakub Guzik <jakubmguzik@gmail.com>
2021-02-24 08:37:25 +01:00
OpenShift Merge Robot d65ba62f56
Merge pull request #2977 from rhatdan/shrink
Eliminate the use of containers/building import in pkg subdirs
2021-02-16 09:03:30 -05:00
TomSweeneyRedHat 758eb20144 Don't fail copy to emptydir
When a COPY command was being targeted at an empty new directory,
the copy would fail.  It could be "worked around" by putting a dummy
file into the directory.

Addresses:  #2964
Signed-off-by: TomSweeneyRedHat <tsweeney@redhat.com>
2021-02-14 14:24:34 -05:00
Daniel J Walsh 4704e6cb3f
Eliminate the use of containers/building import in pkg subdirs
We want to shrink the size of the import when importing pkg from
buildah. This should help us shrink the size of the golang bindings
in podman.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-02-12 12:43:08 -05:00
Daniel J Walsh 811822eb54
Revert patch to allow COPY/ADD of empty dirs.
This Patch breaks conformance with Docker.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-02-12 10:24:45 -05:00
TomSweeneyRedHat 3679b9f7ec Don't fail copy to emptydir
When a COPY command was being targeted at an empty new directory,
the copy would fail.  It could be "worked around" by putting a dummy
file into the directory.

Addresses:  #2964
Signed-off-by: TomSweeneyRedHat <tsweeney@redhat.com>
2021-02-10 19:11:46 -05:00
Nalin Dahyabhai f0404c688a copier.PutOptions: add an "IgnoreDevices" flag
Prior to 1.16, when ADDing contents to a working container, if we were
being run by an unprivileged user using a user namespace, content that
was a device node would be ignored.

Add a flag in copier.PutOptions that tells copier.Put() to ignore
entries that are either a device, or a hard link to a device.

Make buildah.Add() set the IgnoreDevices flag in PutOptions when
libcontainer says we're running in a user namespace.

Together, these two changes should restore the earlier behavior.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-01-04 17:09:57 -05:00
Nalin Dahyabhai a1d4afd16e ADD: handle --chown on URLs
Incorporate the value of the --chown option when digesting content added
from a URL, so that build caching will take it into account.  Add tests
to ensure that layer caching takes into account the changed ownership of
items that we're adding or copying.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2020-11-05 09:26:15 -05:00
Daniel J Walsh 39f4cfb79d
Stop excessive wrapping
Golang built in functions like os.Create and others print the name of
the file system object when they fail.  Wrapping them a second time
with the file system object, makes the error message look like crap
when reported to the user.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2020-10-15 14:03:13 -04:00
Nalin Dahyabhai 9c6969a79e ADD and COPY: descend into excluded directories, sometimes
When a directly-named (or globbed) source directory for ADD or COPY is
marked for exclusion by .dockerignore, check if its name is a prefix for
any exceptions in the .dockerignore file, and if it is, check the
directory for things we need to include anyway.

This will miss exceptions where the pattern uses a wildcard for anything
but the final component.

When adding items, count items that are actually passed over the tar
pipe, rather than items scanned, so that we can correctly diagnose not
having found anything that we needed to copy under a directory that
would otherwise have been excluded.

In copierHandlerGet(), just don't discount any globbed directories that
are excluded.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2020-10-09 11:45:39 -04:00
Nalin Dahyabhai c1a1805ee8 add: preserve ownerships and permissions on ADDed archives
When extracting archives that are added using ADD, don't override
permissions and ownership information.  We regressed on this when we
switched to using the copier package to handle them.

Add a conformance test to prevent regressions on this.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2020-09-29 16:25:31 -04:00
Nalin Dahyabhai ff8cfe7c3f Add(): fix handling of relative paths with no ContextDir
Add() is supposed to handle relative paths when the ContextDir value
passed to it is not set, but it hasn't been doing that correctly since
it was overhauled.  Correct it to recognize relative paths at the
function start, when ContextDir is not set, by combining them with the
current directory to convert them to absolute paths.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2020-09-16 14:19:10 -04:00
Nalin Dahyabhai 02921ee3ab Fix ownership of content copied using COPY --from
COPY --from was incorrectly discarding ownership information on files
copied from other layers, which unlike content copied from the build
context, should not default to being owned by 0:0.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2020-08-22 13:22:48 -04:00
Nalin Dahyabhai 3835460c3b Use pipes for copying
Use the copier package to rework how we handle ADD and COPY.

When evaluating cache for content that's being copied/added in, switch
from (digest the data, check for a cache entry, then maybe copy the data
and create the new layer) to (copy the data and create the new layer,
digesting as we go, check for a cache entry, either commit or discard
the new layer).

Use the copier package for ADD, COPY, and for ensuring that a specified
directory exists in the working container's rootfs.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2020-08-21 12:34:00 -04:00
yangfeiyu20102011 8ef7f25151 Do not skip the directory when the ignore pattern matches
Signed-off-by: yangfeiyu <yangfeiyu20102011@163.com>
2020-04-07 10:55:07 +08:00
Daniel J Walsh fa21ea4938
Fix .dockerignore with globs and ! commands
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2020-03-23 14:31:06 -04:00
Nalin Dahyabhai 8c255bc1cc copy: don't digest files that we ignore
Don't start digesting the contents of any file that we end up skipping.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>

Closes: #2072
Approved by: giuseppe
2020-01-09 11:10:24 +00:00
Nalin Dahyabhai 9b35b5e3d2 Check for .dockerignore specifically
When generating the list of exclusions to process .dockerignore
contents, don't include .dockerignore if we don't have a .dockerignore
file in the context directory.  That way, if the file doesn't exist, and
the caller didn't pass in any patterns, we get no patterns instead of
just one ".dockerignore" pattern, and we can hit the faster copy path.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>

Closes: #2072
Approved by: giuseppe
2020-01-09 11:10:24 +00:00