部署的docker image總是太大,怎么辦?


sudo docker images

REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE

docker.io/golang                  1.10                d0e7a411e3da        5 days ago          794 MB

docker.io/alpine                  latest              11cd0b38bc3c        2 weeks ago         4.41 MB

docker 官方golang鏡像動不動就幾百MB,所以我們只能下載一些優化image 比如apline, 相比官方的鏡像,小了一半,有些甚至只有幾MB大小。

但是會有bug如下:Get https://google.com: x509: failed to load system roots and no roots provided

 

解決方案就是要添加這個語句在編譯文件里面

RUN apk --no-cache add ca-certificates

如果你docker版本是17.05+ 支持多重編譯的新特性就可以使用下面的例子,如果不是,就要自己分步執行,操作了。

FROM golang:1.7.3  as  builder
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go  get  -d -v golang.org/x/net/html 
COPY app.go    .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
 
FROM alpine:latest 
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY -- from =builder /go/src/github.com/alexellis/href-counter/app .
CMD [ "./app"

 

最終編譯出來的大小

sudo docker images

REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE

app_in_apline                     latest              0cb99ea67ced        2 minutes ago       11.1 MB

 

但是有一個問題就是上述的開發,編譯環境不一致,不如使用同一個庫里面開發生產鏡像

FROM golang:alpine as build

RUN apk --no-cache add git

ADD . /go/src/github.com/Example/pls3
WORKDIR /go/src/github.com/Example/pls3/cmd/pls3
RUN go get && go install -v

FROM alpine:latest

WORKDIR /
COPY --from=build /go/bin/pls3 /pls3
RUN apk --no-cache add ca-certificates && update-ca-certificates

ARG COMMIT
ENV COMMIT ${COMMIT}

ENTRYPOINT ["/pls3"]

  

 

 

總結,如果編譯開發image比較大,生產運行的image比較小, 所以在開發鏡像開發編譯可執行文件,編譯的時候加上參數build -a,合入所有依賴到執行文件里面,生產鏡像布署,但是一個問題就是生產鏡像少東西,還得配置。如果為不配置,而選擇完備的鏡像,大小又是幾百MB,又太大。

 

ps:刪除多余的鏡像語句

for i in $(sudo docker images | gawk   '{if (NR>1){print $3}}' | head -n 4) ;do sudo docker rmi $i -f ; done;


配置container里面的APP可以訪問localhost
 

I doing a hack similar to above posts of get the local IP to map to a alias name (DNS) in the container. The major problem is to get dynamically with a simple script that works both in Linux and OSX the host IP address. I did this script that works in both environments (even in Linux distribution with "$LANG" != "en_*" configured):

ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1

So, using Docker Compose, the full configuration will be:

Startup script (docker-run.sh):

export DOCKERHOST=$(ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1)
docker-compose -f docker-compose.yml up

docker-compose.yml:

myapp:
  build: .
  ports:
    - "80:80"
  extra_hosts:
    - "dockerhost:$DOCKERHOST"

Then change http://localhost to http://dockerhost in your code.

For a more advance guide of how to customize the DOCKERHOST script, take a look at this post with a explanation of how it works.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM