Fix Dockerfile COPY/ADD examples without a trailing slash

When copying files to a destination directory, the classic builder requires
the destination to have a trailing slash (to indicate the target is a _directory_.
not a filename). BuildKit is a bit more flexible in this, and will assume the
target is a directory, but users following the example with buildkit disabled
might see an error message, e.g.:

    Sending build context to Docker daemon  3.072kB
    Step 1/2 : FROM busybox
     ---> 69593048aa3a
    Step 2/2 : COPY *.go .
    When using COPY with more than one source file, the destination must be a directory and end with a /

It doesn't hurt to be explicit, so this patch updates some examples to prevent
this.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2021-07-21 12:03:31 +02:00
parent c5b1a6ad00
commit 5011f61213
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
3 changed files with 21 additions and 21 deletions

View File

@ -170,13 +170,13 @@ context, refer to [exclude with .dockerignore](#exclude-with-dockerignore).
> >
> docker build -t myimage:latest -<<EOF > docker build -t myimage:latest -<<EOF
> FROM busybox > FROM busybox
> COPY somefile.txt . > COPY somefile.txt ./
> RUN cat /somefile.txt > RUN cat /somefile.txt
> EOF > EOF
> >
> # observe that the build fails > # observe that the build fails
> ... > ...
> Step 2/3 : COPY somefile.txt . > Step 2/3 : COPY somefile.txt ./
> COPY failed: stat /var/lib/docker/tmp/docker-builder249218248/somefile.txt: no such file or directory > COPY failed: stat /var/lib/docker/tmp/docker-builder249218248/somefile.txt: no such file or directory
> ``` > ```
@ -206,7 +206,7 @@ touch somefile.txt
# build an image using the current directory as context, and a Dockerfile passed through stdin # build an image using the current directory as context, and a Dockerfile passed through stdin
docker build -t myimage:latest -f- . <<EOF docker build -t myimage:latest -f- . <<EOF
FROM busybox FROM busybox
COPY somefile.txt . COPY somefile.txt ./
RUN cat /somefile.txt RUN cat /somefile.txt
EOF EOF
``` ```
@ -232,7 +232,7 @@ the `hello.c` file from the ["hello-world" Git repository on GitHub](https://git
```bash ```bash
docker build -t myimage:latest -f- https://github.com/docker-library/hello-world.git <<EOF docker build -t myimage:latest -f- https://github.com/docker-library/hello-world.git <<EOF
FROM busybox FROM busybox
COPY hello.c . COPY hello.c ./
EOF EOF
``` ```

View File

@ -40,7 +40,7 @@ builder pattern above:
# syntax=docker/dockerfile:1 # syntax=docker/dockerfile:1
FROM golang:1.16 FROM golang:1.16
WORKDIR /go/src/github.com/alexellis/href-counter/ WORKDIR /go/src/github.com/alexellis/href-counter/
COPY app.go . COPY app.go ./
RUN go get -d -v golang.org/x/net/html \ RUN go get -d -v golang.org/x/net/html \
&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . && CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
``` ```
@ -57,7 +57,7 @@ and forget to continue the line using the `\` character, for example.
FROM alpine:latest FROM alpine:latest
RUN apk --no-cache add ca-certificates RUN apk --no-cache add ca-certificates
WORKDIR /root/ WORKDIR /root/
COPY app . COPY app ./
CMD ["./app"] CMD ["./app"]
``` ```
@ -103,13 +103,13 @@ multi-stage builds.
FROM golang:1.16 FROM golang:1.16
WORKDIR /go/src/github.com/alexellis/href-counter/ WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html RUN go get -d -v golang.org/x/net/html
COPY app.go . COPY app.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
FROM alpine:latest FROM alpine:latest
RUN apk --no-cache add ca-certificates RUN apk --no-cache add ca-certificates
WORKDIR /root/ WORKDIR /root/
COPY --from=0 /go/src/github.com/alexellis/href-counter/app . COPY --from=0 /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"] CMD ["./app"]
``` ```
@ -143,13 +143,13 @@ Dockerfile are re-ordered later, the `COPY` doesn't break.
FROM golang:1.16 AS builder FROM golang:1.16 AS builder
WORKDIR /go/src/github.com/alexellis/href-counter/ WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html RUN go get -d -v golang.org/x/net/html
COPY app.go . COPY app.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
FROM alpine:latest FROM alpine:latest
RUN apk --no-cache add ca-certificates RUN apk --no-cache add ca-certificates
WORKDIR /root/ WORKDIR /root/
COPY --from=builder /go/src/github.com/alexellis/href-counter/app . COPY --from=builder /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"] CMD ["./app"]
``` ```

View File

@ -152,8 +152,8 @@ In its simplest form, the `COPY` command takes two parameters. The first paramet
Well copy the `go.mod` and `go.sum` file into our working directory `/app` which, owing to our use of `WORKDIR`, is the current directory (`.`) inside the image. Well copy the `go.mod` and `go.sum` file into our working directory `/app` which, owing to our use of `WORKDIR`, is the current directory (`.`) inside the image.
```dockerfile ```dockerfile
COPY go.mod . COPY go.mod ./
COPY go.sum . COPY go.sum ./
``` ```
Now that we have the module files inside the Docker image that we are building, we can use the `RUN` command to execute the command `go mod download` there as well. This works exactly the same as if we were running `go` locally on our machine, but this time these Go modules will be installed into the a directory inside our image. Now that we have the module files inside the Docker image that we are building, we can use the `RUN` command to execute the command `go mod download` there as well. This works exactly the same as if we were running `go` locally on our machine, but this time these Go modules will be installed into the a directory inside our image.
@ -167,7 +167,7 @@ At this point, we have an image that is based on Go environment version 1.16 (or
The next thing we need to do is to copy our source code into the image. Well use the `COPY` command just like we did with our module files before. The next thing we need to do is to copy our source code into the image. Well use the `COPY` command just like we did with our module files before.
```dockerfile ```dockerfile
COPY *.go . COPY *.go ./
``` ```
This `COPY` command uses a wildcard to copy all files with `.go` extension located in the current directory on the host (the directory where the `Dockerfile` is located) into the current directory inside the image. This `COPY` command uses a wildcard to copy all files with `.go` extension located in the current directory on the host (the directory where the `Dockerfile` is located) into the current directory inside the image.
@ -195,11 +195,11 @@ FROM golang:1.16-alpine
WORKDIR /app WORKDIR /app
COPY go.mod . COPY go.mod ./
COPY go.sum . COPY go.sum ./
RUN go mod download RUN go mod download
COPY *.go . COPY *.go ./
RUN go build -o /docker-gs-ping RUN go build -o /docker-gs-ping
@ -218,8 +218,8 @@ FROM golang:1.16-alpine
WORKDIR /app WORKDIR /app
# Download necessary Go modules # Download necessary Go modules
COPY go.mod . COPY go.mod ./
COPY go.sum . COPY go.sum ./
RUN go mod download RUN go mod download
# ... the rest of the Dockerfile is ... # ... the rest of the Dockerfile is ...
@ -351,11 +351,11 @@ FROM golang:1.16-buster AS build
WORKDIR /app WORKDIR /app
COPY go.mod . COPY go.mod ./
COPY go.sum . COPY go.sum ./
RUN go mod download RUN go mod download
COPY *.go . COPY *.go ./
RUN go build -o /docker-gs-ping RUN go build -o /docker-gs-ping