在腾讯云服务器中使用腾讯的容器服务,拉取腾讯云仓库中的镜像时,会出现如下的报错:

docker pull ccr.ccs.tencentyun.com/qcloud/nginx:1.9
Trying to pull repository ccr.ccs.tencentyun.com/qcloud/nginx ... 
1.9: Pulling from ccr.ccs.tencentyun.com/qcloud/nginx
51f5c6a04d83: Pulling fs layer 
a3ed95caeb02: Pulling fs layer 
640c8f3d0eb2: Downloading 
a4335300aa89: Waiting 
unknown blob

出现这种问题一般是dns解析问题或者https认证等问题。

dns的问题手动添加hosts即可,https问题添加如下配置到/etc/default/docker 即可:

DOCKER_OPTS="--insecure-registry ccr.ccs.tencentyun.com"

重启docker服务

systemctl restart  docker.service

重启完之后,即可正常拉取。

docker pull ccr.ccs.tencentyun.com/qcloud/nginx:1.9
Trying to pull repository ccr.ccs.tencentyun.com/qcloud/nginx ... 
1.9: Pulling from ccr.ccs.tencentyun.com/qcloud/nginx
51f5c6a04d83: Pull complete 
a3ed95caeb02: Pull complete 
640c8f3d0eb2: Pull complete 
a4335300aa89: Pull complete 
Digest: sha256:311e9840c68d889e74eefa18227d0a6f995bc7a74f5453fdcd49fe3c334feb24
Status: Downloaded newer image for ccr.ccs.tencentyun.com/qcloud/nginx:1.9

使用Docker编译Kotlin时,遇到了如下报错:

FAILURE: Build failed with an exception.

* What went wrong:
Could not create service of type ScriptPluginFactory using BuildScopeServices.createScriptPluginFactory().
> Could not create service of type CrossBuildFileHashCache using BuildSessionScopeServices.createCrossBuildFileHashCache().

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s

我的Dockerfile如下:

FROM gradle:5.2.1-jdk8 as builder
COPY ./ /data/dropbox/
WORKDIR /data/dropbox
RUN gradle build --no-daemon

经过排查,发现是权限问题,无法启动gradle进行编译,因此加入如下一句:

USER root

即可成功通过。完整Dockerfile如下:

FROM gradle:5.2.1-jdk8 as builder
COPY ./ /data/dropbox/
USER root
WORKDIR /data/dropbox
RUN gradle build --no-daemon

自 17.05 版本开始,Docker 支持多步骤镜像创建( Multi-stage build)特性,可以精简最终生成的镜像大小 。

对于需要编译的应用(如 CGoJava语言等)来说,通常情况下至少需要准备两个 环境的 Docker镜像:

  • 编译环境镜像 : 包括完整的编译引擎、依赖库等,往往比较庞大。作用是编译应用为二进制文件;
  • 运行环境镜像: 利用编译好的二进制文件,运行应用,由于不需要编译环境,体积比较小。

使用多步骤创建,可以在保证最终生成的运行环境镜像保持精筒的情况下,使用单一的Dockerfile,降低维护复杂度 。

Go语言应用为例。创建干净目录,进入到目录中,创建main.go文件,内容为:

// main.go will output "Hello, Docker"
package main

import ("fmt")

func main() {
    fmt.println("Hello, Docker")
}

创建 Dockerfile,使用 golang:1.9镜像编译应用二进制文件为 app,使用精简的镜像 alpine:latest 作为运行环境 。Dockerfile 完整内容为:

FROM golang:1.9 as builder # define stage name as builder
RUN mkdir -p /go/src/test
WORKDIR /go/src/test
COPY main.go
RUN CGO_ENABLED=O GOOS=linux go build -o app

FROM alpine:latest
RUN apk –no-cache add ca-certificates
WORKDIR /root/
COPY –from=builder /go/src/test/app . # copy file from the builder stage
CMD ["./app"]

执行如下命令创建镜像,并运行应用:

$ docker build -t yeasy/test multistage:latest .
Sending build context to Docker daemon 3.072kB
Step 1/10 : FROM golang:1.9
Successfully built 5fd0cb93dda0
Successfully tagged yeasy/test-multistage:latest 
$ docker run --rm yeasy/test-m ultistage:latest
Hello, Docker

查看生成的最终镜像,大小只有 6.55 MB:

$docker images | grep test-multistage
yeasy/test-multistage latest 5fd0cb93dda0 1 minutes ago 6.55MB

文章来源:

《Docker技术入门与实践》第三版 杨保华、戴王剑、曹亚仑 编著