-
Notifications
You must be signed in to change notification settings - Fork 2
/
my-little-book-of-docker-build
104 lines (72 loc) · 2.74 KB
/
my-little-book-of-docker-build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
% My Little Book of Dockerfile build %%%
[coenraadl@rdr-ap-cld1m Go_tut]$ cat Dockerfile.build
FROM golang:alpine
RUN apk update && apk add git && apk add ca-certificates
RUN adduser -D -g ' ' appuser
COPY ./src $GOPATH/src/
WORKDIR $GOPATH/src/
RUN go get -d -v
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o /go/bin/go-example
1.Build image
docker build -f Dockerfile.build -t "go-example-build-image" .
2. Create container
docker create --name go-example-build go-example-build-image
3. Copy from container
docker cp go-example-build:/go/bin/go-example ./tmp/
[coenraadl@rdr-ap-cld1m Go_tut]$ mkdir tmp
[coenraadl@rdr-ap-cld1m Go_tut]$ docker cp go-example-build:/go/bin/go-example ./tmp/
[coenraadl@rdr-ap-cld1m Go_tut]$ ./tmp/go-example
Ho, Ho Ho !
2018/12/28 13:30:15 Ho, Ho Ho !
2018/12/28 13:30:15 IP: 172.16.236.102
2018/12/28 13:30:15 IP: 192.168.122.1
2018/12/28 13:30:15 IP: 172.17.0.1
2018/12/28 13:30:15 IP: 10.42.0.0
2018/12/28 13:31:01 new request /hello
2018/12/28 13:31:01 Bye!!
2018/12/28 13:31:16 new request /getIP
2018/12/28 13:31:16 IPs:
2018/12/28 13:31:16 172.16.236.102
192.168.122.1
172.17.0.1
10.42.0.0
2018/12/28 13:31:16 404
2018/12/28 13:31:16 http: multiple response.WriteHeader calls
^C
[coenraadl@rdr-ap-cld1m Go_tut]$ docker cp go-example-build:/etc/ssl/certs/ca-certificates.crt ./tmp/
[coenraadl@rdr-ap-cld1m Go_tut]$ docker cp go-example-build:/etc/passwd ./tmp/
Remove container
docker rm go-example-build
Create Dockerfile
FROM scratch
COPY ./tmp/ca-certificates.crt /etc/ssl/certs/
COPY ./tmp/passwd /etc/
COPY ./tmp/go-example /go/bin/go-example
USER appuser
ENTRYPOINT ["/go/bin/go-example "]
~
Build Docker Dockerfile
Build an image from a Dockerfile
[coenraadl@rdr-ap-cld1m Go_tut]$ docker build -t "go-example-image" .
Sending build context to Docker daemon 8.771 MB
Step 1/6 : FROM scratch
--->
Step 2/6 : COPY ./tmp/ca-certificates.crt /etc/ssl/certs/
---> 956f38d1b972
Removing intermediate container 92d1b8034b76
Step 3/6 : COPY ./tmp/passwd /etc/
---> 71ca34a339b9
Removing intermediate container 5e166ae8641e
Step 4/6 : COPY ./tmp/go-example /go/bin/go-example
---> 05b048c8f45a
Removing intermediate container 2eecac44c404
Step 5/6 : USER appuser
---> Running in 33cd36435258
---> 43d98adc50c9
Removing intermediate container 33cd36435258
Step 6/6 : ENTRYPOINT /go/bin/go-example
---> Running in 9a67a94db7bd
---> 35574a93c652
Removing intermediate container 9a67a94db7bd
Successfully built 35574a93c652
---------------------------------------------------------------------------------