如何使用GOLang官方镜像将一个不常用的包导入到Docker中?

huangapple go评论112阅读模式
英文:

How to import an unpopular package to Docker using the GOLang official image?

问题

我已经在imagick git存储库上发布了这个问题,但是它的用户群体非常小,所以我希望能从这里得到一些帮助。我已经尝试了几天,使用官方的goLang dockerfile将https://github.com/gographics/imagick导入到Docker中,但是一直没有成功。由于这个包不太受欢迎,运行apt-get命令无法生效。我尝试过将文件直接添加到容器中,但是没有成功。这是我构建的DockerFile和产生的错误:

===DOCKERFILE===

1) 使用基于debian构建的官方go docker镜像。

FROM golang:latest

2) 环境变量

ENV GOPATH $HOME/
ENV PATH $HOME//bin:$PATH

3) 获取源代码并将其添加到工作区。

ADD . //src/
ADD . //gopkg.in

尝试手动添加文件...没有帮助。

ADD . /opt/local/share/doc/ImageMagick-6

4) 安装revel和revel CLI。

#(注释掉的代码是之前的尝试)
#RUN pkg-config --cflags --libs MagickWand
#RUN go get gopkg.in/gographics/imagick.v2/imagick
RUN go get github.com/revel/revel
RUN go get github.com/revel/cmd/revel

5) 不起作用...找不到该包。

#RUN apt-get install libmagickwand-dev

6) 从主存储库获取godeps

RUN go get github.com/tools/godep

7) 恢复godep依赖项

WORKDIR //src/
RUN godep restore

8) 安装Imagick

#RUN go build -tags no_pkgconfig gopkg.in/gographics/imagick.v2/imagick

9) 使用revel CLI启动我们的应用程序。

ENTRYPOINT revel run dev 9000

10) 打开应用程序运行的端口。

EXPOSE 9000

===END DOCKERFILE===

这使我能够构建docker容器,但是当我尝试运行它时,在kinematic的日志中出现以下错误:

===DOCKER ERROR===

ERROR 2016/08/20 21:15:10 build.go:108: # pkg-config --cflags MagickWand MagickCore MagickWand MagickCore
pkg-config: exec: "pkg-config": 在$PATH中找不到可执行文件
2016-08-20T21:15:10.081426584Z
ERROR 2016/08/20 21:15:10 build.go:308: 解析构建错误失败:
#pkg-config --cflags MagickWand MagickCore MagickWand MagickCore
pkg-config: exec: "pkg-config": 在$PATH中找不到可执行文件
2016-08-20T21:15:10.082140143Z

===END DOCKER ERROR===

英文:

I've posted this question already as an issue on the imagick git repository, but it has a very small user-base, so I'm hoping to get some help from here. I've been trying for a few days now to import https://github.com/gographics/imagick to Docker using the official goLang dockerfile for a project I'm working on, but have been unsuccessful. Since this package isn't popular, running apt-get won't work. I've (hesitantly) tried to just add the files to the container, but that didn't work. Here's the DockerFile I've built and the error it produces:
===DOCKERFILE===

# 1) Use the official go docker image built on debian.
FROM golang:latest

# 2) ENV VARS
ENV GOPATH $HOME/<PROJECT>
ENV PATH $HOME/<PROJECT>/bin:$PATH

# 3) Grab the source code and add it to the workspace.
ADD . /<GO>/src/<PROJECT>
ADD . /<GO>/gopkg.in
# Trying to add the files manually... Doesn't help.
ADD . /opt/local/share/doc/ImageMagick-6


# 4) Install revel and the revel CLI.
#(The commented out code is from previous attempts)
#RUN pkg-config --cflags --libs MagickWand
#RUN go get gopkg.in/gographics/imagick.v2/imagick
RUN go get github.com/revel/revel
RUN go get github.com/revel/cmd/revel

# 5) Does not work... Can't find the package.
#RUN apt-get install libmagickwand-dev

# 6) Get godeps from main repo
RUN go get github.com/tools/godep

# 7) Restore godep dependencies
WORKDIR /<GO>/src/<PROJECT>
RUN godep restore

# 8) Install Imagick
#RUN go build -tags no_pkgconfig gopkg.in/gographics/imagick.v2/imagick

# 9) Use the revel CLI to start up our application.
ENTRYPOINT revel run <PROJECT> dev 9000

# 10) Open up the port where the app is running.
EXPOSE 9000

===END DOCKERFILE===

This allows me to build the docker container, but when I try to run it, I get the following error in the logs of kinematic:

===DOCKER ERROR===

ERROR 2016/08/20 21:15:10 build.go:108: # pkg-config --cflags MagickWand MagickCore MagickWand MagickCore
pkg-config: exec: "pkg-config": executable file not found in $PATH
2016-08-20T21:15:10.081426584Z 
ERROR 2016/08/20 21:15:10 build.go:308: Failed to parse build errors:
 #pkg-config --cflags MagickWand MagickCore MagickWand MagickCore
pkg-config: exec: "pkg-config": executable file not found in $PATH
2016-08-20T21:15:10.082140143Z 

===END DOCKER ERROR===

答案1

得分: 2

大多数基础镜像已删除软件包列表以减小镜像大小。因此,为了使用apt-get安装软件包,你首先需要更新软件包列表,然后再安装所需的软件包。然后,在安装完软件包后,为了避免污染镜像中的不必要文件,需要移除运行apt的所有副作用(可以作为一个单独的RUN命令)。

以下是可以解决问题的Dockerfile示例:

FROM golang:latest

RUN apt-get update \ # 更新软件包列表
 && apt-get install -y libmagickwand-dev \ # 安装软件包
 && apt-get clean \ # 清理软件包缓存
 && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* # 移除其他所有文件

RUN go get gopkg.in/gographics/imagick.v2/imagick

记得在apt-get install中添加-y,因为docker build是非交互式的。

英文:

Most base images have package lists removed to avoid to reduce image size. Thus, in order to install something with apt-get, you first need to update the package lists and then install whatever package you wish. Then, after installing the package, remove all side-effects of running apt to avoid polluting the image with unneeded files (all that necessarily as a single RUN command).

The following Dockerfile should do the trick:

FROM golang:latest

RUN apt-get update \ # update package lists
 && apt-get install -y libmagickwand-dev \ # install the package
 && apt-get clean \ # clean package cache
 && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* # remove everything else

RUN go get gopkg.in/gographics/imagick.v2/imagick

Remember to add -y to apt-get install, because docker build is non-interactive.

huangapple
  • 本文由 发表于 2016年8月21日 05:40:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/39058933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定