mirror of
https://github.com/clearlinux/docker.git
synced 2026-09-07 14:32:09 +00:00
add support for COPY to docker build
This adds a COPY command to docker build which works like ADD, but is only for local files and it doesn't extract files. Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
FROM busybox
|
||||
RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
|
||||
RUN echo 'dockerio:x:1001:' >> /etc/group
|
||||
RUN mkdir /exists
|
||||
RUN touch /exists/exists_file
|
||||
RUN chown -R dockerio.dockerio /exists
|
||||
COPY test_dir/ /exists/
|
||||
RUN [ $(ls -l / | grep exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
|
||||
RUN [ $(ls -l /exists/exists_file | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
|
||||
RUN [ $(ls -l /exists/test_file | awk '{print $3":"$4}') = 'root:root' ]
|
||||
@@ -0,0 +1,8 @@
|
||||
FROM busybox
|
||||
RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
|
||||
RUN echo 'dockerio:x:1001:' >> /etc/group
|
||||
RUN touch /exists
|
||||
RUN chown dockerio.dockerio exists
|
||||
COPY test_dir /
|
||||
RUN [ $(ls -l /test_file | awk '{print $3":"$4}') = 'root:root' ]
|
||||
RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
|
||||
@@ -0,0 +1,2 @@
|
||||
FROM busybox
|
||||
COPY https://index.docker.io/robots.txt /
|
||||
@@ -0,0 +1,2 @@
|
||||
FROM scratch
|
||||
COPY . /
|
||||
@@ -0,0 +1,10 @@
|
||||
FROM busybox
|
||||
RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
|
||||
RUN echo 'dockerio:x:1001:' >> /etc/group
|
||||
RUN mkdir /exists
|
||||
RUN touch /exists/exists_file
|
||||
RUN chown -R dockerio.dockerio /exists
|
||||
COPY test_file /exists/
|
||||
RUN [ $(ls -l / | grep exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
|
||||
RUN [ $(ls -l /exists/test_file | awk '{print $3":"$4}') = 'root:root' ]
|
||||
RUN [ $(ls -l /exists/exists_file | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
|
||||
@@ -0,0 +1,9 @@
|
||||
FROM busybox
|
||||
RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
|
||||
RUN echo 'dockerio:x:1001:' >> /etc/group
|
||||
RUN touch /exists
|
||||
RUN chown dockerio.dockerio /exists
|
||||
COPY test_file /test_dir/
|
||||
RUN [ $(ls -l / | grep test_dir | awk '{print $3":"$4}') = 'root:root' ]
|
||||
RUN [ $(ls -l /test_dir/test_file | awk '{print $3":"$4}') = 'root:root' ]
|
||||
RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
|
||||
@@ -0,0 +1,9 @@
|
||||
FROM busybox
|
||||
RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
|
||||
RUN echo 'dockerio:x:1001:' >> /etc/group
|
||||
RUN touch /exists
|
||||
RUN chown dockerio.dockerio /exists
|
||||
COPY test_file /
|
||||
RUN [ $(ls -l /test_file | awk '{print $3":"$4}') = 'root:root' ]
|
||||
RUN [ $(ls -l /test_file | awk '{print $1}') = '-rw-r--r--' ]
|
||||
RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
|
||||
@@ -0,0 +1,2 @@
|
||||
FROM busybox
|
||||
COPY test_file .
|
||||
@@ -0,0 +1,11 @@
|
||||
FROM busybox
|
||||
RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
|
||||
RUN echo 'dockerio:x:1001:' >> /etc/group
|
||||
RUN touch /exists
|
||||
RUN chown dockerio.dockerio exists
|
||||
COPY test_dir /test_dir
|
||||
RUN [ $(ls -l / | grep test_dir | awk '{print $3":"$4}') = 'root:root' ]
|
||||
RUN [ $(ls -l / | grep test_dir | awk '{print $1}') = 'drwxr-xr-x' ]
|
||||
RUN [ $(ls -l /test_dir/test_file | awk '{print $3":"$4}') = 'root:root' ]
|
||||
RUN [ $(ls -l /test_dir/test_file | awk '{print $1}') = '-rw-r--r--' ]
|
||||
RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
|
||||
@@ -237,6 +237,181 @@ func TestAddEtcToRoot(t *testing.T) {
|
||||
logDone("build - add etc directory to root")
|
||||
}
|
||||
|
||||
func TestCopySingleFileToRoot(t *testing.T) {
|
||||
buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy", "SingleFileToRoot")
|
||||
f, err := os.OpenFile(filepath.Join(buildDirectory, "test_file"), os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Close()
|
||||
buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", ".")
|
||||
buildCmd.Dir = buildDirectory
|
||||
out, exitCode, err := runCommandWithOutput(buildCmd)
|
||||
errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
|
||||
|
||||
if err != nil || exitCode != 0 {
|
||||
t.Fatal("failed to build the image")
|
||||
}
|
||||
|
||||
deleteImages("testcopyimg")
|
||||
|
||||
logDone("build - copy single file to root")
|
||||
}
|
||||
|
||||
// Issue #3960: "ADD src ." hangs - adapted for COPY
|
||||
func TestCopySingleFileToWorkdir(t *testing.T) {
|
||||
buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy", "SingleFileToWorkdir")
|
||||
f, err := os.OpenFile(filepath.Join(buildDirectory, "test_file"), os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Close()
|
||||
buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", ".")
|
||||
buildCmd.Dir = buildDirectory
|
||||
done := make(chan error)
|
||||
go func() {
|
||||
out, exitCode, err := runCommandWithOutput(buildCmd)
|
||||
if err != nil || exitCode != 0 {
|
||||
done <- fmt.Errorf("build failed to complete: %s %v", out, err)
|
||||
return
|
||||
}
|
||||
done <- nil
|
||||
}()
|
||||
select {
|
||||
case <-time.After(5 * time.Second):
|
||||
if err := buildCmd.Process.Kill(); err != nil {
|
||||
fmt.Printf("could not kill build (pid=%d): %v\n", buildCmd.Process.Pid, err)
|
||||
}
|
||||
t.Fatal("build timed out")
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
deleteImages("testcopyimg")
|
||||
|
||||
logDone("build - copy single file to workdir")
|
||||
}
|
||||
|
||||
func TestCopySingleFileToExistDir(t *testing.T) {
|
||||
buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
|
||||
buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", "SingleFileToExistDir")
|
||||
buildCmd.Dir = buildDirectory
|
||||
out, exitCode, err := runCommandWithOutput(buildCmd)
|
||||
errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
|
||||
|
||||
if err != nil || exitCode != 0 {
|
||||
t.Fatal("failed to build the image")
|
||||
}
|
||||
|
||||
deleteImages("testcopyimg")
|
||||
|
||||
logDone("build - add single file to existing dir")
|
||||
}
|
||||
|
||||
func TestCopySingleFileToNonExistDir(t *testing.T) {
|
||||
buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
|
||||
buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", "SingleFileToNonExistDir")
|
||||
buildCmd.Dir = buildDirectory
|
||||
out, exitCode, err := runCommandWithOutput(buildCmd)
|
||||
errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
|
||||
|
||||
if err != nil || exitCode != 0 {
|
||||
t.Fatal("failed to build the image")
|
||||
}
|
||||
|
||||
deleteImages("testcopyimg")
|
||||
|
||||
logDone("build - copy single file to non-existing dir")
|
||||
}
|
||||
|
||||
func TestCopyDirContentToRoot(t *testing.T) {
|
||||
buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
|
||||
buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", "DirContentToRoot")
|
||||
buildCmd.Dir = buildDirectory
|
||||
out, exitCode, err := runCommandWithOutput(buildCmd)
|
||||
errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
|
||||
|
||||
if err != nil || exitCode != 0 {
|
||||
t.Fatal("failed to build the image")
|
||||
}
|
||||
|
||||
deleteImages("testcopyimg")
|
||||
|
||||
logDone("build - copy directory contents to root")
|
||||
}
|
||||
|
||||
func TestCopyDirContentToExistDir(t *testing.T) {
|
||||
buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
|
||||
buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", "DirContentToExistDir")
|
||||
buildCmd.Dir = buildDirectory
|
||||
out, exitCode, err := runCommandWithOutput(buildCmd)
|
||||
errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
|
||||
|
||||
if err != nil || exitCode != 0 {
|
||||
t.Fatal("failed to build the image")
|
||||
}
|
||||
|
||||
deleteImages("testcopyimg")
|
||||
|
||||
logDone("build - copy directory contents to existing dir")
|
||||
}
|
||||
|
||||
func TestCopyWholeDirToRoot(t *testing.T) {
|
||||
buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy", "WholeDirToRoot")
|
||||
test_dir := filepath.Join(buildDirectory, "test_dir")
|
||||
if err := os.MkdirAll(test_dir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f, err := os.OpenFile(filepath.Join(test_dir, "test_file"), os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Close()
|
||||
buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", ".")
|
||||
buildCmd.Dir = buildDirectory
|
||||
out, exitCode, err := runCommandWithOutput(buildCmd)
|
||||
errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
|
||||
|
||||
if err != nil || exitCode != 0 {
|
||||
t.Fatal("failed to build the image")
|
||||
}
|
||||
|
||||
deleteImages("testcopyimg")
|
||||
|
||||
logDone("build - copy whole directory to root")
|
||||
}
|
||||
|
||||
func TestCopyEtcToRoot(t *testing.T) {
|
||||
buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
|
||||
buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", "EtcToRoot")
|
||||
buildCmd.Dir = buildDirectory
|
||||
out, exitCode, err := runCommandWithOutput(buildCmd)
|
||||
errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
|
||||
|
||||
if err != nil || exitCode != 0 {
|
||||
t.Fatal("failed to build the image")
|
||||
}
|
||||
|
||||
deleteImages("testcopyimg")
|
||||
logDone("build - copy etc directory to root")
|
||||
}
|
||||
|
||||
func TestCopyDisallowRemote(t *testing.T) {
|
||||
buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
|
||||
buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", "DisallowRemote")
|
||||
buildCmd.Dir = buildDirectory
|
||||
out, exitCode, err := runCommandWithOutput(buildCmd)
|
||||
|
||||
if err == nil || exitCode == 0 {
|
||||
t.Fatalf("building the image should've failed; output: %s", out)
|
||||
}
|
||||
|
||||
deleteImages("testcopyimg")
|
||||
logDone("build - copy - disallow copy from remote")
|
||||
}
|
||||
|
||||
// Issue #5270 - ensure we throw a better error than "unexpected EOF"
|
||||
// when we can't access files in the context.
|
||||
func TestBuildWithInaccessibleFilesInContext(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user