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;
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.