docker_practice/image/multistage-builds/README.md

192 lines
4.3 KiB
Go
Raw Permalink Normal View History

#
2017-11-24 10:36:30 +00:00
##
2017-11-24 10:36:30 +00:00
Docker 17.05 Docker
### Dockerfile
2017-11-24 10:36:30 +00:00
`Dockerfile`
*
*
2018-12-31 06:50:01 +00:00
`app.go` `Hello World!`
2017-11-24 10:36:30 +00:00
```go
package main
2017-11-24 10:36:30 +00:00
import "fmt"
2017-11-24 10:36:30 +00:00
func main(){
2017-11-24 10:36:30 +00:00
fmt.Printf("Hello World!");
}
```
`Dockerfile.one`
```docker
2021-03-11 15:09:37 +00:00
FROM golang:alpine
2017-11-24 10:36:30 +00:00
RUN apk --no-cache add git ca-certificates
WORKDIR /go/src/github.com/go/helloworld/
COPY app.go .
RUN go get -d -v github.com/go-sql-driver/mysql \
&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . \
&& cp /go/src/github.com/go/helloworld/app /root
WORKDIR /root/
CMD ["./app"]
```
```bash
$ docker build -t go/helloworld:1 -f Dockerfile.one .
```
### Dockerfile
2017-11-24 10:36:30 +00:00
`Dockerfile` `Dockerfile`
2018-12-31 06:50:01 +00:00
`Dockerfile.build`
2017-11-24 10:36:30 +00:00
```docker
2021-03-11 15:09:37 +00:00
FROM golang:alpine
2017-11-24 10:36:30 +00:00
RUN apk --no-cache add git
WORKDIR /go/src/github.com/go/helloworld
COPY app.go .
RUN go get -d -v github.com/go-sql-driver/mysql \
&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
```
`Dockerfile.copy`
```docker
2017-11-24 10:36:30 +00:00
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY app .
CMD ["./app"]
```
`build.sh`
```bash
#!/bin/sh
echo Building go/helloworld:build
docker build -t go/helloworld:build . -f Dockerfile.build
docker create --name extract go/helloworld:build
docker cp extract:/go/src/github.com/go/helloworld/app ./app
docker rm -f extract
echo Building go/helloworld:2
docker build --no-cache -t go/helloworld:2 . -f Dockerfile.copy
rm ./app
```
```bash
$ chmod +x build.sh
$ ./build.sh
```
```bash
$ docker image ls
2017-11-24 10:36:30 +00:00
2017-12-03 02:27:05 +00:00
REPOSITORY TAG IMAGE ID CREATED SIZE
go/helloworld 2 f7cf3465432c 22 seconds ago 6.47MB
go/helloworld 1 f55d3e16affc 2 minutes ago 295MB
2017-11-24 10:36:30 +00:00
```
## 使
2017-11-24 10:36:30 +00:00
Docker v17.05 (`multistage builds`)使 `Dockerfile`
2018-12-31 06:50:01 +00:00
`Dockerfile`
2017-11-24 10:36:30 +00:00
```docker
2021-03-11 15:09:37 +00:00
FROM golang:alpine as builder
2017-11-24 10:36:30 +00:00
RUN apk --no-cache add git
WORKDIR /go/src/github.com/go/helloworld/
RUN go get -d -v github.com/go-sql-driver/mysql
COPY app.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
FROM alpine:latest as prod
2017-11-24 10:36:30 +00:00
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=0 /go/src/github.com/go/helloworld/app .
CMD ["./app"]
2017-11-24 10:36:30 +00:00
```
```bash
$ docker build -t go/helloworld:3 .
```
```bash
$ docker image ls
2017-11-24 10:36:30 +00:00
2017-12-03 02:27:05 +00:00
REPOSITORY TAG IMAGE ID CREATED SIZE
go/helloworld 3 d6911ed9c846 7 seconds ago 6.47MB
go/helloworld 2 f7cf3465432c 22 seconds ago 6.47MB
go/helloworld 1 f55d3e16affc 2 minutes ago 295MB
2017-11-24 10:36:30 +00:00
```
使
###
使 `as`
```docker
2021-03-11 15:09:37 +00:00
FROM golang:alpine as builder
```
2018-12-31 06:50:01 +00:00
`builder` `--target=builder`
```bash
$ docker build --target builder -t username/imagename:tag .
```
###
使 `COPY --from=0 /go/src/github.com/go/helloworld/app .`
```docker
$ COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf
```